python实现多个视频文件合成画中画效果
更新时间:2021年8月26日 16:00 点击:1598
本文实例为大家分享了多个视频文件合成画中画效果的python代码,供大家参考,具体内容如下
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')
源码下载
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 这篇文章主要介绍了python-opencv-画外接矩形框的实例代码,代码简单易懂,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-04
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 2022虎年新年即将来临,小编为大家带来了一个利用Python编写的虎年烟花特效,堪称全网最绚烂,文中的示例代码简洁易懂,感兴趣的同学可以动手试一试...2022-02-14
- 在本篇文章里小编给大家分享的是一篇关于python中numpy.empty()函数实例讲解内容,对此有兴趣的朋友们可以学习下。...2021-02-06
python-for x in range的用法(注意要点、细节)
这篇文章主要介绍了python-for x in range的用法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-05-10- 这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 这篇文章主要介绍了python如何实现b站直播自动发送弹幕,帮助大家更好的理解和学习使用python,感兴趣的朋友可以了解下...2021-02-20
python Matplotlib基础--如何添加文本和标注
这篇文章主要介绍了python Matplotlib基础--如何添加文本和标注,帮助大家更好的利用Matplotlib绘制图表,感兴趣的朋友可以了解下...2021-01-26- 这篇文章主要介绍了解决python 使用openpyxl读写大文件的坑,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-13
- 今天小编就为大家分享一篇python 计算方位角实例(根据两点的坐标计算),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
- 这篇文章主要为大家详细介绍了python实现双色球随机选号,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-05-02
- 在本篇文章里小编给大家整理的是一篇关于python中使用np.delete()的实例方法,对此有兴趣的朋友们可以学习参考下。...2021-02-01
- 这篇文章主要介绍了使用Python的pencolor函数实现渐变色功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-09
- 这篇文章主要介绍了python自动化办公操作PPT的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-05
Python getsizeof()和getsize()区分详解
这篇文章主要介绍了Python getsizeof()和getsize()区分详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-20- 这篇文章主要为大家详细介绍了python实现学生通讯录管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-25
- 这篇文章主要介绍了PyTorch一小时掌握之迁移学习篇,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-08
- 这篇文章主要介绍了解决python 两个时间戳相减出现结果错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-12
- 这篇文章主要介绍了Python绘制的爱心树与表白代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-06