多个视频文件合成画中画效果(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

参考文献:https://www.cnblogs.com/wolf-song/p/7708364.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 222,590评论 6 517
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 95,157评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 169,301评论 0 362
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 60,078评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 69,082评论 6 398
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,682评论 1 312
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,155评论 3 422
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,098评论 0 277
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,638评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,701评论 3 342
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,852评论 1 353
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,520评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,181评论 3 335
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,674评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,788评论 1 274
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 49,279评论 3 379
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,851评论 2 361

推荐阅读更多精彩内容