-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFFProbe.cs
More file actions
413 lines (293 loc) · 12.8 KB
/
FFProbe.cs
File metadata and controls
413 lines (293 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace FFBitrateViewer
{
public class FFProbeFormat
{
[JsonProperty("bit_rate")]
public long? BitRate { get; set; }
/// <summary>Approximate duration in seconds (stream can start *after* the 00:00:00 timecode).</summary>
[JsonProperty("duration")]
public double? Duration { get; set; }
[JsonProperty("filename")]
public string? FileName { get; set; }
[JsonProperty("format_long_name")]
public string? FormatLongName { get; set; }
[JsonProperty("format_name")]
public string? FormatName { get; set; }
[JsonProperty("probe_score")]
public int? ProbeScore { get; set; }
[JsonProperty("nb_programs")]
public int? ProgramsCount { get; set; }
[JsonProperty("size")]
public long? Size { get; set; }
[JsonProperty("start_time")]
public double? StartTime { get; set; }
[JsonProperty("nb_streams")]
public int? StreamsCount { get; set; }
/// <summary>Container and format tags/metadata, not stream-specific tags.</summary>
[JsonProperty("tags")]
public Dictionary<string, string?>? Tags { get; set; }
[JsonExtensionData]
public Dictionary<string, object?>? ExtensionData { get; set; }
}
public class FFProbeFrame
{
[JsonProperty("best_effort_timestamp")]
public int? BestEffortTimestamp { get; set; }
[JsonProperty("best_effort_timestamp_time")]
public double? BestEffortTimestampTime { get; set; }
[JsonProperty("channel_layout")]
public string? ChannelLayout { get; set; }
[JsonProperty("channels")]
public int? Channels { get; set; }
[JsonProperty("coded_picture_number")]
public int? CodedPictureNumber { get; set; }
[JsonProperty("chroma_location")]
public string? ChromaLocation { get; set; }
[JsonProperty("display_picture_number")]
public int? DisplayPictureNumber { get; set; }
[JsonProperty("pkt_dts")]
public int? DTS { get; set; }
[JsonProperty("pkt_dts_time")]
public double? DTSTime { get; set; }
[JsonProperty("pkt_duration")]
public int? Duration { get; set; }
[JsonProperty("pkt_duration_time")]
public double? DurationTime { get; set; }
[JsonProperty("height")]
public int? Height { get; set; }
[JsonProperty("interlaced_frame")]
public bool? InterlacedFrame { get; set; } // todo@ type
[JsonProperty("key_frame")]
public bool? IsKeyFrame { get; set; } // todo@ type
[JsonProperty("media_type")]
public string? MediaType { get; set; }
[JsonProperty("nb_samples")]
public int? NBSamples { get; set; }
[JsonProperty("pict_type")]
public string? PictType { get; set; } // I, P, B
[JsonProperty("pix_fmt")]
public string? PixFmt { get; set; }
// Can be ordered by the field if no time
[JsonProperty("pkt_pos")]
public long? Pos { get; set; }
[JsonProperty("pkt_pts")]
public int? PTS { get; set; }
[JsonProperty("pkt_pts_time")]
public double? PTSTime { get; set; }
[JsonProperty("repeat_pict")]
public bool? RepeatPict { get; set; } // todo@ type
[JsonProperty("sample_aspect_ratio")]
public string? SAR { get; set; }
[JsonProperty("pkt_size")]
public int? Size { get; set; }
[JsonProperty("sample_fmt")]
public string? SampleFmt { get; set; }
[JsonProperty("stream_index")]
public int? StreamIndex { get; set; }
[JsonProperty("top_field_first")]
public bool? TopFieldFirst { get; set; } // todo@ type
[JsonProperty("width")]
public int? Width { get; set; }
[JsonExtensionData]
public Dictionary<string, object?>? ExtensionData { get; set; }
}
public class FFProbePacket
{
[JsonProperty("codec_type")]
public string? CodecType { get; set; }
// decoding time stamp -- how packets stored in stream
[JsonProperty("dts")]
public int? DTS { get; set; }
[JsonProperty("dts_time")]
public double? DTSTime { get; set; }
[JsonProperty("duration")]
public int? Duration { get; set; }
[JsonProperty("duration_time")]
public double? DurationTime { get; set; }
[JsonProperty("flags")]
public string? Flags { get; set; }
// Can be ordered by the field if no time
[JsonProperty("pos")]
public long? Pos { get; set; }
// presentation time stamp -- how packets should be displayed
[JsonProperty("pts")]
public int? PTS { get; set; }
[JsonProperty("pts_time")]
public double? PTSTime { get; set; }
[JsonProperty("size")]
public int? Size { get; set; }
[JsonProperty("stream_index")]
public int? StreamIndex { get; set; }
[JsonExtensionData]
public Dictionary<string, object?>? ExtensionData { get; set; }
}
public class FFProbeStream
{
[JsonProperty("bit_rate")]
public long? BitRate { get; set; }
[JsonProperty("bits_per_sample")]
public int? BitsPerSample { get; set; }
[JsonProperty("bits_per_raw_sample")]
public string? BitsPerSampleRaw { get; set; }
[JsonProperty("channel_layout")]
public string? ChannelLayout { get; set; }
[JsonProperty("channels")]
public int? Channels { get; set; }
[JsonProperty("chroma_location")]
public string? ChromaLocation { get; set; }
[JsonProperty("codec_name")]
public string? CodecName { get; set; }
/// <summary>H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10</summary>
[JsonProperty("codec_long_name")]
public string? CodecLongName { get; set; }
[JsonProperty("codec_type")]
public string? CodecType { get; set; }
[JsonProperty("codec_tag")]
public string? CodecTag { get; set; }
/// <summary>Video codec's FourCC or audio codec's TwoCC</summary>
[JsonProperty("codec_tag_string")]
public string? CodecTagString { get; set; }
[JsonProperty("coded_height")]
public int? CodedHeight { get; set; }
[JsonProperty("coded_width")]
public int? CodedWidth { get; set; }
[JsonProperty("color_range")]
public string? ColorRange { get; set; }
[JsonProperty("display_aspect_ratio")]
public string? DAR { get; set; }
[JsonProperty("duration")]
public double? Duration { get; set; }
/// <summary>Duration expressed in integer time-base units (https://video.stackexchange.com/questions/27546/difference-between-duration-ts-and-duration-in-ffprobe-output</summary>
[JsonProperty("duration_ts")]
public long? DurationTS { get; set; }
[JsonProperty("field_order")]
public string? FieldOrder { get; set; }
[JsonProperty("nb_frames")]
public int? FramesCount { get; set; }
[JsonProperty("r_frame_rate")]
public string? FrameRateR { get; set; }
[JsonProperty("avg_frame_rate")]
public string? FrameRateAvg { get; set; }
[JsonProperty("height")]
public int? Height { get; set; }
[JsonProperty("id")]
public string? Id { get; set; }
[JsonProperty("index")]
public int Index { get; set; }
[JsonProperty("is_avc")]
public bool? IsAVC { get; set; }
[JsonProperty("has_b_frames")]
public bool? IsHasBFrames { get; set; } // todo@ type
[JsonProperty("level")]
public int? Level { get; set; }
[JsonProperty("nb_packets")]
public int? PacketsCount { get; set; }
[JsonProperty("pix_fmt")]
public string? PixFmt { get; set; }
[JsonProperty("profile")]
public string? Profile { get; set; }
[JsonProperty("refs")]
public int? Refs { get; set; }
[JsonProperty("sample_fmt")]
public string? SampleFormat { get; set; }
[JsonProperty("sample_rate")]
public int? SampleRate { get; set; }
[JsonProperty("sample_aspect_ratio")]
public string? SAR { get; set; }
[JsonProperty("start_pts")]
public long? StartPTS { get; set; }
[JsonProperty("start_time")]
public double? StartTime { get; set; }
/// <summary>Stream-specific tags/metadata. See <see cref="KnownFFProbeVideoStreamTags"/>.</summary>
[JsonProperty("tags")]
public Dictionary<string, string?>? Tags { get; set; }
/// <summary>Values like "1/600". See https://stackoverflow.com/questions/43333542/what-is-video-timescale-timebase-or-timestamp-in-ffmpeg </summary>
[JsonProperty("time_base")]
public string? TimeBase { get; set; }
[JsonProperty("width")]
public int? Width { get; set; }
[JsonExtensionData]
public Dictionary<string, object?>? ExtensionData { get; set; }
}
/// <summary>Known tag-names for <see cref="FFProbeStream"/>'s <see cref="FFProbeStream.Tags"/>.</summary>
public static class KnownFFProbeVideoStreamTags
{
/// <summary>Tag value is ISO 8601 datetime</summary>
public const string CreationTime = "creation_time";
/// <summary>Values like H.264</summary>
public const string Encoder = "encoder";
/// <summary>Tag value is a decimal integer value in degrees, e.g. "90".</summary>
public const string Rotate = "rotate";
}
public class FFProbeJsonOutput
{
/// <summary>Information about the container</summary>
[JsonProperty("format")]
public FFProbeFormat? Format { get; set; }
/// <summary>Information about frames</summary>
//[JsonProperty("frames")]
//public List<FFProbeFrame>? Frames { get; set; }
/// <summary>Information about packets</summary>
[JsonProperty("packets")]
public List<FFProbePacket>? Packets { get; set; }
/// <summary>Information about streams</summary>
[JsonProperty("streams")]
public List<FFProbeStream>? Streams { get; set; }
[JsonExtensionData]
public Dictionary<string, object?>? ExtensionData { get; set; }
}
public static class FFProbeOutputExtensions
{
public static FFProbeStream? GetFirstVideoStream(this FFProbeJsonOutput ffProbeOutput)
{
return GetFirstStreamByType(ffProbeOutput, "video");
}
public static FFProbeStream? GetFirstAudioStream(this FFProbeJsonOutput ffProbeOutput)
{
return GetFirstStreamByType(ffProbeOutput, "audio");
}
public static FFProbeStream? GetFirstStreamByType(this FFProbeJsonOutput ffProbeOutput, string type)
{
if (ffProbeOutput.Streams != null)
{
foreach (var stream in ffProbeOutput.Streams)
{
if (string.Equals(stream.CodecType, type, StringComparison.OrdinalIgnoreCase)) return stream;
}
}
return null;
}
public static int? GetRotation(this FFProbeStream ffProbeStream)
{
if (ffProbeStream.Tags != null)
{
if (ffProbeStream.Tags.TryGetValue(KnownFFProbeVideoStreamTags.Rotate, out string? rotateTagValue))
{
if (int.TryParse(rotateTagValue, NumberStyles.Any, CultureInfo.InvariantCulture, out int rotateTagValueInt)) return rotateTagValueInt;
}
}
return null;
}
/// <summary>If the <see cref="FFProbeJsonOutput.Format"/> has a duration, that's returned - otherwise max first stream duration is returned. Returns null if none of the streams (nor the <see cref="FFProbeJsonOutput.Format"/>) has a duration value set.</summary>
public static double? GetDuration(this FFProbeJsonOutput ffProbeOutput)
{
double? result = null;
if (ffProbeOutput.Format != null && ffProbeOutput.Format.Duration.HasValue) result = ffProbeOutput.Format.Duration.Value;
if (result == null && ffProbeOutput.Streams != null)
{
foreach (FFProbeStream stream in ffProbeOutput.Streams)
{
double? duration = stream.Duration;
double start = stream.StartTime.HasValue ? (double)stream.StartTime : 0;
// todo@ should start is taken into consideration?
if (duration.HasValue && (result == null && (duration + start) > result)) result = duration + start;
}
}
return result;
}
}
}