How can I place a still image before the first frame of a video?

前端 未结 2 677
醉梦人生
醉梦人生 2020-12-30 10:51

When I encode videos by FFMpeg I would like to put a jpg image before the very first video frame, because when I embed the video on a webpage with \"video\" html5 tag, it sh

2条回答
  •  感动是毒
    2020-12-30 11:24

    The answer above works for me but in my case it took too much time to execute (perhaps because it re-encodes the entire video). I found another solution that's much faster. The basic idea is:

    1. Create a "video" that only has the image.
    2. Concatenate the above video with the original one, without re-encoding.

    Create a video that only has the image:

    ffmpeg -loop 1 -framerate 30 -i image.jpg -c:v libx264 -t 3 -pix_fmt yuv420p image.mp4
    

    Note the -framerate 30 option. It has to be the same with the main video. Also, the image should have the same dimension with the main video. The -t 3 specifies the length of the video in seconds.

    Convert the videos to MPEG-2 transport stream

    According to the ffmpeg official documentation, only certain files can be concatenated using the concat protocal, this includes the MPEG-2 transport streams. And since we have 2 MP4 videos, they can be losslessly converted to MPEG-2 TS:

    ffmpeg -i image.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts image.ts
    

    and for the main video:

    ffmpeg -i video.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts video.ts
    

    Concatenate the MPEG-2 TS files

    Now use the following command to concatenate the above intermediate files:

    ffmpeg -i "concat:image.ts|video.ts" -c copy -bsf:a aac_adtstoasc output.mp4
    

    Although there are 4 commands to run, combined they're still much faster then re-encoding the entire video.

提交回复
热议问题