Convert AVI into H.264 that works inside an HTML5 Video tag

15,158

.h264 is just a raw H.264 bytestream. That's just video content, which can be played back by sophisticated players, but usually you want to put everything into a container format, such as MPEG-4 Part 14 ("MP4").

So, run:

ffmpeg -i file.avi -c:v libx264 -pix_fmt yuv420p file.mp4

For HTML5 progressive download you may want to move the moov atom of the MP4 container to the beginning of the file, which allows instant playback:

ffmpeg -i file.avi -c:v libx264 -pix_fmt yuv420p -movflags faststart file.mp4

You may be interested in: What is a Codec (e.g. DivX?), and how does it differ from a File Format (e.g. MPG)?

Share:
15,158

Related videos on Youtube

SF Developer
Author by

SF Developer

Updated on September 18, 2022

Comments

  • SF Developer
    SF Developer almost 2 years

    I would like to convert an existing black and white AVI into an H.264 video that works inside an HTML5 video tag.

    I'm currently using this ffmpeg command:

    ffmpeg -i file.avi -y -c:v libx264 file.h264
    

    This command does not work for me. It does produce the H.264 file, but it does not play anywhere else other than VLC player.

  • SF Developer
    SF Developer about 10 years
    The MP4 output does not run on either an HTML5 Video tag or Windows Media Player. When I ran your command, I get a yellow message that says "Deprecated pixel format used, make sure you did set range correctly. No pixel format specified, yuvj444p for h.264 encoding chosen. Use -pix_fmt yuv420p ..which I used and now I can see the video on an HTML5 video tag.
  • slhck
    slhck about 10 years
    Yeah, that's why they have the message there. In the future, please post the full command line output of your ffmpeg commands too, not just the command itself. Then I would've probably told you that you needed to change the pixel format due to the non-standard input you have.
  • SF Developer
    SF Developer about 10 years
    My current scenario is this: the video is created on a local client machine and converted to h264 for compression, quality and SIZE (since I have to upload it via an API to the server). The 70Mg avi becomes 100K h264 video compared to a 1MG mp4 (so for upload reasons it's good to first convert it as h264 and then on the server to final mp4). I wonder if the command -i file.avi -y -c:v libx264 file.h264 I ran on the client should be different ...knowing that I will need to reconvert it on the server to mp4.
  • SF Developer
    SF Developer about 10 years
    I'll try that ...thanks for the help. and yes i'll upload the entire output next time. Much appreciated !!
  • Jet Blue
    Jet Blue about 4 years
    For anyone it might help, if you are using the flag -crf 0, you need to change this to a value of 1 or higher. For whatever reason mp4's created with -crf 0 also fail on "dumb players"
  • slhck
    slhck about 4 years
    @JetBlhe That is because CRF 0 will force YUV444 color format. Many players only support YUV422.
  • Admin
    Admin about 2 years
    thank you, it really worth to convert for an accepted format on modern smartphones