人妖在线一区,国产日韩欧美一区二区综合在线,国产啪精品视频网站免费,欧美内射深插日本少妇

新聞動態(tài)

python實現(xiàn)多個視頻文件合成畫中畫效果

發(fā)布日期:2022-02-04 09:46 | 文章來源:CSDN

本文實例為大家分享了多個視頻文件合成畫中畫效果的python代碼,供大家參考,具體內(nèi)容如下

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 根據(jù)時間軸多個音頻合成一份音頻(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')

源碼下載

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持本站。

香港服務(wù)器租用

版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實時開通

自選配置、實時開通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問服務(wù)

1對1客戶咨詢顧問

在線
客服

在線客服:7*24小時在線

客服
熱線

400-630-3752
7*24小時客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部