How can I set a h.264 profile level with ffmpeg?

100,940

Solution 1

When encoding with libx264, you can set the H.264 profile and level with:

  • -profile:v – one of high, main, or baseline (and others, but this is irrelevant here)
  • -level:v – as defined in Annex A of the H.264 standard, e.g., 4.0.

For example:

ffmpeg -i input.mp4 -c:v libx264 -profile:v high -level:v 4.0 -c:a copy output.mp4

Here we've just copied the audio stream since it won't be affected.

The output will have the correct profile and level set in its metadata. You can check this while encoding, where x264 says something like:

[libx264 @ 0x7fb26103a000] profile High, level 4.0

MediaInfo can also help you analyze container and codec details.

Of course, reencoding the video will degrade its quality to some extent, given that you're applying a lossy conversion again. Try setting the -crf option to influence the constant quality parameter. The default value here is 23, while values between 18 and 28 are considered sane. Lower means better quality. If your input has a bit rate of up to 65,000 kBit/s, chances are it'll still look pretty good after conversion though.

Solution 2

As reference to your comment, try this command:

ffmpeg -i input.mp4 -map 1 -c:v libx264 -profile:v high -level:v 4.0 -c:a copy \
# copies all global metadata from input.mp4 to output.mp4
-map_metadata 0 \
# copies video stream metadata from input.mp4 to output.mp4
-map_metadata:s:v 0:s:v \
# copies audio stream metadata from input.mp4 to output.mp4
-map_metadata:s:a 0:s:a \
output.mp4

Cheers

Share:
100,940

Related videos on Youtube

Sam
Author by

Sam

Updated on September 18, 2022

Comments

  • Sam
    Sam almost 2 years

    I have a movie (m4v/h.264/AAC) which plays fine on my Mac but I recently discovered that it wont play on my Apple TV3. After looking at the properties of this movie file I see that it has a profile [email protected] but Apple TV3's only suport up to [email protected]. I think the only property that is making this video incompatible is the max video bit rate, all of the other properties look like they are supported in [email protected].

    How can I use ffmpeg to downgrade this video to [email protected]?

    Or do I have to instead change the actual property (max bit rate) that makes this video 4.1 instead of 4.0? I am worried that if I just change the bit rate, although the file would then be compatible with [email protected] it would still be 'tagged' as [email protected] and therefore still wouldn't play on my Apple TV3.

  • slhck
    slhck over 11 years
    Yes, the -v options are aliases of the :v ones. What metadata did you lose?
  • Sam
    Sam over 11 years
    I think it might have just been the artwork that was lost. I don't know if that is technically considered metadata... Is the artwork actually contained within the file? I didn't check everything else before I added the whole to it again.
  • Sam
    Sam almost 11 years
    I now have a different file with a similar problem. It is the wrong profile level but the rest of the specs seem to be compatible with my required level (4.0). If I use the commands you gave above, will it leave the data untouched unless it is incompatible with the entered profile level? Basically, I don't want ffmpeg to touch any of the data other than the tag that says what profile and level it is. Is that possible or is it going to set all of the values (nitrate, framerate etc) to preset values?
  • slhck
    slhck almost 11 years
    If you use the above commands it will re-encode the video. If you wanted to just change the level information, you would have to modify the bitstream, which no program I know of can do, unless you write it yourself. The level is stored as raw bytes in the Sequence Parameter Set of the bitstream (you'd need to look up the ISO/IEC 14496-10 standard, sections 7.3.2.1 and Annex A for details).
  • Sam
    Sam almost 11 years
    I just stumbled upon a way to achieve this. Subler actually has an option, when you select the video stream, to change the profile and level to make it compatible with certain devices. It tells you to make sure that the video is compatible wit the profile you are setting so I don't think it does any re-encoding, it must just change the metadata.
  • cdlvcdlv
    cdlvcdlv about 6 years
    To avoid losing metadata (and additionally don't lose the other tracks, audio or subtitles), add -map 0.
  • Ryan Griggs
    Ryan Griggs almost 6 years
    This works well for libx264. How about for h264_omx encoder? This doesn't work (yields an error).
  • slhck
    slhck almost 6 years
    @RyanGriggs The OMX encoder should support that, could you please post a new question and include the full command and command line output? Thanks
  • Ryan Griggs
    Ryan Griggs almost 6 years
    It was just an old version. Upgrading to 3.3+ fixes it.
  •  van9petryk
    van9petryk almost 4 years
    This work, but very slow. In 1 second convert only 1 second of film