@@ -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