Skip to content

Commit

Permalink
Fix MP4 Copy processor and add more logging to processors (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
doughsay authored Mar 16, 2024
1 parent 577c7b4 commit 4083064
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 110 deletions.
6 changes: 6 additions & 0 deletions lib/ambry/media/chapters/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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}
Expand Down
28 changes: 14 additions & 14 deletions lib/ambry/media/processor/mp3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions lib/ambry/media/processor/mp4_concat.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 14 additions & 15 deletions lib/ambry/media/processor/mp4_copy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions lib/ambry/media/processor/mp4_re_encode.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
100 changes: 53 additions & 47 deletions lib/ambry/media/processor/shared.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 4083064

Please sign in to comment.