How to add background music in python:
1、 Import the pygame resource package;
2、 Modify the file path of the music;
3、 Use the init() method to initialize;
4、 Use the load() method to add music files;
5、 Use the play() method to play the music stream.
The following code can be directly copied and pasted into your own code to add music. (The address of the music in the second line needs to write your own address)
import pygame#Import pygame resource package
file=r'E:\Python_Exercise3.mp3'#Music path
pygame.mixer.init()#initialization
track = pygame.mixer.music.load(file)#Load music files
pygame.mixer.music.play()#Start playing music stream
Knowledge point expansion:
Python adds background music to the video and changes the volume
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)
# View audio track information
print('*'*80)
cmd='ffmpeg -i '+ outmp3 +' -filter_complex volumedetect -c:v copy -f null /dev/null'
P = subprocess.call(cmd)
So far, this article on how to add background music in python is introduced. For more information about the method of adding background music in python, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you all Support ZaLou.Cn a lot!
Recommended Posts