How to add background music in python

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

How to add background music in python
How to add custom modules in Python
How to wrap in python code
How to omit parentheses in Python
How to write classes in python
How to read Excel in Python
How to view errors in python
How to write return in python
How to understand variables in Python
How to use SQLite in Python
How to add swap partition in Ubuntu
How to use and and or in Python
How to delete cache files in python
How to introduce third-party modules in Python
How to represent null values in python
How to save text files in python
How to write win programs in python
How to run id function in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to understand global variables in Python
How to view installed modules in python
How to open python in different systems
How to sort a dictionary in python
How to represent relative path in python
How to use the round function in python
How to use the zip function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
How to use the format function in python
How to use code running assistant in python
How to set code auto prompt in python
Teach you how to write games in python
How to delete files and directories in python
How to write a confession program in python
How to perform continuous multiplication calculation in python
How to comment python code
How to learn python quickly
How to uninstall python plugin
How to understand the introduction of packages in Python
How to understand a list of numbers in python
Example of how to automatically download pictures in python
How to save IE as an attachment in python
How to understand python objects
How to use python tuples
How to find the area of a circle in python
Do you still know how to draw cakes in python? ? ?
How to install Helm in Ubuntu
python how to view webpage code
How to use python thread pool
How to write python configuration file
How to install PHP7.4 in CentOS
How to install mysql in Ubuntu 14.04
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install HDP2.6 in Centos7.2
How to solve python dict garbled
How to install mysql in Ubuntu 14.04