Using FFMPEG to add a single frame to end of MP4

风格不统一 提交于 2021-02-18 16:34:55

问题


I have written some image acquisition software and as I am acquiring these images I want to add the last image onto the end of a video file (a time-lapse of all images acquired so far). This will give a video view of all the images.

However I am struggling with trying to add the single frame. I have generated the time-lapse fine. The way I am doing this is waiting until I have gathered 10 images, then I generate the time-lapse. The command I have used to generate the time-lapse I will be adding the frames to is:

-framerate 40 -start_number N -i DSC_%05d.JPG -c:V libx264 -r 30 output.mp4

I have tried a few different method, all almost working but none quite working as needed. Here is what I've tried, with what command and what the outputs are:

Concat Demuxer to join JPG to MP4
The text file with list of files to use for the concatenation looks as follows:

file 'B00334_1.mp4'
file 'test_image.jpg'
duration 10

The command I use to attempt to join the files is:

ffmpeg -f concat -i concat.txt -c copy outputfile.mp4

The output from this attempt is a non-corrupt video (score!) but it only shows the first video and does not show any signs of the new frame, especially not for the specified 10 seconds. (I don't want it there for 10 seconds more like 0.1, however am using 10 seconds just so I can easily see when successful).

Converting JPG to MP4 then Concat Demuxer
So I thought; Maybe it wants a second video instead of an image, so I'll create a shor, frame-long video and append that onto the end of the time lapse.

Command used to create MP4 from JPG:

fmpeg -loop 1 -i test_image.jpg -r -t 5 test_video.mp4

This generates the video fine, I get a 5 second long video showing the still image. This video does not load a thumbnail, I'm not sure if this means anything. Now the concat.txt file looks like this:

file 'B00334_1.mp4'
file 'text_video.mp4'

Upon running the same command to call the text file shown in the first example I get the output of the first file (time-lapse) + the time of the concatenated video (success!?). However once you reach the point in the video at which it should be showing the image, it just shows the last frame of the timelapse and it glitches downwards for the 5 seconds of the second video. Basically not at all showing the second video and for however long the second video is the last frame will glitch out for that long.

Converting both JPG and MP4 to .ts then concating back to MP4
Command used to attempt to turn JPG to TS file:

ffmpeg -y -i test_image.jpg -c copy -bsf:v h264_mp4toannexb -f mpegts medium1.ts

This fails and returns the output: Codec 'mjpeg' (8) is not supported by the bitstream filter 'h264_mp4toannexb'. Supported codecs are: h264 (28).
Changing the codec to the apparently supported h264 doesn't affect the outcome either.

Converting JPG to MP4 and then to .ts then concatenating
Creating two TS files from the MP4s is fine, using the same command as before ffmpeg -y -i input.jpg -c copy -bsf:v h264_mp4toannexb -f mpegts output.ts. The newly made files also display as they should, the ts file created from the image just shows the image and the video shows the video. Now with these two new TS files I should be able to concatenate them together right?

I try to join these files with the concat protocol function of FFMPEG. Using this command:

ffmpeg -y -i 'concat:medium1.ts|medium2.ts' -c copy -bsf:a aac_adtstoasc output.mp4

Now this joins the videos together! The time is appended onto the end, and if you skip to the last few seconds you see the newly added image. However if you let the video play, as soon as it hits that second video the media-player crashes.


Any help and or ideas are greatly appreciated, I can give more information as needed. Thanks for reading.

Edit: Output log from Mulvya's answer:

[mov,mp4,m4a,3gp,3g2,mj2 @ 000000000255c0a0] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'concat.txt':
  Duration: N/A, start: 0.000000, bitrate: 44807 kb/s
    Stream #0:0(und): Video: h264 (High 4:2:2) (avc1 / 0x31637661), yuvj422p(pc), 2896x1944, 44807 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc
    Metadata:
      handler_name    : VideoHandler
Output #0, mp4, to 'merged.mp4':
  Metadata:
    encoder         : Lavf57.66.102
    Stream #0:0(und): Video: h264 (High 4:2:2) ([33][0][0][0] / 0x0021), yuvj422p(pc), 2896x1944, q=2-31, 44807 kb/s, 30 fps, 30 tbr, 15360 tbn, 15360 tbc
    Metadata:
      handler_name    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 0000000002564840] Auto-inserting h264_mp4toannexb 
bitstream filter
frame=   60 fps=0.0 q=-1.0 Lsize=    5564kB time=00:00:01.90 
bitrate=23990.6kbits/s speed=30.6x
video:5563kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.024155%

回答1:


For playback in VLC or ffplay, the following works for me:

ffmpeg -framerate 40 -i DSC_%05d.JPG -c:v libx264 -x264opts stitchable -r 30 main.mp4

.

ffmpeg -framerate 40 -loop 1 -i latest.JPG -c:v libx264 -x264opts stitchable -t 1 -r 30 appendix.mp4

.

file main.mp4
file appendix.mp4

.

ffmpeg -f concat -i list.txt -c copy merged.mp4

If the resolution is different, VLC will interrupt playback to switch context.



来源:https://stackoverflow.com/questions/45811800/using-ffmpeg-to-add-a-single-frame-to-end-of-mp4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!