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

39,323

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.

Share:
39,323

Related videos on Youtube

Richard
Author by

Richard

Updated on September 18, 2022

Comments

  • Richard
    Richard over 1 year

    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.

    • Gene
      Gene almost 9 years
      When is the last time you upgraded handbrakecli?
    • Richard
      Richard almost 9 years
      Pretty frequently in the hope the crashing bug is fixed. I'm currently running 0.10.2 (2015060900) - MinGW x86_64.
    • Bemmu
      Bemmu almost 5 years
      I had an mp4 video that refused to work on iOS, and this was the shortest command I could conjure that fixed it: ffmpeg -i broken_video.mp4 -pix_fmt yuv420p working_video.mp4
  • Elisa Cha Cha
    Elisa Cha Cha almost 9 years
    You can use -vf "scale=-2:720:flags=lanczos" instead. Adding -movflags +faststart is recommended if the file will be viewed via progressive download such as in a browser (and if not the option certainly doesn't hurt).
  • Richard
    Richard over 8 years
    The Zeranoe build I'm using doesn't support x264-params. Short of building it myself, is there a way to get around this problem?
  • Ely
    Ely over 8 years
    I use a Zeranoe build myself and -x264-params works fine. Are you sure you didn't miss something ? The option is mainly a safety to avoid having more than 4 reference frames that might not be handled by an older decoder.
  • Vitaly S.
    Vitaly S. about 7 years
    -acodec libmp3lame doesn't work for me. To enable audio on my iPad Mini I rebuilt ffmpeg with nonfree variant and used libfdk_aac coded.