Skip to content

Commit c1017bf

Browse files
author
Etienne Charland
committed
Version 1.1
1 parent 54afd7c commit c1017bf

16 files changed

+225
-556
lines changed

FFmpeg/EncoderApp.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace EmergenceGuardian.FFmpeg {
8+
/// <summary>
9+
/// Represents the application to use for encoding.
10+
/// </summary>
11+
public enum EncoderApp {
12+
Other,
13+
FFmpeg,
14+
x264
15+
}
16+
}

FFmpeg/EventHandlers.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public StartedEventArgs(FFmpegProcess process) {
5050
/// </summary>
5151
public class ProcessEventArgs : EventArgs {
5252
public Process Process { get; set; }
53+
public bool Handled { get; set; } = false;
5354

5455
public ProcessEventArgs() {
5556
}

FFmpeg/FFmpeg.csproj

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,8 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
43
<PropertyGroup>
5-
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
6-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<ProjectGuid>{C01178B4-A869-45F8-9B2B-7683F9EBB53D}</ProjectGuid>
8-
<OutputType>Library</OutputType>
9-
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>EmergenceGuardian.FFmpeg</RootNamespace>
11-
<AssemblyName>FFmpeg</AssemblyName>
12-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13-
<FileAlignment>512</FileAlignment>
14-
<NuGetPackageImportStamp>
15-
</NuGetPackageImportStamp>
16-
<TargetFrameworkProfile />
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
176
</PropertyGroup>
18-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19-
<PlatformTarget>AnyCPU</PlatformTarget>
20-
<DebugSymbols>true</DebugSymbols>
21-
<DebugType>full</DebugType>
22-
<Optimize>false</Optimize>
23-
<OutputPath>bin\Debug\</OutputPath>
24-
<DefineConstants>DEBUG;TRACE</DefineConstants>
25-
<ErrorReport>prompt</ErrorReport>
26-
<WarningLevel>4</WarningLevel>
27-
<Prefer32Bit>false</Prefer32Bit>
28-
</PropertyGroup>
29-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30-
<PlatformTarget>AnyCPU</PlatformTarget>
31-
<DebugType>pdbonly</DebugType>
32-
<Optimize>true</Optimize>
33-
<OutputPath>bin\Release\</OutputPath>
34-
<DefineConstants>TRACE</DefineConstants>
35-
<ErrorReport>prompt</ErrorReport>
36-
<WarningLevel>4</WarningLevel>
37-
<Prefer32Bit>false</Prefer32Bit>
38-
</PropertyGroup>
39-
<PropertyGroup>
40-
<StartupObject />
41-
</PropertyGroup>
42-
<ItemGroup>
43-
<Reference Include="System" />
44-
<Reference Include="System.Core" />
45-
<Reference Include="Microsoft.CSharp" />
46-
<Reference Include="System.Management" />
47-
</ItemGroup>
48-
<ItemGroup>
49-
<None Include="FfmpegBusiness.cs" />
50-
<Compile Include="CompletionStatus.cs" />
51-
<Compile Include="FFmpegConfig.cs" />
52-
<Compile Include="FFmpegDisplayMode.cs" />
53-
<Compile Include="FFmpegParser.cs" />
54-
<Compile Include="FFmpegStatus.cs" />
55-
<Compile Include="FFmpegStream.cs" />
56-
<Compile Include="FFmpegStreamInfo.cs" />
57-
<Compile Include="FFmpegProcess.cs" />
58-
<Compile Include="FFmpegStreamType.cs" />
59-
<Compile Include="MediaProcesses.cs" />
60-
<Compile Include="TimeLeftCalculator.cs" />
61-
<Compile Include="UserInterfaceManagerBase.cs" />
62-
<Compile Include="MediaInfo.cs" />
63-
<Compile Include="MediaEncoder.cs" />
64-
<Compile Include="MediaMuxer.cs" />
65-
<Compile Include="CompletedEventHandler.cs" />
66-
<Compile Include="ProcessStartOptions.cs" />
67-
<Compile Include="EventHandlers.cs" />
68-
<Compile Include="Properties\AssemblyInfo.cs" />
69-
</ItemGroup>
70-
<ItemGroup>
71-
<None Include="app.config" />
72-
</ItemGroup>
73-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
74-
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
75-
<PropertyGroup>
76-
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
77-
</PropertyGroup>
78-
</Target>
79-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
80-
Other similar extension points exist, see Microsoft.Common.targets.
81-
<Target Name="BeforeBuild">
82-
</Target>
83-
<Target Name="AfterBuild">
84-
</Target>
85-
-->
86-
</Project>
7+
8+
</Project>

FFmpeg/FFmpeg.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>EmergenceGuardian.FFmpeg</id>
5-
<version>1.0.1.0</version>
5+
<version>1.1.0.0</version>
66
<title>FFmpeg.NET</title>
77
<authors>Etienne Charland</authors>
88
<owners>Etienne Charland</owners>

FFmpeg/FFmpegParser.cs

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ internal static FFmpegStreamInfo ParseStreamInfo(string item) {
103103
string[] ColorSpaceValues = StreamInfo[1].Split('(', ')');
104104
V.ColorSpace = ColorSpaceValues[0];
105105
if (ColorSpaceValues.Length > 1) {
106-
string[] ColorRange = ColorSpaceValues[1].Split(';');
106+
string[] ColorRange = ColorSpaceValues[1].Split(new string[] { "; " }, StringSplitOptions.RemoveEmptyEntries);
107107
if (ColorRange.Any(c => c == "tv"))
108108
V.ColorRange = "tv";
109109
else if (ColorRange.Any(c => c == "pc"))
@@ -124,10 +124,12 @@ internal static FFmpegStreamInfo ParseStreamInfo(string item) {
124124
V.DAR2 = int.Parse(Size[7], CultureInfo.InvariantCulture);
125125
V.DisplayAspectRatio = Math.Round((double)V.DAR1 / V.DAR2, 3);
126126
}
127-
string Fps = StreamInfo.First(s => s.EndsWith("fps"));
128-
Fps = Fps.Substring(0, Fps.Length - 4);
129-
if (Fps != "1k") // sometimes it returns 1k ?
130-
V.FrameRate = double.Parse(Fps, CultureInfo.InvariantCulture);
127+
string Fps = StreamInfo.FirstOrDefault(s => s.EndsWith("fps"));
128+
if (Fps != null && Fps.Length > 4) {
129+
Fps = Fps.Substring(0, Fps.Length - 4);
130+
if (Fps != "1k") // sometimes it returns 1k ?
131+
V.FrameRate = double.Parse(Fps, CultureInfo.InvariantCulture);
132+
}
131133
}
132134
catch {
133135
}
@@ -159,20 +161,65 @@ internal static FFmpegStreamInfo ParseStreamInfo(string item) {
159161
/// </summary>
160162
/// <param name="text">The raw output line from FFmpeg.</param>
161163
/// <returns>A FFmpegProgress object.</returns>
162-
internal static FFmpegStatus ParseProgress(string text) {
164+
internal static FFmpegStatus ParseFFmpegProgress(string text) {
163165
FFmpegStatus Result = new FFmpegStatus();
164166
// frame= 929 fps=0.0 q=-0.0 size= 68483kB time=00:00:37.00 bitrate=15162.6kbits/s speed= 74x
165167
string[] Values = text.Split('=');
166168
try {
167-
Result.Frame = int.Parse(Values[1].TrimStart().Split(' ')[0], CultureInfo.InvariantCulture);
168-
Result.Fps = float.Parse(Values[2].TrimStart().Split(' ')[0], CultureInfo.InvariantCulture);
169-
Result.Quantizer = float.Parse(Values[3].TrimStart().Split(' ')[0], CultureInfo.InvariantCulture);
170-
Result.Size = Values[4].TrimStart().Split(' ')[0];
171-
Result.Time = TimeSpan.Parse(Values[5].TrimStart().Split(' ')[0], CultureInfo.InvariantCulture);
172-
Result.Bitrate = Values[6].TrimStart().Split(' ')[0];
173-
string SpeedString = Values[7].Trim().Split('x')[0];
169+
Result.Frame = long.Parse(ParseAttribute(text, "frame"), CultureInfo.InvariantCulture);
170+
Result.Fps = float.Parse(ParseAttribute(text, "fps"), CultureInfo.InvariantCulture);
171+
Result.Quantizer = float.Parse(ParseAttribute(text, "q"), CultureInfo.InvariantCulture);
172+
Result.Size = ParseAttribute(text, "size");
173+
Result.Time = TimeSpan.Parse(ParseAttribute(text, "time"), CultureInfo.InvariantCulture);
174+
Result.Bitrate = ParseAttribute(text, "bitrate");
175+
string SpeedString = ParseAttribute(text, "speed");
174176
if (SpeedString != "N/A")
175-
Result.Speed = float.Parse(SpeedString, CultureInfo.InvariantCulture);
177+
Result.Speed = float.Parse(SpeedString.TrimEnd('x'), CultureInfo.InvariantCulture);
178+
} catch {
179+
}
180+
return Result;
181+
}
182+
183+
/// <summary>
184+
/// Returns the value of specified attribute within a line of text. It will search 'key=' and return the following value until a space is found.
185+
/// </summary>
186+
/// <param name="text">The line of text to parse.</param>
187+
/// <param name="key">The key of the attribute to look for.</param>
188+
/// <returns></returns>
189+
internal static string ParseAttribute(string text, string key) {
190+
int Pos = text.IndexOf(key + "=");
191+
if (Pos >= 0) {
192+
// Find first non-space character.
193+
Pos += key.Length + 1;
194+
while (Pos < text.Length && text[Pos] == ' ') {
195+
Pos++;
196+
}
197+
// Find space after value.
198+
int PosEnd = text.IndexOf(' ', Pos);
199+
if (PosEnd == -1)
200+
PosEnd = text.Length;
201+
return text.Substring(Pos, PosEnd - Pos);
202+
} else
203+
return null;
204+
}
205+
206+
/// <summary>
207+
/// Parses x264's progress into an object.
208+
/// </summary>
209+
/// <param name="text">The raw output line from FFmpeg.</param>
210+
/// <returns>A FFmpegProgress object.</returns>
211+
internal static FFmpegStatus ParseX264Progress(string text) {
212+
FFmpegStatus Result = new FFmpegStatus();
213+
if (text.Length != 48)
214+
return Result;
215+
216+
// 1 0.10 10985.28 0:00:10 22.35 KB
217+
string[] Values = text.Split('=');
218+
try {
219+
Result.Frame = int.Parse(text.Substring(0, 6).Trim());
220+
Result.Fps = float.Parse(text.Substring(6, 7).Trim());
221+
Result.Bitrate = text.Substring(13, 10).Trim();
222+
Result.Size = text.Substring(34, 12).Trim();
176223
}
177224
catch {
178225
}

0 commit comments

Comments
 (0)