我需要并排合并两个 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)
我正在使用 ubuntu 12.04 LTS、python 2.7 和 gstreamer。
我有以下几个问题,
- 当我使用更大的输入文件(持续时间超过 30 分钟)时,程序卡在 lator 阶段,但它仍然提供 output.mp4。
- 这很慢,如果我转换 30 分钟,程序还要运行 20 -25 分钟
- 两个输入文件可能有几秒(10-20 秒)的时间间隔,这会是问题所在吗?
如果我有任何其他方式来合并和转换这些文件,除了 gstreamer 之外也可以接受。
更新 1:
经过几天的工作,我发现程序在 pipeline.set_state(gst.STATE_NULL)
行挂起。任何人都有想法,如何克服这个问题。
基本上我需要毫无困难地释放管道资源。
更新 2:
我需要并排合并两个视频(avi)文件(每个文件都会有音频)并转换为 MP4 格式,这是这个问题的一个整体思路。我尝试使用 gstreamer 并停留在我在上面描述的地方。
最佳答案
工作 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
关于python - 以编程方式并排合并两个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21950054/