internal static void Check(Media media) { if (media.MediaType == TMediaType.Movie || media.MediaType == TMediaType.Unknown) { TimeSpan videoDuration; TimeSpan audioDuration; int startTickCunt = Environment.TickCount; using (FFMpegWrapper ffmpeg = new FFMpegWrapper(media.FullPath)) { videoDuration = ffmpeg.GetFrameCount().SMPTEFramesToTimeSpan(); audioDuration = (TimeSpan)ffmpeg.GetAudioDuration(); if (videoDuration == TimeSpan.Zero) { MediaInfoLib.MediaInfo mi = new MediaInfoLib.MediaInfo(); try { mi.Open(media.FullPath); long frameCount; if (long.TryParse(mi.Get(MediaInfoLib.StreamKind.Video, 0, "FrameCount"), out frameCount)) videoDuration = frameCount.SMPTEFramesToTimeSpan(); long audioMilliseconds; if (long.TryParse(mi.Get(MediaInfoLib.StreamKind.Audio, 0, "Duration"), out audioMilliseconds)) audioDuration = TimeSpan.FromMilliseconds(audioMilliseconds); //mi.Option("Complete"); //Debug.WriteLine(mi.Inform()); } finally { mi.Close(); } } media.Duration = videoDuration; if (media.DurationPlay == TimeSpan.Zero || media.DurationPlay > videoDuration) media.DurationPlay = videoDuration; int w = ffmpeg.GetWidth(); int h = ffmpeg.GetHeight(); FieldOrder order = ffmpeg.GetFieldOrder(); Rational frameRate = ffmpeg.GetFrameRate(); Rational sar = ffmpeg.GetSAR(); if (h == 608 && w == 720) { media.HasExtraLines = true; h = 576; } else media.HasExtraLines = false; RationalNumber sAR = ((sar.Num == 608 && sar.Den == 405) || (sar.Num == 1 && sar.Den == 1)) ? VideoFormatDescription.Descriptions[TVideoFormat.PAL_FHA].SAR : (sar.Num == 152 && sar.Den == 135) ? VideoFormatDescription.Descriptions[TVideoFormat.PAL].SAR : new RationalNumber(sar.Num, sar.Den); var vfd = VideoFormatDescription.Match(new System.Drawing.Size(w, h), new RationalNumber(frameRate.Num, frameRate.Den), sAR, order != FieldOrder.PROGRESSIVE); media.VideoFormat = vfd.Format; media.VideoFormatDescription = vfd; if (videoDuration > TimeSpan.Zero) { media.MediaType = TMediaType.Movie; if (Math.Abs(videoDuration.Ticks - audioDuration.Ticks) >= TimeSpan.TicksPerSecond / 2 && audioDuration != TimeSpan.Zero) // when more than 0.5 sec difference media.MediaStatus = TMediaStatus.ValidationError; else media.MediaStatus = TMediaStatus.Available; } else media.MediaStatus = TMediaStatus.ValidationError; } Debug.WriteLine("Check of {0} finished with status {1}. It took {2} milliseconds", media.FullPath, media.MediaStatus, Environment.TickCount - startTickCunt); } else media.MediaStatus = TMediaStatus.Available; }
internal static void Check(MediaBase media) { if (media.MediaType == TMediaType.Movie || media.MediaType == TMediaType.Unknown) { int startTickCunt = Environment.TickCount; using (FFMpegWrapper ffmpeg = new FFMpegWrapper(media.FullPath)) { Rational r = ffmpeg.GetFrameRate(); RationalNumber frameRate = new RationalNumber(r.Num, r.Den); var videoDuration = ffmpeg.GetFrameCount().SMPTEFramesToTimeSpan(frameRate); var audioDuration = (TimeSpan)ffmpeg.GetAudioDuration(); var mediaDuration = ((videoDuration > audioDuration) && (audioDuration > TimeSpan.Zero) ? audioDuration : videoDuration).Round(frameRate); media.Duration = mediaDuration; if (media.DurationPlay == TimeSpan.Zero || media.DurationPlay > mediaDuration) { media.DurationPlay = mediaDuration; } int w = ffmpeg.GetWidth(); int h = ffmpeg.GetHeight(); FieldOrder order = ffmpeg.GetFieldOrder(); Rational sar = ffmpeg.GetSAR(); if (h == 608 && w == 720) { media.HasExtraLines = true; h = 576; } else { media.HasExtraLines = false; } string timecode = ffmpeg.GetTimeCode(); if (timecode != null && timecode.IsValidSMPTETimecode(frameRate)) { media.TcStart = timecode.SMPTETimecodeToTimeSpan(frameRate); if (media.TcPlay < media.TcStart) { media.TcPlay = media.TcStart; } } RationalNumber sAR = (h == 576 && ((sar.Num == 608 && sar.Den == 405) || (sar.Num == 1 && sar.Den == 1) || (sar.Num == 118 && sar.Den == 81))) ? VideoFormatDescription.Descriptions[TVideoFormat.PAL_FHA].SAR : (sar.Num == 152 && sar.Den == 135) ? VideoFormatDescription.Descriptions[TVideoFormat.PAL].SAR : new RationalNumber(sar.Num, sar.Den); var vfd = VideoFormatDescription.Match(new System.Drawing.Size(w, h), new RationalNumber(frameRate.Num, frameRate.Den), sAR, order != FieldOrder.PROGRESSIVE); media.VideoFormat = vfd.Format; if (media is IngestMedia ingestMedia) { ingestMedia.StreamInfo = ffmpeg.GetStreamInfo(); } if (media is TempMedia tempMedia) { tempMedia.StreamInfo = ffmpeg.GetStreamInfo(); } Debug.WriteLine("FFmpeg check of {0} finished. It took {1} milliseconds", media.FullPath, Environment.TickCount - startTickCunt); if (videoDuration > TimeSpan.Zero) { media.MediaType = TMediaType.Movie; if (Math.Abs(videoDuration.Ticks - audioDuration.Ticks) >= TimeSpan.TicksPerSecond && audioDuration != TimeSpan.Zero) { // when more than 0.5 sec difference media.MediaStatus = TMediaStatus.ValidationError; } media.MediaStatus = TMediaStatus.Available; } media.MediaStatus = TMediaStatus.ValidationError; } } media.MediaStatus = TMediaStatus.Available; }
private void CheckInputFile(Media mf) { using (FFMpegWrapper ff = new FFMpegWrapper(mf.FullPath)) { inputFileStreams = ff.GetStreamInfo(); } MediaInfo mi = new MediaInfo(); try { mi.Open(mf.FullPath); mi.Option("Complete"); string miOutput = mi.Inform(); Regex format_lxf = new Regex("Format\\s*:\\s*LXF"); if (format_lxf.Match(miOutput).Success) { string[] miOutputLines = miOutput.Split('\n'); Regex vitc = new Regex("ATC_VITC"); Regex re = new Regex("Time code of first frame\\s*:[\\s]\\d{2}:\\d{2}:\\d{2}:\\d{2}"); for (int i = 0; i < miOutputLines.Length; i++) { if (vitc.Match(miOutputLines[i]).Success && i >= 1) { Match m_tcs = re.Match(miOutputLines[i - 1]); if (m_tcs.Success) { Regex reg_tc = new Regex("\\d{2}:\\d{2}:\\d{2}:\\d{2}"); Match m_tc = reg_tc.Match(m_tcs.Value); if (m_tc.Success) { DestMedia.TCStart = reg_tc.Match(m_tc.Value).Value.SMPTETimecodeToTimeSpan(); if (DestMedia.TCPlay == TimeSpan.Zero) DestMedia.TCPlay = DestMedia.TCStart; break; } } } } } Regex format_mxf = new Regex(@"Format\s*:\s*MXF"); if (format_mxf.Match(miOutput).Success) { string[] miOutputLines = miOutput.Split('\n'); Regex mxf_tc = new Regex(@"MXF TC"); Regex re = new Regex(@"Time code of first frame\s*:[\s]\d{2}:\d{2}:\d{2}:\d{2}"); for (int i = 0; i < miOutputLines.Length; i++) { Match mxf_match = mxf_tc.Match(miOutputLines[i]); if (mxf_match.Success && i < miOutputLines.Length - 1) { Regex reg_tc = new Regex(@"\d{2}:\d{2}:\d{2}:\d{2}"); Match m_tc = re.Match(miOutputLines[i + 1]); if (m_tc.Success) { DestMedia.TCStart = reg_tc.Match(m_tc.Value).Value.SMPTETimecodeToTimeSpan(); if (DestMedia.TCPlay == TimeSpan.Zero) DestMedia.TCPlay = DestMedia.TCStart; break; } } } } } finally { mi.Close(); } }