Merge pull request #165 from hey-red/expose-tags

Add tags on media stream and format
This commit is contained in:
Malte Rosenbjerg 2021-01-29 17:50:39 +01:00 committed by GitHub
commit 8d2251c136
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View file

@ -8,6 +8,7 @@ namespace FFMpegCore
internal class MediaAnalysis : IMediaAnalysis internal class MediaAnalysis : IMediaAnalysis
{ {
private static readonly Regex DurationRegex = new Regex("^(\\d{1,2}:\\d{1,2}:\\d{1,2}(.\\d{1,7})?)", RegexOptions.Compiled); private static readonly Regex DurationRegex = new Regex("^(\\d{1,2}:\\d{1,2}:\\d{1,2}(.\\d{1,7})?)", RegexOptions.Compiled);
internal MediaAnalysis(string path, FFProbeAnalysis analysis) internal MediaAnalysis(string path, FFProbeAnalysis analysis)
{ {
Format = ParseFormat(analysis.Format); Format = ParseFormat(analysis.Format);
@ -27,14 +28,15 @@ private MediaFormat ParseFormat(Format analysisFormat)
FormatLongName = analysisFormat.FormatLongName, FormatLongName = analysisFormat.FormatLongName,
StreamCount = analysisFormat.NbStreams, StreamCount = analysisFormat.NbStreams,
ProbeScore = analysisFormat.ProbeScore, ProbeScore = analysisFormat.ProbeScore,
BitRate = long.Parse(analysisFormat.BitRate ?? "0") BitRate = long.Parse(analysisFormat.BitRate ?? "0"),
Tags = analysisFormat.Tags,
}; };
} }
public string Path { get; } public string Path { get; }
public string Extension => System.IO.Path.GetExtension(Path); public string Extension => System.IO.Path.GetExtension(Path);
public TimeSpan Duration => new [] public TimeSpan Duration => new[]
{ {
Format.Duration, Format.Duration,
PrimaryVideoStream?.Duration ?? TimeSpan.Zero, PrimaryVideoStream?.Duration ?? TimeSpan.Zero,
@ -67,7 +69,8 @@ private VideoStream ParseVideoStream(FFProbeStream stream)
Profile = stream.Profile, Profile = stream.Profile,
PixelFormat = stream.PixelFormat, PixelFormat = stream.PixelFormat,
Rotation = (int)float.Parse(stream.GetRotate() ?? "0"), Rotation = (int)float.Parse(stream.GetRotate() ?? "0"),
Language = stream.GetLanguage() Language = stream.GetLanguage(),
Tags = stream.Tags,
}; };
} }
@ -77,6 +80,7 @@ private static TimeSpan ParseDuration(FFProbeStream ffProbeStream)
? TimeSpan.Parse(ffProbeStream.Duration) ? TimeSpan.Parse(ffProbeStream.Duration)
: TimeSpan.Parse(TrimTimeSpan(ffProbeStream.GetDuration()) ?? "0"); : TimeSpan.Parse(TrimTimeSpan(ffProbeStream.GetDuration()) ?? "0");
} }
private static string? TrimTimeSpan(string? durationTag) private static string? TrimTimeSpan(string? durationTag)
{ {
var durationMatch = DurationRegex.Match(durationTag ?? ""); var durationMatch = DurationRegex.Match(durationTag ?? "");
@ -96,17 +100,20 @@ private AudioStream ParseAudioStream(FFProbeStream stream)
Duration = ParseDuration(stream), Duration = ParseDuration(stream),
SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? ParseIntInvariant(stream.SampleRate) : default, SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? ParseIntInvariant(stream.SampleRate) : default,
Profile = stream.Profile, Profile = stream.Profile,
Language = stream.GetLanguage() Language = stream.GetLanguage(),
Tags = stream.Tags,
}; };
} }
private static double DivideRatio((double, double) ratio) => ratio.Item1 / ratio.Item2; private static double DivideRatio((double, double) ratio) => ratio.Item1 / ratio.Item2;
private static (int, int) ParseRatioInt(string input, char separator) private static (int, int) ParseRatioInt(string input, char separator)
{ {
if (string.IsNullOrEmpty(input)) return (0, 0); if (string.IsNullOrEmpty(input)) return (0, 0);
var ratio = input.Split(separator); var ratio = input.Split(separator);
return (ParseIntInvariant(ratio[0]), ParseIntInvariant(ratio[1])); return (ParseIntInvariant(ratio[0]), ParseIntInvariant(ratio[1]));
} }
private static (double, double) ParseRatioDouble(string input, char separator) private static (double, double) ParseRatioDouble(string input, char separator)
{ {
if (string.IsNullOrEmpty(input)) return (0, 0); if (string.IsNullOrEmpty(input)) return (0, 0);
@ -116,6 +123,7 @@ private static (double, double) ParseRatioDouble(string input, char separator)
private static double ParseDoubleInvariant(string line) => private static double ParseDoubleInvariant(string line) =>
double.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture); double.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture);
private static int ParseIntInvariant(string line) => private static int ParseIntInvariant(string line) =>
int.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture); int.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture);
} }

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
namespace FFMpegCore namespace FFMpegCore
{ {
@ -10,5 +11,6 @@ public class MediaFormat
public int StreamCount { get; set; } public int StreamCount { get; set; }
public double ProbeScore { get; set; } public double ProbeScore { get; set; }
public double BitRate { get; set; } public double BitRate { get; set; }
public Dictionary<string, string>? Tags { get; set; }
} }
} }

View file

@ -1,5 +1,7 @@
using FFMpegCore.Enums; using FFMpegCore.Enums;
using System; using System;
using System.Collections.Generic;
namespace FFMpegCore namespace FFMpegCore
{ {
@ -11,6 +13,7 @@ public class MediaStream
public int BitRate { get; internal set; } public int BitRate { get; internal set; }
public TimeSpan Duration { get; internal set; } public TimeSpan Duration { get; internal set; }
public string? Language { get; internal set; } public string? Language { get; internal set; }
public Dictionary<string, string>? Tags { get; internal set; }
public Codec GetCodecInfo() => FFMpeg.GetCodec(CodecName); public Codec GetCodecInfo() => FFMpeg.GetCodec(CodecName);
} }