What ffmpeg command would convert a video for iPad/iPhone/iPod?

I'm currently using a batch file which (amongst other things) runs the following handbrakecli command line to convert a video:

HandBrakeCLI.exe -v0 --input "D:\input.mkv" --preset="Universal" --ipod-atom -t 1 --angle 1 --gain 0 --output="D:\output.mp4"

This has served me well for many years, producing a video file which is smaller than the original (good when you only have 16Gb devices), looks good enough and works on pretty much any iPod, iPad and iPhone I put it on.

However over time handbrakecli is struggling to convert more and more videos to the point that I need to look for an alternative. It's not helped by the fact that the logs provide absolutely no information as to why it has crashed. This video, for example, crashed at 71% through the conversion and this is the last 4 lines:

AC3 Passthru requested and input codec is not compatible for track 2, using AC3 encoder
x264 [info]: using SAR=9593/9600
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
x264 [info]: profile Constrained Baseline, level 3.0

Not very helpful.

I looked at using ffmpeg as an alternative, but all the examples I've seen on the internet do one or more of the following:

  1. Don't work (command line options aren't recognised) - common!
  2. Result in a file which is larger than the original - very common!
  3. Distort the picture, especially when an unexpected video size is converted (eg. non-widescreen PAL)
  4. Remove the audio
  5. Result in a video that the iPhone, iPod or iPad won't play

Is there a decent all-purpose ffmepg command line which will make any video run happily on all Apple device plus achieving a reduction in file size?

Bonus points if it'll shrink (but not distort) any video over 720p too.


Solution 1:

Here's my take on it. I'll take the iPhone 4 or newer (including all iPads) as the target : H.264 Main profile, level 3.1

ffmpeg -i input.mp4 -vcodec libx264 -profile:v main -level 3.1 -preset medium -crf 23 -x264-params ref=4 -acodec copy -movflags +faststart output.mp4

The values you are free to change :

  • -crf 23 : The constant quality setting. Higher value = less quality, smaller file. Lower = better quality, bigger file. Sane values are [18 - 24]
  • -preset medium : If this is too slow for you, use a faster preset. If you have the patience for it, use a slower one. See here for more information.
  • -acodec copy : Tries to copy the audio track into the MP4 container. If it can't be copied, you will have to reencode it to AAC/MP3/AC3... e.g -acodec libmp3lame

If your video is greater than 720p, add the following after the input :

-vf "scale=-2:720:flags=lanczos"

This will shrink your video to 720p and keep the aspect ratio. The scaling algorithm used will be lanczos as it is much better than the default.