24

How can I convert a video from H.264 (High 4:4:4 Profile) to H.264 (Main Profile) using ffmpeg?

I can't do that with this command: ffmpeg -i 1/25359.mp4 -profile:v main out.mp4.

That'd return an error:

...
That'd return an error:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '1/25359.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.40.101
  Duration: 00:00:06.08, start: 0.000000, bitrate: 1059 kb/s
    Stream #0:0(und): Video: h264 (High 4:4:4 Predictive) (avc1 / 0x31637661), yuv444p, 351x297, 1057 kb/s, 12.50 fps, 12.50 tbr, 12800 tbn, 25 tbc (default)
    Metadata:
      handler_name    : VideoHandler
No pixel format specified, yuv444p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
x264 [error]: main profile doesn't support 4:4:4
[libx264 @ 0x8fa9640] Error setting profile main.
[libx264 @ 0x8fa9640] Possible profiles: baseline main high high10 high422 high444
Output #0, mp4, to '1/24545.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.40.101
    Stream #0:0(und): Video: h264, none, q=2-31, 128 kb/s, 12.50 fps (default)
    Metadata:
      handler_name    : VideoHandler
      encoder         : Lavc56.60.100 libx264
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Milad
  • 692
  • 2
  • 9
  • 27

2 Answers2

49

Your source video has full-sized chroma planes - as indicated by the latter two 4s in YUV444P - and main profile doesn't support that format, so you'll have to select a pixel format like YUV 4:2:0

ffmpeg -i 1/25359.mp4 -vf "scale=2*trunc(iw/2):-2,setsar=1" -profile:v main -pix_fmt yuv420p out.mp4
Gyan
  • 85,394
  • 9
  • 169
  • 201
  • 1
    Just one question. Is it OK to use "-pix_fmt yuv420p" for every video? I mean, would it give any error for some videos? How does it affect the videos? (I just wanna create a good ffmpeg command to use it in my website.) – Milad Jun 22 '16 at 13:49
  • 8
    For web playback, you have to use it. It's the only pixel format supported. – Gyan Jun 22 '16 at 14:05
  • this work for me, whis this settings my videos were compatible with whatsapp and other social media – Natan Jun 09 '19 at 16:35
  • What is the goal of these options: -vf "scale=2*trunc(iw/2):-2,setsar=1" ? – jap1968 Aug 26 '19 at 18:42
  • The scale args ensure an even width and height. The setsar is force a sample aspect ratio of 1 so that players don't resize the video during display. – Gyan Aug 26 '19 at 19:22
  • 2
    web (Chrome) supports 4:4:4 now, no problem. – Валерий Заподовников Sep 28 '21 at 12:03
8

The former answer was right, but I think the following answer was exactly what the asker want.

ffmpeg -i 1/25359.mp4 -profile:v main -pix_fmt yuv420p out.mp4
  • -profile:v: means which mode you want to encode video, there are some options here
  • -pix_fmt: yuv420p was allright for old videos.
FantasyJXF
  • 754
  • 7
  • 14