Parallel transcoding with FFmpeg on M1 Mac

I’m trying to determine how effective an M1 Mac would be for transcoding video using FFmpeg (specifically resizing and adjusting bitrate). I can do single transcodes using a command like this:

ffmpeg -I in.mp4 -nostdin -c:v h264_videotoolbox -c:a copy -vf scale=1280:720 -b:v 8000k out.mp4

Now, when I run this, I can see the process in Activity Monitor, but it shows significant CPU use but 0% GPU use, although it’s certainly using some form of hardware acceleration (using libx264 instead of h264_videotoolbox is much slower).

When I try running multiple processes concurrently, the timings suggest little evidence of parallel execution:

Test Timing
convert single test file 6.8s
convert file 5 times sequentially 33.6s
convert file 5 times in parallel 31.5s

Since the M1 chip is supposed to have 7 or 8 GPUs inside it, I expected to see quite good parallelism, so are there options which I’m missing which would:

  1. ensure that the transcoding is actually running on the GPU?
  2. allow parallel execution across the multiple GPUs?

Answer

Here is what I found on the topic:

For starters, here’s a pretty good answer right here on superuser.com relating to how you can increase your ffmpeg threading/speed:

[1] https://superuser.com/a/792609/1211883

Apple M1 7-Core GPU

Next, here is probably the best resources I found on the topic for you and your M1 Mac at least:

[2] https://superuser.com/a/1324326/1211883 ⭐️⭐️⭐️⭐️

[3] https://trac.ffmpeg.org/wiki/HWAccelIntro#VideoToolbox ⭐️⭐️⭐️⭐️⭐️

[4] https://doesitarm.com/tv/running-ffmpeg-on-mac-with-apple-silicon-m1-i-woz7p7zmz2s/ ⭐️⭐️⭐️⭐️

[5] https://github.com/cdgriffith/FastFlix/issues/196 ⭐️⭐️⭐️

To summarize,

On macOPS there’s videotoolbox.

Check this out for the encoder options:

ffmpeg -hide_banner -h encoder=hevc_videotoolbox

Essentially you’ve got to use the encoder=hevc_videotoolbox hardware encoder.

Nvidia GPU

And here are probably the best resources I found relating to nvidia gpus, just incase someone has one of those instead:

[6] https://developer.nvidia.com/blog/nvidia-ffmpeg-transcoding-guide/

It states that:

“Activating support for hardware acceleration when building [ffmpeg] from source requires some extra steps:”

Which you’ll probably want to do what it says there and add support for hardware acceleration onto your ffmpeg installation.

Then under the “Multi-GPU” heading within [6] it states the following:

Multi-GPU
Encoding and decoding work must be explicitly assigned to a GPU when using multiple GPUs in one system. GPUs are identified by their index number; by default all work is performed on the GPU with index 0. Use the following command to obtain a list of all NVIDIA GPUs in the system and their corresponding ID numbers:

ffmpeg -vsync 0 -i input.mp4 -c:v h264_nvenc -gpu list -f null –

Once you know the index, the -hwaccel_device index flag can be used to set the active GPU for decoding and encoding. In the example below the work will be executed on the gpu with index 1.

ffmpeg -vsync 0 -hwaccel cuvid -hwaccel_device 1 -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:a copy -c:v h264_nvenc -b:v 5M output.mp4

Other Helpful Resources

Lastly, the ffmpeg docs themselves probably wouldn’t hurt to check out:

[7] https://ffmpeg.org/ffmpeg.html

Again, this resource is awesome showing all the different ffmpeg platform availabilities and ffmpeg gpu options by platform:

[8] https://trac.ffmpeg.org/wiki/HWAccelIntro

I’m trying to level up so I can actually vote on some posts going forward, so I hope this helps!

But yea, trying to transcode video in a timely manner is always fun; 🤞😉 I do wish you the best of luck in all your endeavors!! 😂

Attribution
Source : Link , Question Author : Pushkar , Answer Author : Chase Gibbons

Leave a Comment