Step 1 从视频中分离出音频(MP4->mp3)

def separateMp4ToMp3(tmp):
mp4 = tmp.replace('.tmp', '.mp4')
print('---> Separate the video clip {0}'.format(mp4)) mp3 = tmp.replace('.tmp', '.mp3')
if os.path.exists(mp3):
print '\n\t{0} is detected. Skip. \n\tPlease delete .mp3 file if you need re-separate.'.format(mp3)
return cmd = 'ffmpeg -i {0} -f mp3 -vn -loglevel fatal {1}'.format(mp4, mp3)
print '\t{0}'.format(cmd) x = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for log in x.stdout.readlines():
print '[ffmpeg info] {0}'.format(log)
for log in x.stderr.readlines():
print '[ffmpeg error] {0}'.format(log) print '\tSuccess! {0} -> {1}\n'.format(mp4, mp3)

Step 2 根据时间轴多个音频合成一份音频(MP3->mp3)

def composeMp3ToMp3(arr = []):
if len(arr) <=0 :
print('--->Operate audio array is empty!')
return thisDir = os.path.dirname(arr[0])
if (os.path.exists(thisDir + "/composeAudio.mp3")):
print('--->{0}/composeAudio.mp3 is exist, if you need re-gennerate,Please delete it!'.format(thisDir))
return print('---> Compose the audio :')
var = ''
for tem in arr:
if os.path.exists(tem) == False:
print '\n\t{0} is not exist! \n\tPlease make sure audio file be exist if you need compose.'.format(tem)
return
var = var + " -i " + tem if var == '':
print '\n\t{0} is empty. \n\tPlease check .mp3 file if you need compose.'.format(var)
return cmd = 'ffmpeg {0} -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f mp3 -loglevel fatal {1}/composeAudio.mp3'.format(var, thisDir)
print '\t{0}'.format(cmd)
x = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for log in x.stdout.readlines():
print '[ffmpeg info] {0}'.format(log)
for log in x.stderr.readlines():
print '[ffmpeg error] {0}'.format(log) print '\tSuccess! {0} -> {1}\n'.format(var, thisDir + "/composeAudio.mp3")

Step 3 多个视频合成画中画效果<无声>(MP4->mp4)

def composeMp4ToMp4(arr = []):
if len(arr) <= 0:
print('--->Operate video array is empty!')
return thisDir = os.path.dirname(arr[0])
if (os.path.exists(thisDir + "/composeVideo.mp4")):
print('--->{0}/composeVideo.mp4 is exist, if you need re-gennerate,Please delete it!'.format(thisDir))
return print('---> Compose the video :')
var = ''
temparr = []
for tem in arr:
if os.path.exists(tem) == False:
print '\n\t{0} is not exist! \n\tPlease make sure video file be exist if you need compose.'.format(tem)
return #split image
png = tem.replace('.mp4', '.png')
tempcmd="ffmpeg -i {0} -ss 00:00:2.435 -loglevel fatal -vframes 1 {1}".format(tem, png)
print '\t{0}'.format(tempcmd)
x = subprocess.Popen(tempcmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
x.wait()
for log in x.stdout.readlines():
print'[ffmpeg info] {0}'.format(log)
for log in x.stderr.readlines():
print'[ffmpeg error] {0}'.format(log) img = Image.open(png)
imgSize = img.size
#ipad
if (imgSize[0] > imgSize[1]) :
temparr.append(tem)
#mobile
else:
var = var + " -i " + tem
img.close() if (len(temparr) > 0):
for tem in temparr:
var = var + " -i " + tem if var == '':
print '\n\t{0} is empty. \n\tPlease check video file if you need compose.'.format(var)
return cmd = 'ffmpeg ' + var + ' -filter_complex "[1:v]scale=w=176:h=144:force_original_aspect_ratio=decrease[ckout];[0:v]' \
'[ckout]overlay=x=W-w-10:y=10[out]" -map "[out]" -movflags faststart -loglevel fatal ' + thisDir + '/composeVideo.mp4'.format(var, thisDir)
print '\t{0}'.format(cmd)
x = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for log in x.stdout.readlines():
print '[ffmpeg info] {0}'.format(log)
for log in x.stderr.readlines():
print '[ffmpeg error] {0}'.format(log) print '\tSuccess!\n {0} -> {1}\n'.format(var, thisDir + "/composeVideo.mp4")

Step 4 音频与视频合成

def communicateAudioVideo(folder):
if (os.path.exists(folder + "/communicateVideo.mp4")):
print('--->{0}/communicateVideo.mp4 is exist, if you need re-gennerate,Please delete it!'.format(folder))
return if ((os.path.exists(folder + "/composeVideo.mp4") == False) or
(os.path.exists(folder + "/composeAudio.mp3") == False)):
print('--->{0}/composeVideo.mp4 or composeAudio.mp3 must be exist!'.format(folder))
return print('---> Communicate the video :')
cmd = 'ffmpeg -i ' + folder + '/composeVideo.mp4 -i ' + folder + '/composeAudio.mp3 -f mp4 ' \
' -loglevel fatal ' + folder +'/communicateVideo.mp4'
print '\t{0}'.format(cmd)
x = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for log in x.stdout.readlines():
print '[ffmpeg info] {0}'.format(log)
for log in x.stderr.readlines():
print '[ffmpeg error] {0}'.format(log) print '\tSuccess!\n {0} and {1} -> {2}\n'.format(folder + '/composeVideo.mp4', folder + '/composeAudio.mp3', folder +'/communicateVideo.mp4')

源码下载:https://github.com/wolf-song-ml/recording/tree/master

refer to:http://blog.csdn.net/wolfjson/article/details/76509709

多个视频文件合成画中画效果(Python、ffmpeg)的更多相关文章

  1. avi视频文件提取与合并

    最近在做一个avi视频文件的提取与合并,花了几天熟悉avi文件格式.制作了一个提取与合并的动态库,不过仅限于提取视频,视频的合并还没添加一些额外判断,可能导致不同分辨率的视频文件合成后不能播放.欢迎大 ...

  2. FFmpeg命令行工具和批处理脚本进行简单的音视频文件编辑

    FFmpeg_Tutorial FFmpeg工具和sdk库的使用demo 一.使用FFmpeg命令行工具和批处理脚本进行简单的音视频文件编辑 1.基本介绍 对于每一个从事音视频技术开发的工程师,想必没 ...

  3. Python调用ffpmeg和ffprobe处理视频文件

    需求: 运营有若干批次的视频.有上千个,视频文件,有mp4格式的,有ts格式的 现在有需要去掉视频文件片头和片尾的批量操作需求. 比如 文件夹A下面的视频去掉片尾10秒 文件夹B下面的视频去掉片头6秒 ...

  4. PyQt+moviepy音视频剪辑实战1:多个音视频合成顺序播放或同屏播放的视频文件实现详解

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...

  5. 用Python和FFmpeg查找大码率的视频文件

    用Python和FFmpeg查找大码率的视频文件 本文使用Python2.7, 这个工作分两步 遍历目录下的视频文件 用ffprobe获取是视频文件的码率信息 用ffprobe 获取json格式的视频 ...

  6. OpenCV+Python实现视频文件裁剪功能

    Python编程实现对视频文件进行剪切的功能.截取指定长度的视频并保存,运行后首先选择要裁剪的视频,然后输入开始时间点和停止时间点即可.将剪切后的视频保存为output.avi文件 所属网站分类: 资 ...

  7. [Swift通天遁地]八、媒体与动画-(2)实现视频文件的播放和画中画

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  8. python opencv3 视频文件的读写

    git: https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 读取视频文件 ...

  9. python实现调用摄像头或打开视频文件

    目录: (一)调用摄像头或打开视频文件代码实现 (二)说明和补充 (一)调用摄像头或打开视频文件代码实现 1 # -*- coding=GBK -*- 2 import cv2 as cv 3 4 5 ...

  10. 使用FFmpeg处理视频文件:视频转码、剪切、合并、播放速调整

    安装 略. 转码 最简单命令如下: ffmpeg -i out.ogv -vcodec h264 out.mp4ffmpeg -i out.ogv -vcodec mpeg4 out.mp4ffmpe ...

随机推荐

  1. 数据挖掘神经网络—R实现

    神经网络 生物神经网络主要是指人脑的神经网络,它是人工神经网络的技术原型.人脑是人类思维的物质基础,思维的功能定位在大脑皮层,后者含有大约10^11个神经元,每个神经元又通过神经突触与大约103个其它 ...

  2. 腾讯出品小程序自动化测试框架【Minium】系列(七)测试框架的设计和开发

    前言 整个框架的开发及调通是在3月27日晚上22点完成,如下: 这篇文章真的是拖了太久了,久到我居然把代码部分完成后,彻底给忘了,这记性,真的是年纪大了! 框架的设计开发 1.框架搭建设计要素 日志& ...

  3. SPI-SPI主机硬件片选功能使用说明

    SPI主机硬件片选功能使用说明 SPI协议最早的标准,是由摩托罗拉公司制定.在协议使用的过程中,根据实际需求可能会进行一些扩展和修改. 在一份由飞思卡尔半导体发布的SPI V4.01版本规范中,对片选 ...

  4. 32-webpack详细配置-entry

    const HtmlWebpackPlugin = require('html-webpack-plugin') const {resolve} = require('path') /** * ent ...

  5. 从案例中详解go-errgroup-源码

    一.背景 某次会议上发表了error group包,一个g失败,其他的g会同时失败的错误言论(看了一下源码中的一句话The first call to return a non-nil error c ...

  6. mosn基于延迟负载均衡算法 -- 走得更快,期待走得更稳

    前言 这篇文章主要是介绍mosn在v1.5.0中新引入的基于延迟的负载均衡算法. 对分布式系统中延迟出现的原因进行剖析 介绍mosn都通过哪些方法来降低延迟 构建来与生产环境性能分布相近的测试用例来对 ...

  7. 2022-04-05:golang中go.mod文件,做框架开发需要解析,请问如何解析?

    2022-04-05:golang中go.mod文件,做框架开发需要解析,请问如何解析? 答案2022-04-05: go.mod解析可参考这个.做框架开发主要是用到require和replace.具 ...

  8. 2022-02-09:k8s安装redis,yaml如何写?

    2022-02-09:k8s安装redis,yaml如何写? 答案2022-02-29: apiVersion: apps/v1 kind: Deployment metadata: labels: ...

  9. 2021-05-04:给定一个非负整数c,你要判断是否存在两个整数a和b,使得a*a+b*b=c。【举例】c=5时,返回true。c=4时,返回true。c=3时,返回false。

    2021-05-04:给定一个非负整数c,你要判断是否存在两个整数a和b,使得aa+bb=c.[举例]c=5时,返回true.c=4时,返回true.c=3时,返回false. 福大大 答案2021- ...

  10. Django接入SwaggerAPI接口文档-完整操作(包含错误处理)

    Swagger的简介: Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化RESTful风格的Web服务,在做后端开发的同时自动生成一个API文档供前端查看,当接口有变动时,对应的接口 ...