mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2024-11-14 02:04:14 +01:00
Move MediaAnalysis parsing helper methods to static class
This commit is contained in:
parent
0a146251e7
commit
8a314f02ae
1 changed files with 56 additions and 49 deletions
|
@ -7,8 +7,6 @@ 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);
|
|
||||||
|
|
||||||
internal MediaAnalysis(FFProbeAnalysis analysis)
|
internal MediaAnalysis(FFProbeAnalysis analysis)
|
||||||
{
|
{
|
||||||
Format = ParseFormat(analysis.Format);
|
Format = ParseFormat(analysis.Format);
|
||||||
|
@ -50,14 +48,14 @@ private VideoStream ParseVideoStream(FFProbeStream stream)
|
||||||
return new VideoStream
|
return new VideoStream
|
||||||
{
|
{
|
||||||
Index = stream.Index,
|
Index = stream.Index,
|
||||||
AvgFrameRate = DivideRatio(ParseRatioDouble(stream.AvgFrameRate, '/')),
|
AvgFrameRate = MediaAnalysisUtils.DivideRatio(MediaAnalysisUtils.ParseRatioDouble(stream.AvgFrameRate, '/')),
|
||||||
BitRate = !string.IsNullOrEmpty(stream.BitRate) ? ParseIntInvariant(stream.BitRate) : default,
|
BitRate = !string.IsNullOrEmpty(stream.BitRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.BitRate) : default,
|
||||||
BitsPerRawSample = !string.IsNullOrEmpty(stream.BitsPerRawSample) ? ParseIntInvariant(stream.BitsPerRawSample) : default,
|
BitsPerRawSample = !string.IsNullOrEmpty(stream.BitsPerRawSample) ? MediaAnalysisUtils.ParseIntInvariant(stream.BitsPerRawSample) : default,
|
||||||
CodecName = stream.CodecName,
|
CodecName = stream.CodecName,
|
||||||
CodecLongName = stream.CodecLongName,
|
CodecLongName = stream.CodecLongName,
|
||||||
DisplayAspectRatio = ParseRatioInt(stream.DisplayAspectRatio, ':'),
|
DisplayAspectRatio = MediaAnalysisUtils.ParseRatioInt(stream.DisplayAspectRatio, ':'),
|
||||||
Duration = ParseDuration(stream),
|
Duration = MediaAnalysisUtils.ParseDuration(stream),
|
||||||
FrameRate = DivideRatio(ParseRatioDouble(stream.FrameRate, '/')),
|
FrameRate = MediaAnalysisUtils.DivideRatio(MediaAnalysisUtils.ParseRatioDouble(stream.FrameRate, '/')),
|
||||||
Height = stream.Height ?? 0,
|
Height = stream.Height ?? 0,
|
||||||
Width = stream.Width ?? 0,
|
Width = stream.Width ?? 0,
|
||||||
Profile = stream.Profile,
|
Profile = stream.Profile,
|
||||||
|
@ -68,7 +66,56 @@ private VideoStream ParseVideoStream(FFProbeStream stream)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private static TimeSpan ParseDuration(FFProbeStream ffProbeStream)
|
|
||||||
|
private AudioStream ParseAudioStream(FFProbeStream stream)
|
||||||
|
{
|
||||||
|
return new AudioStream
|
||||||
|
{
|
||||||
|
Index = stream.Index,
|
||||||
|
BitRate = !string.IsNullOrEmpty(stream.BitRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.BitRate) : default,
|
||||||
|
CodecName = stream.CodecName,
|
||||||
|
CodecLongName = stream.CodecLongName,
|
||||||
|
Channels = stream.Channels ?? default,
|
||||||
|
ChannelLayout = stream.ChannelLayout,
|
||||||
|
Duration = MediaAnalysisUtils.ParseDuration(stream),
|
||||||
|
SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? MediaAnalysisUtils.ParseIntInvariant(stream.SampleRate) : default,
|
||||||
|
Profile = stream.Profile,
|
||||||
|
Language = stream.GetLanguage(),
|
||||||
|
Tags = stream.Tags,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MediaAnalysisUtils
|
||||||
|
{
|
||||||
|
private static readonly Regex DurationRegex = new Regex("^(\\d{1,5}:\\d{1,2}:\\d{1,2}(.\\d{1,7})?)", RegexOptions.Compiled);
|
||||||
|
|
||||||
|
public static double DivideRatio((double, double) ratio) => ratio.Item1 / ratio.Item2;
|
||||||
|
|
||||||
|
public static (int, int) ParseRatioInt(string input, char separator)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(input)) return (0, 0);
|
||||||
|
var ratio = input.Split(separator);
|
||||||
|
return (ParseIntInvariant(ratio[0]), ParseIntInvariant(ratio[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static (double, double) ParseRatioDouble(string input, char separator)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(input)) return (0, 0);
|
||||||
|
var ratio = input.Split(separator);
|
||||||
|
return (ratio.Length > 0 ? ParseDoubleInvariant(ratio[0]) : 0, ratio.Length > 1 ? ParseDoubleInvariant(ratio[1]) : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double ParseDoubleInvariant(string line) =>
|
||||||
|
double.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
|
public static int ParseIntInvariant(string line) =>
|
||||||
|
int.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture);
|
||||||
|
|
||||||
|
|
||||||
|
public static TimeSpan ParseDuration(FFProbeStream ffProbeStream)
|
||||||
{
|
{
|
||||||
return !string.IsNullOrEmpty(ffProbeStream.Duration)
|
return !string.IsNullOrEmpty(ffProbeStream.Duration)
|
||||||
? TimeSpan.Parse(ffProbeStream.Duration)
|
? TimeSpan.Parse(ffProbeStream.Duration)
|
||||||
|
@ -80,45 +127,5 @@ private static TimeSpan ParseDuration(FFProbeStream ffProbeStream)
|
||||||
var durationMatch = DurationRegex.Match(durationTag ?? "");
|
var durationMatch = DurationRegex.Match(durationTag ?? "");
|
||||||
return durationMatch.Success ? durationMatch.Groups[1].Value : null;
|
return durationMatch.Success ? durationMatch.Groups[1].Value : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private AudioStream ParseAudioStream(FFProbeStream stream)
|
|
||||||
{
|
|
||||||
return new AudioStream
|
|
||||||
{
|
|
||||||
Index = stream.Index,
|
|
||||||
BitRate = !string.IsNullOrEmpty(stream.BitRate) ? ParseIntInvariant(stream.BitRate) : default,
|
|
||||||
CodecName = stream.CodecName,
|
|
||||||
CodecLongName = stream.CodecLongName,
|
|
||||||
Channels = stream.Channels ?? default,
|
|
||||||
ChannelLayout = stream.ChannelLayout,
|
|
||||||
Duration = ParseDuration(stream),
|
|
||||||
SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? ParseIntInvariant(stream.SampleRate) : default,
|
|
||||||
Profile = stream.Profile,
|
|
||||||
Language = stream.GetLanguage(),
|
|
||||||
Tags = stream.Tags,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static double DivideRatio((double, double) ratio) => ratio.Item1 / ratio.Item2;
|
|
||||||
|
|
||||||
private static (int, int) ParseRatioInt(string input, char separator)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(input)) return (0, 0);
|
|
||||||
var ratio = input.Split(separator);
|
|
||||||
return (ParseIntInvariant(ratio[0]), ParseIntInvariant(ratio[1]));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static (double, double) ParseRatioDouble(string input, char separator)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(input)) return (0, 0);
|
|
||||||
var ratio = input.Split(separator);
|
|
||||||
return (ratio.Length > 0 ? ParseDoubleInvariant(ratio[0]) : 0, ratio.Length > 1 ? ParseDoubleInvariant(ratio[1]) : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static double ParseDoubleInvariant(string line) =>
|
|
||||||
double.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture);
|
|
||||||
|
|
||||||
private static int ParseIntInvariant(string line) =>
|
|
||||||
int.Parse(line, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue