亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2021年08月26日 14:21:15   作者:~狼  
這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)多個視頻文件合成畫中畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了多個視頻文件合成畫中畫效果的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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Django框架模型簡單介紹與使用分析

    Django框架模型簡單介紹與使用分析

    這篇文章主要介紹了Django框架模型簡單介紹與使用,結(jié)合實例形式分析了Django框架中模型的概念、原理、使用技巧與相關(guān)操作注意事項,需要的朋友可以參考下
    2019-07-07
  • Pytorch卷積神經(jīng)網(wǎng)絡(luò)resent網(wǎng)絡(luò)實踐

    Pytorch卷積神經(jīng)網(wǎng)絡(luò)resent網(wǎng)絡(luò)實踐

    這篇文章主要為大家介紹了Pytorch卷積神經(jīng)網(wǎng)絡(luò)resent網(wǎng)絡(luò)實踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • python腳本編輯oss文件的實現(xiàn)示例

    python腳本編輯oss文件的實現(xiàn)示例

    本文主要介紹了python腳本編輯oss文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Python中一些深不見底的“坑”

    Python中一些深不見底的“坑”

    這篇文章主要給大家介紹了關(guān)于Python中一些深不見底的“坑”,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Django models文件模型變更錯誤解決

    Django models文件模型變更錯誤解決

    這篇文章主要介紹了Django models文件模型變更錯誤解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • Python編程語言的35個與眾不同之處(語言特征和使用技巧)

    Python編程語言的35個與眾不同之處(語言特征和使用技巧)

    這篇文章主要介紹了Python編程語言的35個與眾不同之處,Python編程語言的語言特征和使用技巧,需要的朋友可以參考下
    2014-07-07
  • Django Admin實現(xiàn)上傳圖片校驗功能

    Django Admin實現(xiàn)上傳圖片校驗功能

    這篇文章主要介紹了Django Admin實現(xiàn)上傳圖片校驗功能的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • 基于Python實現(xiàn)音樂節(jié)奏可視化效果

    基于Python實現(xiàn)音樂節(jié)奏可視化效果

    這篇文章主要為大家詳細(xì)介紹了如何基于Python語言實現(xiàn)音樂節(jié)奏可視化效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-06-06
  • Python中的flask框架詳解

    Python中的flask框架詳解

    flask是一個基于Python開發(fā)并且依賴jinja2模板和Werkzeug?WSGI服務(wù)的一個微型框架,對于Werkzeug本質(zhì)是Socket服務(wù)端,其用于接收http請求并對請求進(jìn)行預(yù)處理,然后觸發(fā)Flask框架,本文給大家介紹Python中的flask框架,感興趣的朋友跟隨小編一起看看吧
    2022-02-02
  • 安裝python-docx后,無法在pycharm中導(dǎo)入的解決方案

    安裝python-docx后,無法在pycharm中導(dǎo)入的解決方案

    這篇文章主要介紹了安裝python-docx后,無法在pycharm中導(dǎo)入的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論