How long does it take to edit music? 3 lines of code in Python instantly

You read that right, using Python, you only need 3 lines of statements to edit music. For example, I want to edit the music clip from 33 seconds to 1 minute and 10 seconds of "End of Time":

from pydub import AudioSegment
song = AudioSegment.from_mp3("end_of_time.mp3")
song[33,[object Object],1000].export('end_of_time_slice.mp3')

Running this script, we can complete the clip in an instant:


This function is very basic, and the processing speed is very fast. During this processing time, ordinary students may not have successfully opened Adobe Audition.

Let's introduce some more advanced gameplay of the Pydub module.

1. Prepare

Before starting, you need to make sure that Python and pip have been successfully installed on your computer. If not, you can visit this article: Super detailed Python installation guide  for installation.

(Optional 1)  If you use Python for data analysis, you can install Anaconda directly: Anaconda, a good helper for Python data analysis and mining , has built-in Python and pip.

(Optional 2)  In addition, it is recommended that you use the VSCode editor, which has many advantages: The best partner for Python programming—VSCode detailed guide .

Please choose one of the following ways to enter the command to install dependencies :
1. Open Cmd (Start-Run-CMD) in the Windows environment.
2. Open Terminal in the MacOS environment (command+space to enter Terminal).
3. If you are using VSCode editor or Pycharm, you can directly use the Terminal at the bottom of the interface.

pip install pydub

If you see Successfully installed xxx, the installation is successful. In addition, you need to install ffmpeg. There are many tutorials on the Internet. You can also read our previous article: " Extracting Music Climax" , which contains detailed installation tutorials.

Reply to the music clip in the background to get all the codes in this article.

2. Volume change

We can modify the entry volume and exit volume of the piece of music clipped above:

from pydub import AudioSegment

# 1秒=1000毫秒
SECOND = 1000
# 导入音乐
song = AudioSegment.from_mp3("end_of_time.mp3")

# 取33秒到70秒间的片段
song = song[33*SECOND:70*SECOND]

# 入场部分提高6分贝, 退场部分减少5分贝
ten_seconds = 10 * SECOND
last_five_seconds = -5 * SECOND
beginning = song[:ten_seconds] + 6
ending = song[last_five_seconds:] - 5

# 形成新片段
new_song = beginning + song[ten_seconds:last_five_seconds] + ending

# 导出音乐
new_song.export('end_of_time_slice.mp3')

listen and see:

The effect is the same as expected. Think about it, if you use a professional music editor to do this, you have to draw segments, set the volume, and then save it. Thinking carefully, it is too time-consuming!

2. Duplicate fragments

How to repeat the music clip from 33 seconds to 70 seconds we mentioned at the beginning? It's easy, you just need to know how to do multiplication:

from pydub import AudioSegment
song = AudioSegment.from_mp3("end_of_time.mp3")
(song[33*1000:63*1000])*2.export('end_of_time_slice.mp3')

Since the WeChat official account only allows three audios, you can read the original text and listen to this example. That's right, in pydub, addition , do you remember?

3. Ease in and out

Sometimes our ears need time to adapt to the change in volume, especially when watching short videos, the volume fluctuating from loud to loud is really not user-experienced. At this time, the design of gradual and gradual exit is particularly critical:

# 公众号:Python 实用宝典
from pydub import AudioSegment

# 导入音乐
song = AudioSegment.from_mp3("end_of_time.mp3")

# 提取片段
song = song[33*1000:70*1000]

# 渐进渐出
awesome = song.fade_in(5000).fade_out(3000)

# 导出音乐 
awesome.export('end_of_time_fade.mp3')

Listen to it, it's excellent!

4. Reverse the music

This is probably the funniest and most likely to do something special, and it will make you not recognize the song at all:

from pydub import AudioSegment

# 导入音乐
song = AudioSegment.from_mp3("end_of_time.mp3")[33*1000:70*1000]

# 翻转音乐
backwards = song.reverse()

# 导出音乐
backwards.export("end_of_time_reverse.mp3")

Of course, after reversing the music, the "ears" will become unbearable to "listen", which is also normal. After the original "positive law" is reversed, the tone rhythm also changes (it is not ruled out that people with special tastes like this feeling ). Curious? If you are curious, try it yourself!

This is the end of our article. If you like today's Python practical tutorial, please continue to pay attention to Python Practical Collection.

If you have any questions, you can reply in the background of the official account: join the group , answer the corresponding red letter verification information , and enter the mutual assistance group to ask.

Originality is not easy, I hope you can give me a thumbs up below and watch to support me to continue creating, thank you!

Click below to read the original text for a better reading experience

Python Practical Collection (pythondict.com)
is not just a collection.
Welcome to pay attention to the official account: Python Practical Collection

53e368532163f7bae0908f14fac1e5a3.png

Guess you like

Origin blog.csdn.net/u010751000/article/details/125109365