From 4083064ccbc6c135fbab09a1eccdca1ceec13f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Dos=C3=A9?= Date: Fri, 15 Mar 2024 21:18:13 -0700 Subject: [PATCH] Fix MP4 Copy processor and add more logging to processors (#814) --- lib/ambry/media/chapters/utils.ex | 6 ++ lib/ambry/media/processor/mp3.ex | 28 +++--- lib/ambry/media/processor/mp4_concat.ex | 40 ++++----- lib/ambry/media/processor/mp4_copy.ex | 29 +++--- lib/ambry/media/processor/mp4_re_encode.ex | 28 +++--- lib/ambry/media/processor/shared.ex | 100 +++++++++++---------- 6 files changed, 121 insertions(+), 110 deletions(-) diff --git a/lib/ambry/media/chapters/utils.ex b/lib/ambry/media/chapters/utils.ex index 39874404..a2d31e4b 100644 --- a/lib/ambry/media/chapters/utils.ex +++ b/lib/ambry/media/chapters/utils.ex @@ -65,6 +65,8 @@ defmodule Ambry.Media.Chapters.Utils do "quiet" ] + Logger.info(fn -> "Running `#{command} #{Enum.join(args, " ")}`" end) + case System.cmd(command, args, cd: Media.source_path(media), parallelism: true) do {output, 0} -> {:ok, output} @@ -94,6 +96,8 @@ defmodule Ambry.Media.Chapters.Utils do ] try do + Logger.info(fn -> "Running `#{command} #{Enum.join(args, " ")}`" end) + {output, 0} = System.cmd(command, args, cd: Media.source_path(media), @@ -143,6 +147,8 @@ defmodule Ambry.Media.Chapters.Utils do command = "ffprobe" args = ["-i", mp4_file, "-print_format", "json", "-show_chapters", "-loglevel", "error"] + Logger.info(fn -> "Running `#{command} #{Enum.join(args, " ")}`" end) + case System.cmd(command, args, cd: Media.source_path(media), parallelism: true) do {output, 0} -> {:ok, output} diff --git a/lib/ambry/media/processor/mp3.ex b/lib/ambry/media/processor/mp3.ex index d24ca1b1..c755e7bb 100644 --- a/lib/ambry/media/processor/mp3.ex +++ b/lib/ambry/media/processor/mp3.ex @@ -40,20 +40,20 @@ defmodule Ambry.Media.Processor.MP3 do {:ok, _progress_tracker} = ProgressTracker.start(media, progress_file_path, @extensions) - command = "ffmpeg" - - args = [ - "-loglevel", - "quiet", - "-vn", - "-i", - "../#{mp3_file}", - "-progress", - progress_file_path, - "#{id}.mp4" - ] - - {_output, 0} = System.cmd(command, args, cd: Media.out_path(media), parallelism: true) + run_command!( + "ffmpeg", + [ + "-loglevel", + "quiet", + "-vn", + "-i", + "../#{mp3_file}", + "-progress", + progress_file_path, + "#{id}.mp4" + ], + cd: Media.out_path(media) + ) id end diff --git a/lib/ambry/media/processor/mp4_concat.ex b/lib/ambry/media/processor/mp4_concat.ex index a701e0dc..d0b513b6 100644 --- a/lib/ambry/media/processor/mp4_concat.ex +++ b/lib/ambry/media/processor/mp4_concat.ex @@ -41,26 +41,26 @@ defmodule Ambry.Media.Processor.MP4Concat do {:ok, _progress_tracker} = ProgressTracker.start(media, progress_file_path, @extensions) - command = "ffmpeg" - - args = [ - "-loglevel", - "quiet", - "-f", - "concat", - "-safe", - "0", - "-vn", - "-acodec", - "copy", - "-i", - "files.txt", - "-progress", - progress_file_path, - "#{id}.mp4" - ] - - {_output, 0} = System.cmd(command, args, cd: Media.out_path(media), parallelism: true) + run_command!( + "ffmpeg", + [ + "-loglevel", + "quiet", + "-f", + "concat", + "-safe", + "0", + "-vn", + "-acodec", + "copy", + "-i", + "files.txt", + "-progress", + progress_file_path, + "#{id}.mp4" + ], + cd: Media.out_path(media) + ) id end diff --git a/lib/ambry/media/processor/mp4_copy.ex b/lib/ambry/media/processor/mp4_copy.ex index 7eab7134..48d8a5a5 100644 --- a/lib/ambry/media/processor/mp4_copy.ex +++ b/lib/ambry/media/processor/mp4_copy.ex @@ -41,21 +41,20 @@ defmodule Ambry.Media.Processor.MP4Copy do {:ok, _progress_tracker} = ProgressTracker.start(media, progress_file_path, @extensions) - command = "ffmpeg" - - args = [ - "-loglevel", - "quiet", - "-vn", - "-i", - "../#{mp4_file}", - "-c copy", - "-progress", - progress_file_path, - "#{id}.mp4" - ] - - {_output, 0} = System.cmd(command, args, cd: Media.out_path(media), parallelism: true) + run_command!( + "ffmpeg", + [ + "-vn", + "-i", + "../#{mp4_file}", + "-c", + "copy", + "-progress", + progress_file_path, + "#{id}.mp4" + ], + cd: Media.out_path(media) + ) id end diff --git a/lib/ambry/media/processor/mp4_re_encode.ex b/lib/ambry/media/processor/mp4_re_encode.ex index 27883d15..0c3671c8 100644 --- a/lib/ambry/media/processor/mp4_re_encode.ex +++ b/lib/ambry/media/processor/mp4_re_encode.ex @@ -41,20 +41,20 @@ defmodule Ambry.Media.Processor.MP4ReEncode do {:ok, _progress_tracker} = ProgressTracker.start(media, progress_file_path, @extensions) - command = "ffmpeg" - - args = [ - "-loglevel", - "quiet", - "-vn", - "-i", - "../#{mp4_file}", - "-progress", - progress_file_path, - "#{id}.mp4" - ] - - {_output, 0} = System.cmd(command, args, cd: Media.out_path(media), parallelism: true) + run_command!( + "ffmpeg", + [ + "-loglevel", + "quiet", + "-vn", + "-i", + "../#{mp4_file}", + "-progress", + progress_file_path, + "#{id}.mp4" + ], + cd: Media.out_path(media) + ) id end diff --git a/lib/ambry/media/processor/shared.ex b/lib/ambry/media/processor/shared.ex index d38916a1..770a1224 100644 --- a/lib/ambry/media/processor/shared.ex +++ b/lib/ambry/media/processor/shared.ex @@ -9,6 +9,8 @@ defmodule Ambry.Media.Processor.Shared do alias Ambry.Media.Media alias Ambry.Media.Processor.ProgressTracker + require Logger + def filter_filenames(filenames, extensions) do filenames |> Enum.filter(&(Path.extname(&1) in extensions)) @@ -44,45 +46,45 @@ defmodule Ambry.Media.Processor.Shared do {:ok, _progress_tracker} = ProgressTracker.start(media, progress_file_path, extensions) - command = "ffmpeg" - - args = [ - "-loglevel", - "quiet", - "-f", - "concat", - "-safe", - "0", - "-vn", - "-i", - "files.txt", - "-progress", - progress_file_path, - "#{id}.mp4" - ] - - {_output, 0} = System.cmd(command, args, cd: Media.out_path(media), parallelism: true) + run_command!( + "ffmpeg", + [ + "-loglevel", + "quiet", + "-f", + "concat", + "-safe", + "0", + "-vn", + "-i", + "files.txt", + "-progress", + progress_file_path, + "#{id}.mp4" + ], + cd: Media.out_path(media) + ) id end def create_stream!(media, id) do - command = "shaka-packager" - - args = [ - "in=#{id}.mp4,stream=audio,out=#{id}.mp4,playlist_name=#{id}_0.m3u8", - "--base_urls", - "/uploads/media/", - "--hls_base_url", - "/uploads/media/", - "--mpd_output", - "#{id}.mpd", - "--hls_master_playlist_output", - "#{id}.m3u8", - "-quiet" - ] - - {_output, 0} = System.cmd(command, args, cd: Media.out_path(media), parallelism: true) + run_command!( + "shaka-packager", + [ + "in=#{id}.mp4,stream=audio,out=#{id}.mp4,playlist_name=#{id}_0.m3u8", + "--base_urls", + "/uploads/media/", + "--hls_base_url", + "/uploads/media/", + "--mpd_output", + "#{id}.mpd", + "--hls_master_playlist_output", + "#{id}.m3u8", + "-quiet" + ], + cd: Media.out_path(media) + ) end def finalize!(media, id) do @@ -116,22 +118,26 @@ defmodule Ambry.Media.Processor.Shared do # produce. But to get duration from unknown source files, we should not rely # on the metadata reported duration. - command = "ffprobe" + output = + run_command!("ffprobe", [ + "-i", + file, + "-print_format", + "json", + "-show_entries", + "format=duration", + "-v", + "quiet" + ]) - args = [ - "-i", - file, - "-print_format", - "json", - "-show_entries", - "format=duration", - "-v", - "quiet" - ] - - {output, 0} = System.cmd(command, args, parallelism: true) %{"format" => %{"duration" => duration_string}} = Jason.decode!(output) Decimal.new(duration_string) end + + def run_command!(command, args, opts \\ []) do + Logger.info(fn -> "Running `#{command} #{Enum.join(args, " ")}`" end) + {output, 0} = System.cmd(command, args, Keyword.put(opts, :parallelism, true)) + output + end end