From ae248b045a923449c9f957c0205ef387ad8a4047 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Wed, 26 Mar 2014 17:05:31 -0400 Subject: use ffprobe -show_chapters command --- .../MediaEncoder/MediaEncoder.cs | 90 ++-------------------- 1 file changed, 6 insertions(+), 84 deletions(-) (limited to 'MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs') diff --git a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs index fddba7662..c646d80bc 100644 --- a/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs +++ b/MediaBrowser.Server.Implementations/MediaEncoder/MediaEncoder.cs @@ -175,6 +175,10 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder string probeSizeArgument, CancellationToken cancellationToken) { + var args = extractChapters + ? "{0} -i {1} -threads 0 -v info -print_format json -show_streams -show_chapters -show_format" + : "{0} -i {1} -threads 0 -v info -print_format json -show_streams -show_format"; + var process = new Process { StartInfo = new ProcessStartInfo @@ -186,8 +190,7 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder RedirectStandardOutput = true, RedirectStandardError = true, FileName = FFProbePath, - Arguments = string.Format( - "{0} -i {1} -threads 0 -v info -print_format json -show_streams -show_format", + Arguments = string.Format(args, probeSizeArgument, inputPath).Trim(), WindowStyle = ProcessWindowStyle.Hidden, @@ -204,7 +207,6 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder await _ffProbeResourcePool.WaitAsync(cancellationToken).ConfigureAwait(false); InternalMediaInfoResult result; - string standardError = null; try { @@ -221,24 +223,9 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder try { - Task standardErrorReadTask = null; - - // MUST read both stdout and stderr asynchronously or a deadlock may occurr - if (extractChapters) - { - standardErrorReadTask = process.StandardError.ReadToEndAsync(); - } - else - { - process.BeginErrorReadLine(); - } + process.BeginErrorReadLine(); result = _jsonSerializer.DeserializeFromStream(process.StandardOutput.BaseStream); - - if (extractChapters) - { - standardError = await standardErrorReadTask.ConfigureAwait(false); - } } catch { @@ -282,11 +269,6 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder } } - if (extractChapters && !string.IsNullOrEmpty(standardError)) - { - AddChapters(result, standardError); - } - return result; } @@ -295,66 +277,6 @@ namespace MediaBrowser.Server.Implementations.MediaEncoder /// protected readonly CultureInfo UsCulture = new CultureInfo("en-US"); - /// - /// Adds the chapters. - /// - /// The result. - /// The standard error. - private void AddChapters(InternalMediaInfoResult result, string standardError) - { - var lines = standardError.Split('\n').Select(l => l.TrimStart()); - - var chapters = new List(); - - ChapterInfo lastChapter = null; - - foreach (var line in lines) - { - if (line.StartsWith("Chapter", StringComparison.OrdinalIgnoreCase)) - { - // Example: - // Chapter #0.2: start 400.534, end 4565.435 - const string srch = "start "; - var start = line.IndexOf(srch, StringComparison.OrdinalIgnoreCase); - - if (start == -1) - { - continue; - } - - var subString = line.Substring(start + srch.Length); - subString = subString.Substring(0, subString.IndexOf(',')); - - double seconds; - - if (double.TryParse(subString, NumberStyles.Any, UsCulture, out seconds)) - { - lastChapter = new ChapterInfo - { - StartPositionTicks = TimeSpan.FromSeconds(seconds).Ticks - }; - - chapters.Add(lastChapter); - } - } - - else if (line.StartsWith("title", StringComparison.OrdinalIgnoreCase)) - { - if (lastChapter != null && string.IsNullOrEmpty(lastChapter.Name)) - { - var index = line.IndexOf(':'); - - if (index != -1) - { - lastChapter.Name = line.Substring(index + 1).Trim().TrimEnd('\r'); - } - } - } - } - - result.Chapters = chapters; - } - /// /// Processes the exited. /// -- cgit v1.2.3