←back to thread

72 points indulona | 2 comments | | HN request time: 0.001s | source

I am working on a website that has video hosting capability. Users can upload video files and i will generate multiple versions with different qualities or just audio, thumbnails and things like that.

I have chosen the mp4 container because of how widely supported it is. To prevent users having to fetch whole files, i use the fast start option, where the container's metadata is written at the beginning of the file, instead of at the end.

Next, I have picked h264 codec because of how widely supported it is. VP8/VP9/AV1/x265/x266 are certainly better but the h264 software encoding is often beating hardware encoding due to highly optimized and time-proven code and supported hardware. And the uploaded videos are already compressed, users won't be uploading some 8k raw videos where most advanced codes would be useful for preserving "quality".

For audio, i have picked opus codec. Seems like good value over others. Not much else to add.

I run the ffmpeg to convert video with command like this:

ffmpeg -hide_banner -loglevel error -i input.mp4 -g 52 -c:v h264 -maxrate:v vbr -bufsize vbr -s HxW -c:a libopus -af aformat=channel_layouts=7.1|5.1|stereo -maxrate:a abr -ar 48000 -ac 2 -f mp4 -movflags faststart -map 0:v:0 -map 0:a:0 output.mp4

where vbr is video bitrate like 1024k(1mbps), abr is audio bitrate like 190k and HxW is video dimensions in case of resizing.

I wonder how are folks that handle video encoding process and generate their videos?

How did you pick your settings, what issues have you encountered and any tips you can share are certainly appreciated.

Quite a niche segment when it comes to operations and not being merely consumer/customer.

Show context
Am4TIfIsER0ppos ◴[] No.41029268[source]
For synchronized group watching I typically encode to h264 and aac in mp4. Video: render subtitles if available, downscale to 720p if larger, ensure yuv420p, encode with: preset slow, crf 24, and tune animation or film as appropriate. Copy audio if aac, 2 channel, and bitrate is less than 160k otherwise: force 2 channel, encode at 128k. Fast start is needed otherwise I'd use a separate program to put the "header" at the start.

As for your command line, what do you think -g 52 does? Why do you give conflicting audio channel settings?

replies(2): >>41031390 #>>41055795 #
indulona ◴[] No.41031390[source]
-g 52 = at the latest every 52nd frame will be a key-frame(so little over 2 seconds at 24 fps), this influences seeking(how long an image might have artifacts before it renders properly). there are no two conflicting audio channels settings. the -ar is audio sampling rate(data points per second) whereas -maxrate:a is bitrate(actual bits of data per second). the yuv is good point, i guess i is a safe-guard for exotic input. also, copying the audio might be worth it, although then i would end up with mixed output files where one might be original aac and another might be opus. so i think i prefer the uniform output over some saving in processing.
replies(1): >>41032224 #
1. Am4TIfIsER0ppos ◴[] No.41032224[source]
Okay there is some thought put into your choice of keyframe interval.

And there is a conflict. You allow the aformat filter to negotiate for 5.1, 7.1, or stereo but then insist there must be 2 audio channels.

I copy the audio when it is suitable just to save another lossy encode.

replies(1): >>41033612 #
2. indulona ◴[] No.41033612[source]
https://trac.ffmpeg.org/ticket/5718