以编程方式并排合并两个视频

2024-09-28 05:16:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要合并两个avi视频并排,我成功地使用python+gstreamer,如下代码所示。在

pipe = """
videomixer2 name=mix background=1 
  sink_0::xpos=0 sink_0::ypos=60 sink_0::zorder=0 
  sink_1::xpos=640 sink_1::ypos=60 sink_1::zorder=0 !
ffmpegcolorspace name=colorsp_saida ! 
video/x-raw-yuv, format=(fourcc)I420, width=1280, height=480, framerate=25/1 ! 
x264enc quantizer=45 speed-preset=6 profile=1 ! queue ! 
mp4mux name=mux  ! queue ! filesink location="output.mp4"

filesrc location="video1.avi" ! decodebin2 name=dbvideo1 ! 
aspectratiocrop aspect-ratio=16/9 ! videoscale ! videorate ! 
ffmpegcolorspace name=colorsp_video1 ! 
video/x-raw-yuv, format=(fourcc)AYUV, framerate=25/1, width=640, height=360 ! 
mix.sink_0 

filesrc location="video2.avi" ! decodebin2 name=dbvideo2 ! 
aspectratiocrop aspect-ratio=16/9 ! videoscale ! videorate ! 
ffmpegcolorspace name=colorsp_video2 ! 
video/x-raw-yuv, format=(fourcc)AYUV, framerate=25/1, width=640, height=360 ! 
mix.sink_1 
"""

import gst
pipeline = gst.Pipeline()
bus = pipeline.get_bus()

gst_bin = gst.parse_bin_from_description(pipe, False)
pipeline.add(gst_bin)

pipeline.set_state(gst.STATE_PLAYING)
msg = bus.timed_pop_filtered(gst.CLOCK_TIME_NONE, gst.MESSAGE_ERROR | gst.MESSAGE_EOS)
pipeline.set_state(gst.STATE_NULL)

我使用的是Ubuntu12.04LTS、Python2.7和gstreamer。在

我有以下几个问题

  • 当我使用更大的输入文件(持续时间超过30分钟)时,程序挂在lator阶段,但它仍然提供输出.mp4. 在
  • 这很慢,如果我转换30分钟,程序也运行20-25分钟
  • 两个输入文件可能有几秒钟(10-20秒)的时间间隔,会不会有问题?在

如果我有其他方法合并和转换这个文件,除了gstreamer也可以接受。在

更新1:

经过几天的工作,我发现程序挂在pipeline.set_state(gst.STATE_NULL)行上。谁有办法,怎么克服这个问题。在

基本上我需要释放管道的资源,没有任何麻烦。在

更新2:

我需要合并两个视频(avi)文件(任何一个文件都会有音频)并排并转换成MP4格式,是这个问题的一个整体思路。我尝试了gstreamer,并坚持在上面描述的地方。在


Tags: 文件nameformatrawpipelinevideomixsink
2条回答

工作ffmpeg代码

./ffmpeg -i video1.avi -i video2.avi -r 30 -filter_complex "[0:v]scale=640:480, setpts=PTS-STARTPTS, pad=1280:720:0:120[left]; [1:v]scale=640:480, setpts=PTS-STARTPTS, pad=640:720:0:120[right]; [left][right]overlay=w; amerge,pan=stereo:c0<c0+c2:c1<c1+c3" -vcodec libx264 -acodec aac -strict experimental output.mp4

相关问题 更多 >

    热门问题