多个视频文件合成画中画效果(Python、ffmpeg)
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)的更多相关文章
- avi视频文件提取与合并
最近在做一个avi视频文件的提取与合并,花了几天熟悉avi文件格式.制作了一个提取与合并的动态库,不过仅限于提取视频,视频的合并还没添加一些额外判断,可能导致不同分辨率的视频文件合成后不能播放.欢迎大 ...
- FFmpeg命令行工具和批处理脚本进行简单的音视频文件编辑
FFmpeg_Tutorial FFmpeg工具和sdk库的使用demo 一.使用FFmpeg命令行工具和批处理脚本进行简单的音视频文件编辑 1.基本介绍 对于每一个从事音视频技术开发的工程师,想必没 ...
- Python调用ffpmeg和ffprobe处理视频文件
需求: 运营有若干批次的视频.有上千个,视频文件,有mp4格式的,有ts格式的 现在有需要去掉视频文件片头和片尾的批量操作需求. 比如 文件夹A下面的视频去掉片尾10秒 文件夹B下面的视频去掉片头6秒 ...
- PyQt+moviepy音视频剪辑实战1:多个音视频合成顺序播放或同屏播放的视频文件实现详解
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt+moviepy音视频剪辑实战 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一. ...
- 用Python和FFmpeg查找大码率的视频文件
用Python和FFmpeg查找大码率的视频文件 本文使用Python2.7, 这个工作分两步 遍历目录下的视频文件 用ffprobe获取是视频文件的码率信息 用ffprobe 获取json格式的视频 ...
- OpenCV+Python实现视频文件裁剪功能
Python编程实现对视频文件进行剪切的功能.截取指定长度的视频并保存,运行后首先选择要裁剪的视频,然后输入开始时间点和停止时间点即可.将剪切后的视频保存为output.avi文件 所属网站分类: 资 ...
- [Swift通天遁地]八、媒体与动画-(2)实现视频文件的播放和画中画
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- python opencv3 视频文件的读写
git: https://github.com/linyi0604/Computer-Vision # coding:utf8 import cv2 """ 读取视频文件 ...
- python实现调用摄像头或打开视频文件
目录: (一)调用摄像头或打开视频文件代码实现 (二)说明和补充 (一)调用摄像头或打开视频文件代码实现 1 # -*- coding=GBK -*- 2 import cv2 as cv 3 4 5 ...
- 使用FFmpeg处理视频文件:视频转码、剪切、合并、播放速调整
安装 略. 转码 最简单命令如下: ffmpeg -i out.ogv -vcodec h264 out.mp4ffmpeg -i out.ogv -vcodec mpeg4 out.mp4ffmpe ...
随机推荐
- 四月十五号java基础知识
1.今天下午做了一个题感受很深,自己做题没有思路或者有点思路死磕也没有搞清楚,看起来很简单的问题,在我手里很难 做咯许久还是室友帮忙解决的,后面重新打一遍还是出问题,找他解决的,问了问他我自己的问题, ...
- Python程序笔记20230302
Alice.Bob 和他们的朋友们 问题主体 密码学家 Rivest.Shamir.Adleman 于1977年4月撰写了一篇论文<数字签名与公钥密码学>(On Digital Signa ...
- Hugging News #0414: Attention 在多模态情景中的应用、Unity API 以及 Gradio 主题构建器
每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...
- 浅谈php GC(垃圾回收)机制及其与CTF的一点缘分
0x00 侠客日常(一):CTF江湖试剑 众所周知,在php中,当对象被销毁时会自动调用__destruct()方法,同时也要知道,如果程序报错或者抛出异常,则就不会触发该魔术方法. 看题: < ...
- Python if 语句练习
if语句 练习 # 1.以特殊方式跟管理员打招呼 # 创建一个至少包含 5 个用户名的列表,且其中一个用户名为 'admin' .想象你要编写代码,在每位用户登录网站后都打印一条问 # 候消息.遍历用 ...
- 做了个vscode 小插件,用于修改window 的颜色以区分同时打开的不同工作区,快用起来吧!
Coralize marketplace/coralize 以高效且便捷的方式自定义Visual Studio Code工作区窗口的状态栏.标题栏以及活动边栏等颜色!这将对那些需要同时打开多个vsco ...
- Grafana系列-统一展示-7-ElasticSearch数据源
系列文章 Grafana 系列文章 ElasticSearch 数据源 Grafana内置了对Elasticsearch的支持.你可以进行多种类型的查询,以可视化存储在Elasticsearch中的日 ...
- 2023-02-24:请用go语言调用ffmpeg,解码mp4文件并保存为YUV420SP格式文件,采用YUV420P转YUV420SP的方式。
2023-02-24:请用go语言调用ffmpeg,解码mp4文件并保存为YUV420SP格式文件,采用YUV420P转YUV420SP的方式. 答案2023-02-24: 使用 github.com ...
- 2023-01-13:joxit/docker-registry-ui是registry的web界面工具之一。请问部署在k3s中,yaml如何写?
2023-01-13:joxit/docker-registry-ui是registry的web界面工具之一.请问部署在k3s中,yaml如何写? 答案2023-01-13: yaml如下: apiV ...
- 2022-10-03:给定一个正数n,比如6 表示数轴上有 0,1,2,3,4,5,6 <0 或者 >6 的位置认为无法到达 给定两个数字x和y,0<= x,y <= n 表示小人一开始在x的位置,它
2022-10-03:给定一个正数n,比如6 表示数轴上有 0,1,2,3,4,5,6 <0 或者 >6 的位置认为无法到达 给定两个数字x和y,0<= x,y <= n 表示 ...