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

标签 python ubuntu-12.04 gstreamer

我需要并排合并两个 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/

相关文章:

arduino - 无法使用 stino 和 Ubuntu 12.04 (Precise Pangolin) 将代码上传到 Arduino Uno

opencv - Yocto构建中OpenCV中的Gsteamer冲突声明

c++ - Gstreamer 通过 RTP 流式传输多个摄像头,同时保存每个流

c - 使用带有 c API 的 gstreamer 显示图像

python - 在 PySpark 中缓存用户和产品潜在特征以缩短预测时间

python - 字典的排序列表---默认行为是什么(没有关键参数)?

php - Ubuntu 12.04 php、mysql-server 和 phpmyadmin 安装 apache2 Web 服务器错误

大文件上的c++疯狂内存消耗

python - 将列表内容存储为具有相同索引的两个不同列表

python - 如何表达调用派生虚拟的基类方法?