Skip to content

Commit

Permalink
Merge branch 'main' into pr/431
Browse files Browse the repository at this point in the history
  • Loading branch information
rosenbjerg committed Oct 5, 2023
2 parents 54e5d08 + 20a5b15 commit f8407bc
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
1 change: 1 addition & 0 deletions FFMpegCore.Test/FFProbeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public void Probe_Success()
{
var info = FFProbe.Analyse(TestResources.Mp4Video);
Assert.AreEqual(3, info.Duration.Seconds);
Assert.AreEqual(0, info.Chapters.Count);

Assert.AreEqual("5.1", info.PrimaryAudioStream!.ChannelLayout);
Assert.AreEqual(6, info.PrimaryAudioStream.Channels);
Expand Down
2 changes: 2 additions & 0 deletions FFMpegCore/FFMpeg/Builders/MetaData/ChapterData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class ChapterData
public TimeSpan Start { get; private set; }
public TimeSpan End { get; private set; }

public TimeSpan Duration => End - Start;

public ChapterData(string title, TimeSpan start, TimeSpan end)
{
Title = title;
Expand Down
2 changes: 1 addition & 1 deletion FFMpegCore/FFProbe/FFProbe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private static void ThrowIfExitCodeNotZero(IProcessResult result)
}

private static ProcessArguments PrepareStreamAnalysisInstance(string filePath, FFOptions ffOptions, string? customArguments)
=> PrepareInstance($"-loglevel error -print_format json -show_format -sexagesimal -show_streams \"{filePath}\"", ffOptions, customArguments);
=> PrepareInstance($"-loglevel error -print_format json -show_format -sexagesimal -show_streams -show_chapters \"{filePath}\"", ffOptions, customArguments);
private static ProcessArguments PrepareFrameAnalysisInstance(string filePath, FFOptions ffOptions, string? customArguments)
=> PrepareInstance($"-loglevel error -print_format json -show_frames -v quiet -sexagesimal \"{filePath}\"", ffOptions, customArguments);
private static ProcessArguments PreparePacketAnalysisInstance(string filePath, FFOptions ffOptions, string? customArguments)
Expand Down
27 changes: 27 additions & 0 deletions FFMpegCore/FFProbe/FFProbeAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class FFProbeAnalysis
[JsonPropertyName("format")]
public Format Format { get; set; } = null!;

[JsonPropertyName("chapters")]
public List<Chapter> Chapters { get; set; } = null!;

[JsonIgnore]
public IReadOnlyList<string> ErrorData { get; set; } = new List<string>();
}
Expand Down Expand Up @@ -129,6 +132,30 @@ public class Format : ITagsContainer
public Dictionary<string, string>? Tags { get; set; }
}

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; }
}

public interface IDispositionContainer
{
Dictionary<string, int> Disposition { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion FFMpegCore/FFProbe/IMediaAnalysis.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
namespace FFMpegCore
using FFMpegCore.Builders.MetaData;

namespace FFMpegCore
{
public interface IMediaAnalysis
{
TimeSpan Duration { get; }
MediaFormat Format { get; }
List<ChapterData> Chapters { get; }
AudioStream? PrimaryAudioStream { get; }
VideoStream? PrimaryVideoStream { get; }
SubtitleStream? PrimarySubtitleStream { get; }
Expand Down
13 changes: 13 additions & 0 deletions FFMpegCore/FFProbe/MediaAnalysis.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.RegularExpressions;
using FFMpegCore.Builders.MetaData;

namespace FFMpegCore
{
Expand All @@ -7,6 +8,7 @@ internal class MediaAnalysis : IMediaAnalysis
internal MediaAnalysis(FFProbeAnalysis analysis)
{
Format = ParseFormat(analysis.Format);
Chapters = analysis.Chapters.Select(c => ParseChapter(c)).ToList();
VideoStreams = analysis.Streams.Where(stream => stream.CodecType == "video").Select(ParseVideoStream).ToList();
AudioStreams = analysis.Streams.Where(stream => stream.CodecType == "audio").Select(ParseAudioStream).ToList();
SubtitleStreams = analysis.Streams.Where(stream => stream.CodecType == "subtitle").Select(ParseSubtitleStream).ToList();
Expand All @@ -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[]
{
Format.Duration,
Expand All @@ -37,6 +48,8 @@ private MediaFormat ParseFormat(Format analysisFormat)

public MediaFormat Format { get; }

public List<ChapterData> Chapters { get; }

public AudioStream? PrimaryAudioStream => AudioStreams.OrderBy(stream => stream.Index).FirstOrDefault();
public VideoStream? PrimaryVideoStream => VideoStreams.OrderBy(stream => stream.Index).FirstOrDefault();
public SubtitleStream? PrimarySubtitleStream => SubtitleStreams.OrderBy(stream => stream.Index).FirstOrDefault();
Expand Down
17 changes: 14 additions & 3 deletions FFMpegCore/GlobalFFOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ private static string GetFFBinaryPath(string name, FFOptions ffOptions)
}

var target = Environment.Is64BitProcess ? "x64" : "x86";
if (Directory.Exists(Path.Combine(ffOptions.BinaryFolder, target)))
var possiblePaths = new List<string>()
{
ffName = Path.Combine(target, ffName);
Path.Combine(ffOptions.BinaryFolder, target),
ffOptions.BinaryFolder
};

foreach (var possiblePath in possiblePaths)
{
var possibleFFMpegPath = Path.Combine(possiblePath, ffName);
if (File.Exists(possibleFFMpegPath))
{
return possibleFFMpegPath;
}
}

return Path.Combine(ffOptions.BinaryFolder, ffName);
//Fall back to the assumption this tool exists in the PATH
return ffName;
}

private static FFOptions LoadFFOptions()
Expand Down

0 comments on commit f8407bc

Please sign in to comment.