使用FFMPEG将单个帧添加到MP4的末尾 [英] Using FFMPEG to add a single frame to end of MP4

查看:77
本文介绍了使用FFMPEG将单个帧添加到MP4的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了一些图像获取软件,并且在获取这些图像时,我想将最后一个图像添加到视频文件的末尾(到目前为止获取的所有图像都经过一段时间).这将提供所有图像的视频视图.

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.

但是,我在尝试添加单个框架时遇到了麻烦.我已经产生了延时罚款.我这样做的方法是等到我收集了10张图像,然后生成延时镜头.我用来生成延时镜头的命令是:

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

-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将JPG加入MP4
带有用于连接的文件列表的文本文件如下所示:

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

文件'B00334_1.mp4'
文件"test_image.jpg"
持续时间10

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复制outputfile.mp4

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

此尝试的输出是不损坏的视频(得分!),但它仅显示第一个视频,并且不显示新帧的任何信号,尤其是在指定的10秒钟内.(我不希望它再像0.1那样停留10秒钟,但是我使用10秒钟只是为了让我很容易看到成功的时间.)

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).

先将JPG转换为MP4,然后将Concat Demuxer转换
也许它想要第二个视频而不是图像,所以我将创建一个短的,长帧的视频,并将其附加到时间流逝的末尾.

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.

用于从JPG创建MP4的命令:

Command used to create MP4 from JPG:

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

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

这会产生很好的视频,我得到了一个5秒钟长的视频,显示了静止图像.该视频未加载缩略图,我不确定是否有任何意义.现在,concat.txt文件如下所示:

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:

文件'B00334_1.mp4'
文件"text_video.mp4"

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

运行相同的命令以调用第一个示例中所示的文本文件时,我得到第一个文件的输出(延时)+所连视频的时间(成功!?).但是,一旦到达视频中应显示图像的位置,它便会显示间隔拍摄的最后一帧,并在第二个视频的5秒钟内向下毛刺.基本上根本不显示第二个视频,而将第二个视频作为最后一帧播放的时间会持续这么长时间.

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.

将JPG和MP4都转换为.ts,然后再转换回MP4
用于尝试将JPG转换为TS文件的命令:

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复制-bsf:v h264_mp4toannexb -f mpegts medium1.ts

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

这失败并返回输出:编解码器'mjpeg'(8)不被位流过滤器'h264_mp4toannexb'支持.支持的编解码器为:h264(28).
将编解码器更改为显然支持的h264也不影响结果.

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.

将JPG转换为MP4,然后转换为.ts,然后进行连接
使用与 ffmpeg -y -i input.jpg -c copy -bsf:v h264_mp4toannexb -f mpegts output.ts 之前相同的命令,从MP4创建两个TS文件就可以了.新创建的文件也将按原样显示,从图像创建的ts文件仅显示该图像,而视频显示该视频.现在,有了这两个新的TS文件,我应该可以将它们串联在一起了,对吗?

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?

我尝试使用FFMPEG的 concat协议功能将这些文件加入.使用此命令:

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复制-bsf:a aac_adtstoasc output.mp4

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.


任何帮助和/或想法都将不胜感激,我可以根据需要提供更多信息.感谢您的阅读.

编辑:来自Mulvya的答案的输出日志:


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%

推荐答案

要在VLC或ffplay中播放,以下对我有用:

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

如果分辨率不同,则VLC将中断播放以切换上下文.

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

这篇关于使用FFMPEG将单个帧添加到MP4的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆