gpt4 book ai didi

python实现多个视频文件合成画中画效果

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 31 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章python实现多个视频文件合成画中画效果由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例为大家分享了多个视频文件合成画中画效果的python代码,供大家参考,具体内容如下 。

Step 1 从视频中分离出音频(MP4->mp3) 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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) 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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) 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 音频与视频合成 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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://blog.csdn.net/wolfjson/article/details/76509709 。

最后此篇关于python实现多个视频文件合成画中画效果的文章就讲到这里了,如果你想了解更多关于python实现多个视频文件合成画中画效果的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com