Transcoding video files for playback in a browser
August 06, 2022 [Tech]ffmpeg -i original.mkv -c:v libx264 -c:a aac -ac 2 -ab 384000 -ar 48000 new.mp4
(Short answer: use the above ffmpeg command line. Read on for how I did this in Tdarr.)
I recently discovered Jellyfin, which gives me a Netflix-like UI for viewing my own videos, and seems great.
The only problem I had was that some videos were in formats that can't be played natively in a web browser. Jellyfin heroically tries to transcode them on the fly, but my server is very lightweight, and there's no way it can do that.
So, I needed to transcode those videos to a more suitable format.
Tdarr allows transcoding large numbers of files, and with a little head-scratching I worked out how to get it running, but I still needed the right ffmpeg options to make the videos work well in Firefox, without needing transcoding of video, audio or the container.
Here are the Tdarr settings that I found worked well:
Output file container: .mp4 Encoder: ffmpeg CLI arguments:-c:v libx264 -c:a aac -ac 2 -ab 384000 -ar 48000 Only transcode videos in these codecs: hevc
Explanation:
- Output file container: .mp4 - wraps the video up in an MP4 container - surprisingly, Firefox doesn't seem to support MKV.
- -c:v libx264 - re-encodes the video as H.264. Firefox can't do H.265, and H.264 is reasonably compatible with lots of browsers. If you don't care about Safari or various Microsoft browsers, you might want to think about VP9 as it's natively supported on Firefox, so should work on weird architectures etc.
- -c:a aac -ac 2 -ab 384000 -ar 48000 - re-encodes the audio as AAC with the right bitrates etc. Jellyfin was still transcoding the audio when I just specified -c:a aac, and it took me a while to work out that you need those other options too.
- Only transcode videos in these codecs: hevc - hevc means H.265 encoding, and the videos I had problems with were in that encoding, but you might have different problems. If in doubt, you can choose "Don't transcode videos in these codecs:" and uncheck all the encodings, meaning all your videos will be re-encoded.
If you are not using Tdarr, here is the plain command line to use with ffmpeg:
ffmpeg -i original.mkv -c:v libx264 -c:a aac -ac 2 -ab 384000 -ar 48000 new.mp4