trim excess fractions of duration tag

Former-commit-id: 9be3fc5a07
This commit is contained in:
Malte Rosenbjerg 2020-06-16 07:42:14 +02:00
parent f72a301d78
commit 5ee26d52ad

View file

@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace FFMpegCore
{
public class MediaAnalysis
{
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)
{
VideoStreams = analysis.Streams.Where(stream => stream.CodecType == "video").Select(ParseVideoStream).ToList();
@ -54,7 +56,12 @@ private static TimeSpan ParseDuration(FFProbeStream ffProbeStream)
{
return ffProbeStream.Duration != null
? TimeSpan.FromSeconds(ParseDoubleInvariant(ffProbeStream.Duration))
: TimeSpan.Parse(ffProbeStream.Tags?.Duration ?? "0");
: TimeSpan.Parse(TrimTimeSpan(ffProbeStream.Tags?.Duration) ?? "0");
}
private static string? TrimTimeSpan(string? durationTag)
{
var durationMatch = DurationRegex.Match(durationTag ?? "");
return durationMatch.Success ? durationMatch.Groups[1].Value : null;
}
private AudioStream ParseAudioStream(FFProbeStream stream)
@ -67,7 +74,7 @@ private AudioStream ParseAudioStream(FFProbeStream stream)
CodecLongName = stream.CodecLongName,
Channels = stream.Channels ?? default,
ChannelLayout = stream.ChannelLayout,
Duration = TimeSpan.FromSeconds(ParseDoubleInvariant(stream.Duration ?? stream.Tags?.Duration ?? "0")),
Duration = ParseDuration(stream),
SampleRateHz = !string.IsNullOrEmpty(stream.SampleRate) ? ParseIntInvariant(stream.SampleRate) : default,
Language = stream.Tags?.Language
};