Use ffmpeg to achieve audio double speed

Using Python's ffmpeg library to achieve audio double speed

In audio processing, sometimes we need to change the playback speed of the audio, that is, change the multiple speed of the audio. Python's ffmpeg library provides powerful and flexible functions that can easily achieve audio double-speed processing. This article will introduce how to use Python's ffmpeg library to achieve audio double speed.

Install the ffmpeg library

First, we need to install the ffmpeg library. Use the pip command to install:

pip install ffmpeg-python

Double the audio speed

Next, we will use the ffmpeg library to achieve audio double speed, the specific steps are as follows:

main function

from ffmpeg import audio
# 原音频路径
music = r"C:\Users\lenovo\Desktop\phone\ting\1.mp3"
# 生成的2倍速音频路径
music_2x = r"C:\Users\lenovo\Desktop\phone\ting2x\1_2x.mp3"
audio.a_speed(music, "2", music_2x)

Below is the code for batch conversion

# -*- coding: UTF-8 -*-
from ffmpeg import audio
import os
import tqdm


# 原文件夹路径
files = r"C:\Users\lenovo\Desktop\phone\ting55"
# 生成的2倍速文件夹路径
des_file = r"C:\Users\lenovo\Desktop\phone\ting2x"
# 将原文件的名字(默认原音频文件的名字是纯数字)拿到并转成列表
file_list = os.listdir(files)
j = int(input("从第几个开始转换: "))
for i in tqdm.tqdm(file_list):
    new_i = i[:-4]
    if int(new_i) >= j:
    	# 拼接出原音频文件的绝对路径
        a = os.path.join(files + "\\" + i)
        # 拼接出目标音频文件
        b = os.path.join(des_file + "\\" + f"{
      
      new_i}_2x.mp3")
        # 调用ffmpeg的audio函数,中间的参数”2“是2倍速转换
        audio.a_speed(a, "2", b)

Summarize

This article describes how to use Python's ffmpeg library to achieve audio double speed. By importing the ffmpeg library, specifying the input and output audio files, setting the speed value, and using the functions of the ffmpeg library, we can easily realize the processing of the audio double speed. Hope this article can help you, welcome to try and explore more functions of ffmpeg library!

Guess you like

Origin blog.csdn.net/m0_46114594/article/details/119273996