diff --git a/internal/config/config_ffmpeg.go b/internal/config/config_ffmpeg.go index 23aa40dad..5af5c3bde 100644 --- a/internal/config/config_ffmpeg.go +++ b/internal/config/config_ffmpeg.go @@ -9,7 +9,7 @@ import ( // FFmpegBin returns the ffmpeg executable file name. func (c *Config) FFmpegBin() string { - return findBin(c.options.FFmpegBin, "ffmpeg") + return findBin(c.options.FFmpegBin, ffmpeg.DefaultBin) } // FFmpegEnabled checks if FFmpeg is enabled for video transcoding. diff --git a/internal/config/flags.go b/internal/config/flags.go index c29911c45..9342b9a5b 100644 --- a/internal/config/flags.go +++ b/internal/config/flags.go @@ -578,7 +578,7 @@ var Flags = CliFlags{ Flag: cli.StringFlag{ Name: "ffmpeg-bin", Usage: "FFmpeg `COMMAND` for video transcoding and thumbnail extraction", - Value: "ffmpeg", + Value: ffmpeg.DefaultBin, EnvVar: EnvVar("FFMPEG_BIN"), }}, { Flag: cli.StringFlag{ diff --git a/internal/ffmpeg/convert.go b/internal/ffmpeg/convert.go index 01fd54da0..e90417940 100644 --- a/internal/ffmpeg/convert.go +++ b/internal/ffmpeg/convert.go @@ -18,10 +18,18 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, // Don't transcode more than one video at the same time. useMutex = true + // Get configured ffmpeg command name. + ffmpeg := opt.Bin + + // Use default ffmpeg command name? + if ffmpeg == "" { + ffmpeg = DefaultBin + } + // Don't use hardware transcoding for animated images. if fs.TypeAnimated[fs.FileType(fileName)] != "" { result = exec.Command( - opt.Bin, + ffmpeg, "-i", fileName, "-movflags", "faststart", "-pix_fmt", FormatYUV420P.String(), @@ -43,7 +51,7 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, case IntelEncoder: // ffmpeg -hide_banner -h encoder=h264_qsv result = exec.Command( - opt.Bin, + ffmpeg, "-qsv_device", "/dev/dri/renderD128", "-i", fileName, "-c:a", "aac", @@ -51,7 +59,6 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, "-c:v", opt.Encoder.String(), "-map", opt.MapVideo, "-map", opt.MapAudio, - "-vsync", "vfr", "-r", "30", "-b:v", opt.Bitrate, "-bitrate", opt.Bitrate, @@ -63,7 +70,7 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, case AppleEncoder: // ffmpeg -hide_banner -h encoder=h264_videotoolbox result = exec.Command( - opt.Bin, + ffmpeg, "-i", fileName, "-c:v", opt.Encoder.String(), "-map", opt.MapVideo, @@ -72,7 +79,6 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, "-vf", opt.VideoFilter(FormatYUV420P), "-profile", "high", "-level", "51", - "-vsync", "vfr", "-r", "30", "-b:v", opt.Bitrate, "-f", "mp4", @@ -82,7 +88,7 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, case VAAPIEncoder: result = exec.Command( - opt.Bin, + ffmpeg, "-hwaccel", "vaapi", "-i", fileName, "-c:a", "aac", @@ -90,7 +96,6 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, "-c:v", opt.Encoder.String(), "-map", opt.MapVideo, "-map", opt.MapAudio, - "-vsync", "vfr", "-r", "30", "-b:v", opt.Bitrate, "-f", "mp4", @@ -101,7 +106,7 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, case NvidiaEncoder: // ffmpeg -hide_banner -h encoder=h264_nvenc result = exec.Command( - opt.Bin, + ffmpeg, "-hwaccel", "auto", "-i", fileName, "-pix_fmt", FormatYUV420P.String(), @@ -129,7 +134,7 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, case Video4LinuxEncoder: // ffmpeg -hide_banner -h encoder=h264_v4l2m2m result = exec.Command( - opt.Bin, + ffmpeg, "-i", fileName, "-c:v", opt.Encoder.String(), "-map", opt.MapVideo, @@ -140,7 +145,6 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, "-num_capture_buffers", "64", "-max_muxing_queue_size", "1024", "-crf", "23", - "-vsync", "vfr", "-r", "30", "-b:v", opt.Bitrate, "-f", "mp4", @@ -150,7 +154,7 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, default: result = exec.Command( - opt.Bin, + ffmpeg, "-i", fileName, "-c:v", opt.Encoder.String(), "-map", opt.MapVideo, @@ -159,7 +163,6 @@ func AvcConvertCommand(fileName, avcName string, opt Options) (result *exec.Cmd, "-vf", opt.VideoFilter(FormatYUV420P), "-max_muxing_queue_size", "1024", "-crf", "23", - "-vsync", "vfr", "-r", "30", "-b:v", opt.Bitrate, "-f", "mp4", diff --git a/internal/ffmpeg/convert_test.go b/internal/ffmpeg/convert_test.go index 50b7b3c05..28fd87358 100644 --- a/internal/ffmpeg/convert_test.go +++ b/internal/ffmpeg/convert_test.go @@ -13,8 +13,8 @@ func TestAvcConvertCommand(t *testing.T) { Encoder: "intel", Size: 1500, Bitrate: "50", - MapVideo: "", - MapAudio: "", + MapVideo: MapVideoDefault, + MapAudio: MapAudioDefault, } _, _, err := AvcConvertCommand("", "", Options) @@ -26,8 +26,8 @@ func TestAvcConvertCommand(t *testing.T) { Encoder: "intel", Size: 1500, Bitrate: "50", - MapVideo: "", - MapAudio: "", + MapVideo: MapVideoDefault, + MapAudio: MapAudioDefault, } _, _, err := AvcConvertCommand("VID123.mov", "", Options) @@ -39,15 +39,15 @@ func TestAvcConvertCommand(t *testing.T) { Encoder: "intel", Size: 1500, Bitrate: "50", - MapVideo: "", - MapAudio: "", + MapVideo: MapVideoDefault, + MapAudio: MapAudioDefault, } r, _, err := AvcConvertCommand("VID123.gif", "VID123.gif.avc", Options) if err != nil { t.Fatal(err) } - assert.Equal(t, " -i VID123.gif -movflags faststart -pix_fmt yuv420p -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 -f mp4 -y VID123.gif.avc", r.String()) + assert.Equal(t, "/usr/bin/ffmpeg -i VID123.gif -movflags faststart -pix_fmt yuv420p -vf scale=trunc(iw/2)*2:trunc(ih/2)*2 -f mp4 -y VID123.gif.avc", r.String()) }) t.Run("libx264", func(t *testing.T) { Options := Options{ @@ -55,15 +55,15 @@ func TestAvcConvertCommand(t *testing.T) { Encoder: "libx264", Size: 1500, Bitrate: "50", - MapVideo: "", - MapAudio: "", + MapVideo: MapVideoDefault, + MapAudio: MapAudioDefault, } r, _, err := AvcConvertCommand("VID123.mov", "VID123.mov.avc", Options) if err != nil { t.Fatal(err) } - assert.Equal(t, " -i VID123.mov -c:v libx264 -map -map -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=yuv420p -max_muxing_queue_size 1024 -crf 23 -vsync vfr -r 30 -b:v 50 -f mp4 -y VID123.mov.avc", r.String()) + assert.Equal(t, "/usr/bin/ffmpeg -i VID123.mov -c:v libx264 -map 0:v:0 -map 0:a:0? -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=yuv420p -max_muxing_queue_size 1024 -crf 23 -r 30 -b:v 50 -f mp4 -y VID123.mov.avc", r.String()) }) t.Run("h264_qsv", func(t *testing.T) { Options := Options{ @@ -71,15 +71,15 @@ func TestAvcConvertCommand(t *testing.T) { Encoder: "h264_qsv", Size: 1500, Bitrate: "50", - MapVideo: "", - MapAudio: "", + MapVideo: MapVideoDefault, + MapAudio: MapAudioDefault, } r, _, err := AvcConvertCommand("VID123.mov", "VID123.mov.avc", Options) if err != nil { t.Fatal(err) } - assert.Equal(t, " -qsv_device /dev/dri/renderD128 -i VID123.mov -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=rgb32 -c:v h264_qsv -map -map -vsync vfr -r 30 -b:v 50 -bitrate 50 -f mp4 -y VID123.mov.avc", r.String()) + assert.Equal(t, "/usr/bin/ffmpeg -qsv_device /dev/dri/renderD128 -i VID123.mov -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=rgb32 -c:v h264_qsv -map 0:v:0 -map 0:a:0? -r 30 -b:v 50 -bitrate 50 -f mp4 -y VID123.mov.avc", r.String()) }) t.Run("h264_videotoolbox", func(t *testing.T) { Options := Options{ @@ -87,15 +87,15 @@ func TestAvcConvertCommand(t *testing.T) { Encoder: "h264_videotoolbox", Size: 1500, Bitrate: "50", - MapVideo: "", - MapAudio: "", + MapVideo: MapVideoDefault, + MapAudio: MapAudioDefault, } r, _, err := AvcConvertCommand("VID123.mov", "VID123.mov.avc", Options) if err != nil { t.Fatal(err) } - assert.Equal(t, " -i VID123.mov -c:v h264_videotoolbox -map -map -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=yuv420p -profile high -level 51 -vsync vfr -r 30 -b:v 50 -f mp4 -y VID123.mov.avc", r.String()) + assert.Equal(t, "/usr/bin/ffmpeg -i VID123.mov -c:v h264_videotoolbox -map 0:v:0 -map 0:a:0? -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=yuv420p -profile high -level 51 -r 30 -b:v 50 -f mp4 -y VID123.mov.avc", r.String()) }) t.Run("h264_vaapi", func(t *testing.T) { Options := Options{ @@ -103,15 +103,15 @@ func TestAvcConvertCommand(t *testing.T) { Encoder: "h264_vaapi", Size: 1500, Bitrate: "50", - MapVideo: "", - MapAudio: "", + MapVideo: MapVideoDefault, + MapAudio: MapAudioDefault, } r, _, err := AvcConvertCommand("VID123.mov", "VID123.mov.avc", Options) if err != nil { t.Fatal(err) } - assert.Equal(t, " -hwaccel vaapi -i VID123.mov -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=nv12,hwupload -c:v h264_vaapi -map -map -vsync vfr -r 30 -b:v 50 -f mp4 -y VID123.mov.avc", r.String()) + assert.Equal(t, "/usr/bin/ffmpeg -hwaccel vaapi -i VID123.mov -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=nv12,hwupload -c:v h264_vaapi -map 0:v:0 -map 0:a:0? -r 30 -b:v 50 -f mp4 -y VID123.mov.avc", r.String()) }) t.Run("h264_nvenc", func(t *testing.T) { Options := Options{ @@ -119,15 +119,15 @@ func TestAvcConvertCommand(t *testing.T) { Encoder: "h264_nvenc", Size: 1500, Bitrate: "50", - MapVideo: "", - MapAudio: "", + MapVideo: MapVideoDefault, + MapAudio: MapAudioDefault, } r, _, err := AvcConvertCommand("VID123.mov", "VID123.mov.avc", Options) if err != nil { t.Fatal(err) } - assert.Equal(t, " -hwaccel auto -i VID123.mov -pix_fmt yuv420p -c:v h264_nvenc -map -map -c:a aac -preset 15 -pixel_format yuv420p -gpu any -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=yuv420p -rc:v constqp -cq 0 -tune 2 -r 30 -b:v 50 -profile:v 1 -level:v auto -coder:v 1 -f mp4 -y VID123.mov.avc", r.String()) + assert.Equal(t, "/usr/bin/ffmpeg -hwaccel auto -i VID123.mov -pix_fmt yuv420p -c:v h264_nvenc -map 0:v:0 -map 0:a:0? -c:a aac -preset 15 -pixel_format yuv420p -gpu any -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=yuv420p -rc:v constqp -cq 0 -tune 2 -r 30 -b:v 50 -profile:v 1 -level:v auto -coder:v 1 -f mp4 -y VID123.mov.avc", r.String()) }) t.Run("h264_v4l2m2m", func(t *testing.T) { Options := Options{ @@ -135,14 +135,14 @@ func TestAvcConvertCommand(t *testing.T) { Encoder: "h264_v4l2m2m", Size: 1500, Bitrate: "50", - MapVideo: "", - MapAudio: "", + MapVideo: MapVideoDefault, + MapAudio: MapAudioDefault, } r, _, err := AvcConvertCommand("VID123.mov", "VID123.mov.avc", Options) if err != nil { t.Fatal(err) } - assert.Equal(t, " -i VID123.mov -c:v h264_v4l2m2m -map -map -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=yuv420p -num_output_buffers 72 -num_capture_buffers 64 -max_muxing_queue_size 1024 -crf 23 -vsync vfr -r 30 -b:v 50 -f mp4 -y VID123.mov.avc", r.String()) + assert.Equal(t, "/usr/bin/ffmpeg -i VID123.mov -c:v h264_v4l2m2m -map 0:v:0 -map 0:a:0? -c:a aac -vf scale='if(gte(iw,ih), min(1500, iw), -2):if(gte(iw,ih), -2, min(1500, ih))',format=yuv420p -num_output_buffers 72 -num_capture_buffers 64 -max_muxing_queue_size 1024 -crf 23 -r 30 -b:v 50 -f mp4 -y VID123.mov.avc", r.String()) }) } diff --git a/internal/ffmpeg/defaults.go b/internal/ffmpeg/defaults.go index e8f056307..9d2133869 100644 --- a/internal/ffmpeg/defaults.go +++ b/internal/ffmpeg/defaults.go @@ -1,6 +1,7 @@ package ffmpeg const ( + DefaultBin = "ffmpeg" MapVideoDefault = "0:v:0" MapAudioDefault = "0:a:0?" )