Add chapters to FFProbe

This commit is contained in:
Phillip Fisher 2023-02-25 11:29:41 -06:00
parent dc88862602
commit 38789162eb
5 changed files with 49 additions and 2 deletions

View file

@ -6,11 +6,15 @@ public class ChapterData
public TimeSpan Start { get; private set; } public TimeSpan Start { get; private set; }
public TimeSpan End { get; private set; } public TimeSpan End { get; private set; }
public TimeSpan Duration { get; private set; }
public ChapterData(string title, TimeSpan start, TimeSpan end) public ChapterData(string title, TimeSpan start, TimeSpan end)
{ {
Title = title; Title = title;
Start = start; Start = start;
End = end; End = end;
Duration = end - start;
} }
} }
} }

View file

@ -213,7 +213,7 @@ private static void ThrowIfExitCodeNotZero(IProcessResult result)
} }
private static ProcessArguments PrepareStreamAnalysisInstance(string filePath, FFOptions ffOptions) private static ProcessArguments PrepareStreamAnalysisInstance(string filePath, FFOptions ffOptions)
=> PrepareInstance($"-loglevel error -print_format json -show_format -sexagesimal -show_streams \"{filePath}\"", ffOptions); => PrepareInstance($"-loglevel error -print_format json -show_format -sexagesimal -show_streams -show_chapters \"{filePath}\"", ffOptions);
private static ProcessArguments PrepareFrameAnalysisInstance(string filePath, FFOptions ffOptions) private static ProcessArguments PrepareFrameAnalysisInstance(string filePath, FFOptions ffOptions)
=> PrepareInstance($"-loglevel error -print_format json -show_frames -v quiet -sexagesimal \"{filePath}\"", ffOptions); => PrepareInstance($"-loglevel error -print_format json -show_frames -v quiet -sexagesimal \"{filePath}\"", ffOptions);
private static ProcessArguments PreparePacketAnalysisInstance(string filePath, FFOptions ffOptions) private static ProcessArguments PreparePacketAnalysisInstance(string filePath, FFOptions ffOptions)

View file

@ -11,6 +11,9 @@ public class FFProbeAnalysis
[JsonPropertyName("format")] [JsonPropertyName("format")]
public Format Format { get; set; } = null!; public Format Format { get; set; } = null!;
[JsonPropertyName("chapters")]
public List<Chapter> Chapters { get; set; } = null!;
[JsonIgnore] [JsonIgnore]
public IReadOnlyList<string> ErrorData { get; set; } = new List<string>(); public IReadOnlyList<string> ErrorData { get; set; } = new List<string>();
} }
@ -129,6 +132,30 @@ public class Format : ITagsContainer
public Dictionary<string, string> Tags { get; set; } = null!; public Dictionary<string, string> Tags { get; set; } = null!;
} }
public class Chapter : ITagsContainer
{
[JsonPropertyName("id")]
public int Id { get; set; }
[JsonPropertyName("time_base")]
public string TimeBase { get; set; } = null!;
[JsonPropertyName("start")]
public int Start { get; set; }
[JsonPropertyName("start_time")]
public string StartTime { get; set; } = null!;
[JsonPropertyName("end")]
public int End { get; set; }
[JsonPropertyName("end_time")]
public string EndTime { get; set; } = null!;
[JsonPropertyName("tags")]
public Dictionary<string, string> Tags { get; set; } = null!;
}
public interface IDispositionContainer public interface IDispositionContainer
{ {
Dictionary<string, int> Disposition { get; set; } Dictionary<string, int> Disposition { get; set; }

View file

@ -1,9 +1,12 @@
namespace FFMpegCore using FFMpegCore.Builders.MetaData;
namespace FFMpegCore
{ {
public interface IMediaAnalysis public interface IMediaAnalysis
{ {
TimeSpan Duration { get; } TimeSpan Duration { get; }
MediaFormat Format { get; } MediaFormat Format { get; }
List<ChapterData> Chapters { get; }
AudioStream? PrimaryAudioStream { get; } AudioStream? PrimaryAudioStream { get; }
VideoStream? PrimaryVideoStream { get; } VideoStream? PrimaryVideoStream { get; }
SubtitleStream? PrimarySubtitleStream { get; } SubtitleStream? PrimarySubtitleStream { get; }

View file

@ -1,4 +1,5 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using FFMpegCore.Builders.MetaData;
namespace FFMpegCore namespace FFMpegCore
{ {
@ -7,6 +8,7 @@ internal class MediaAnalysis : IMediaAnalysis
internal MediaAnalysis(FFProbeAnalysis analysis) internal MediaAnalysis(FFProbeAnalysis analysis)
{ {
Format = ParseFormat(analysis.Format); Format = ParseFormat(analysis.Format);
Chapters = analysis.Chapters.Select(c => ParseChapter(c)).ToList();
VideoStreams = analysis.Streams.Where(stream => stream.CodecType == "video").Select(ParseVideoStream).ToList(); VideoStreams = analysis.Streams.Where(stream => stream.CodecType == "video").Select(ParseVideoStream).ToList();
AudioStreams = analysis.Streams.Where(stream => stream.CodecType == "audio").Select(ParseAudioStream).ToList(); AudioStreams = analysis.Streams.Where(stream => stream.CodecType == "audio").Select(ParseAudioStream).ToList();
SubtitleStreams = analysis.Streams.Where(stream => stream.CodecType == "subtitle").Select(ParseSubtitleStream).ToList(); SubtitleStreams = analysis.Streams.Where(stream => stream.CodecType == "subtitle").Select(ParseSubtitleStream).ToList();
@ -28,6 +30,15 @@ private MediaFormat ParseFormat(Format analysisFormat)
}; };
} }
private ChapterData ParseChapter(Chapter analysisChapter)
{
var title = analysisChapter.Tags.FirstOrDefault(t => t.Key == "title").Value;
var start = MediaAnalysisUtils.ParseDuration(analysisChapter.StartTime);
var end = MediaAnalysisUtils.ParseDuration(analysisChapter.EndTime);
return new ChapterData(title, start, end);
}
public TimeSpan Duration => new[] public TimeSpan Duration => new[]
{ {
Format.Duration, Format.Duration,
@ -37,6 +48,8 @@ private MediaFormat ParseFormat(Format analysisFormat)
public MediaFormat Format { get; } public MediaFormat Format { get; }
public List<ChapterData> Chapters { get; }
public AudioStream? PrimaryAudioStream => AudioStreams.OrderBy(stream => stream.Index).FirstOrDefault(); public AudioStream? PrimaryAudioStream => AudioStreams.OrderBy(stream => stream.Index).FirstOrDefault();
public VideoStream? PrimaryVideoStream => VideoStreams.OrderBy(stream => stream.Index).FirstOrDefault(); public VideoStream? PrimaryVideoStream => VideoStreams.OrderBy(stream => stream.Index).FirstOrDefault();
public SubtitleStream? PrimarySubtitleStream => SubtitleStreams.OrderBy(stream => stream.Index).FirstOrDefault(); public SubtitleStream? PrimarySubtitleStream => SubtitleStreams.OrderBy(stream => stream.Index).FirstOrDefault();