用到给视频添加背景音乐,并改变音量。记录一下,与碰到同样问题的朋友共享。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
import subprocess
inmp4 = 'E:/PycharmProjects/untitled2/hecheng/191030_232_xs.mp4'
inmp3 = 'E:/PycharmProjects/untitled2/hecheng/bg.mp3'
inmp32 = 'E:/PycharmProjects/untitled2/hecheng/bg2.mp3'
outmp3 = 'E:/PycharmProjects/untitled2/hecheng/bg_out.mp3'
outmp4 = 'E:/PycharmProjects/untitled2/hecheng/191030_232_xs_bg.mp4'
cmd = 'ffmpeg -y -i ' + inmp4 + ' -i ' + inmp3 + ' - filter_complex \
"[ 0 :a]volume = 10dB [a0]; \
[ 1 :a]volume = - 10dB [a1]; \
[a0][a1]amix = inputs = 2 [a]" \
- map 0 :v - map "[a]" - c:v copy - c:a aac - shortest ' + outmp4
p = subprocess.call(cmd, shell = True , stdout = subprocess.PIPE, stderr = subprocess.PIPE)
# 查看音轨信息
print ( '*' * 80 )
cmd = 'ffmpeg -i ' + outmp3 + ' -filter_complex volumedetect -c:v copy -f null /dev/null'
P = subprocess.call(cmd)
|
记录几个cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# 分别降低音量后叠加
cmd = 'ffmpeg -y -i ' + inmp4 + ' -i ' + inmp3 + ' - filter_complex \
"[ 0 :a]volume = 10dB [a0]; \
[ 1 :a]volume = - 30dB [a1]; \
[a0][a1]amix = inputs = 2 [a]" \
- map 0 :v - map "[a]" - c:v copy - c:a aac - shortest ' + outmp4
# 纯音频,叠加,可控制音量,并且设置一个循环,并按照第一个截取时间
cmd = 'ffmpeg -y -i ' + mp4p3 + ' -i ' + inmp3 + ' - filter_complex \
"[ 0 :a]volume = 10dB [a0]; \
[ 1 :a]aloop = loop = - 1 :size = 2e + 09 [a1]; \
[a1]volume = - 15dB [a12]; \
[a0][a12]amix = inputs = 2 :duration = first:dropout_transition = 2 " ' + outmp4mp3
# 视频添加循环音乐
cmd = 'ffmpeg -y -i ' + inmp4 + ' -i ' + inmp3 + ' - filter_complex \
"[ 0 :a]volume = 10dB [a0]; \
[ 1 :a]aloop = loop = - 1 :size = 2e + 09 [a1]; \
[a1]volume = - 15dB [a12]; \
[a0][a12]amix = inputs = 2 :duration = first:dropout_transition = 2 [a]" \
- map 0 :v - map "[a]" - c:v copy - c:a aac - shortest ' + outmp4
|
ffmpeg 隐藏调试信息
1
|
- loglevel quiet
|
资料参考:
https://*.com/questions/13780736/ffmpeg-unable-to-find-a-suitable-output-format-for-i
https://*.com/questions/44712868/ffmpeg-set-volume-in-amix
到此这篇关于python给视频添加背景音乐并改变音量的具体方法的文章就介绍到这了,更多相关python给视频添加背景音乐并改变音量内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://wqian.net/blog/2019/1031-python-ffmpeg-index.html