Create a short video and audio with Python j

Keywords: Database MySQL

Welcome to pay attention

Python wechat ordering applet course video

https://edu.csdn.net/course/detail/36074

Python practical quantitative transaction financial management system

https://edu.csdn.net/course/detail/35475

pydub is a simple, convenient and powerful Python audio processing library

Project address:

https://github.com/jiaaro/pydub

It can be used to complete daily audio editing processing tools, such as audio extraction, audio cutting, sound effect processing, loudness control, channel configuration, audio synthesis, etc

First, install the dependency package

# Installation dependency
pip3 install pydub

3. Common operations

Next, let's talk about common operations of pydub

3-1 AudioSegment object

The most important class of pudub is AudioSegment

It is an immutable object that represents an audio segment object

First, we instantiate an AudioSegment object, which has a variety of built-in implementations

For example, we load a wav audio file locally

from pydub import AudioSegment

# Audio path
audio_path = "./raw/1.wav"

# Specify the audio format. Take wav audio as an example
format='wav'

# Instantiate an AudioSegment object
audio_segment = AudioSegment.from_file(audio_path, format)

3-2 clip a piece of audio

For the AudioSegment object, use brackets to specify the start time and end time, that is, you can quickly extract a piece of audio

PS: time in milliseconds

# An audio file
# Specify start time and end time
# Time in milliseconds
audio_part = audio_segment[start_time:end_time]

3-3 merge audio

Using pydub to merge multiple audio segments is very convenient. You only need to use the symbol + to add up the AudioSegment objects of the three audio segments

def sound_compound(one_audio_segment, *other):
    """
    Merge two pieces of audio
    :param one_audio_segment:
    :param other_audio_segment:
    :return:
    """
    result = one_audio_segment

    # Use the symbol + to merge one paragraph at a time
    for segment in other:
        result += segment
    return result

# Merge 3 audio segments
audio_segment1 = AudioSegment.from_file('./1.wav', 'wav')
audio_segment2 = AudioSegment.from_file('./2.wav', 'wav')
audio_segment3 = AudioSegment.from_file('./3.wav', 'wav')

# Merge 3 audio files
audio_segment_result = sound_compound(audio_segment1, audio_segment2, audio_segment3)

3-4 common audio attributes

Common audio attributes include:

  • duration
  • loudness
  • Channels
  • Frame rate
  • raw data

There are two ways to obtain the duration of audio, namely:

# Common audio properties
# Instantiate AudioSegment object
as = AudioSegment.from_file("sound1.wav")

# duration_seconds: len() is called internally, in seconds
# Mode 1: duration_seconds, in seconds
as_duration1 = as.duration_seconds

# Mode 2: len(as), in milliseconds
as_duration2 = (len(as) / 1000.0)

Other raw data can be obtained from the corresponding properties of AudioSegment object:

# Common audio properties
# Instantiate AudioSegment object
as = AudioSegment.from_file("sound1.wav")

# 2. Audio loudness
as_loudness = as.dBFS

# 3. Number of channels
as_channel_num = as.channels

# 4. Frame rate
# General values are 44100 (CD), 48000 (DVD), 22050, 24000, 12000 and 11025
as_frame_rate = sound.frame_rate

# 5. Audio raw data
as_raw_data = sound.raw_data

3-5 single audio fade in and fade out

In video clips, audio often needs to be faded in and out to make the sound effect play more natural

For example, for a single audio, use fade in at the beginning and fade out at the end, and specify the fade in and fade out time

PS: in milliseconds

def sound_fade_in_and_out(one_audio_segment, fade_in=0, fade_out=0):
    """
    Set fade in at the beginning and fade out at the end of a single audio segment
    Many operators can be used in a string, because operators will return one AudioSegment object
    :param one_audio_segment:
    :param fade_in:Fade in time (MS)
    :param fade_out:Fade out time (MS)
    :return:
    """
    return one_audio_segment.fade_in(fade_in).fade_out(fade_out)

audio_segment = AudioSegment.from_file("./raw/1.wav", format='wav')

# A single video fades in at the beginning and out at the end
sound_fade_in_and_out(audio_segment,2000,2000)

It should be noted that the fade() function built in the AudioSegment object can more flexibly achieve the fade in and fade out effect

sound = AudioSegment.from_file("./sound.wav")

# Fade to achieve fade in / fade out effect
# 1. Starting from 1s and lasting for 2s, the desalination effect is implemented, and gradually increases from 0dB to 3dB
sound_fade1 = sound.fade(to_gain=+3.0, start=1000, duration=2000)

# 2. From 1s to 4s, the desalination effect is performed in the middle of 1s, gradually reducing - 2dB from 0db
sound_fade2 = sound1.fade(to_gain=-2, start=1000, end=4000)

3-6 adjust audio playback speed

Audio speed adjustment is common in video clips

For example, at the end of the video, adjust the last picture frame to slow motion, and you also need to slow down the playback speed of the audio synchronously

def speed_change(sound, speed=1.0):
    """
    Change the speed of the audio
    :param sound:
    :param speed:
    :return:
    """
    sound_with_altered_frame_rate = sound._spawn(sound.raw_data, overrides={
        "frame_rate": int(sound.frame_rate * speed)
    })
    return sound_with_altered_frame_rate.set_frame_rate(sound.frame_rate)

# Change the playback speed of audio
# For example, 0.8 is adjusted to 0.8 of the previous speed
audio_new = speed_change(self.audio_segment, 0.8)

3-7 playing audio

AudioSegment Object use pydub Built in play() Method, you can play audio, which is very convenient when debugging code
from pydub.playback import play

sound1 = AudioSegment.from_file("./1.wav")

# Play audio
play(sound1)
3-8  Volume gain and decrease
 To adjust the volume of a piece of audio, you can directly AudioSegment The number of decibels corresponding to the instance can be added or subtracted
def sound_gain(audio_segment, db_value):
    """
    Sound gain
    :param audio_segment:
    :param db_value Decibel
    :return:
    """
    return audio_segment + db_value


def sound_reduce(audio_segment, db_value):
    """
    lower the volume
    :param audio_segment:
    :param db_value Decibel
    :return:
    """
    return audio_segment - db_value

# Reduce the volume, - 10 dB
audio_segment_temp = sound_reduce(audio_segment, 10)

# Increase the volume, + 10 dB
audio_segment_temp1 = sound_gain(audio_segment, 10)

3-9 cross desalination effect

use append() Method, you can merge multiple audio objects and add the effect of cross dilution
PS: use crossfade Parameter specifies the duration of cross fade in milliseconds

def sound_overlap_effect(one_audio_segment, other_audio_segment, overlap_during):
    """
    The effect of cross dilution is used while merging two pieces of audio
    :param one_audio_segment:First paragraph
    :param other_audio_segment:The second paragraph
    :param overlap_during:Unit: ms
    :return:
    """
    return one_audio_segment.append(other_audio_segment, crossfade=overlap_during)

# Two segment audio
audio_segment1 = AudioSegment.from_file('./1.wav', 'wav')
audio_segment2 = AudioSegment.from_file('./2.wav', 'wav')

# Combine two pieces of audio and add effects
# Duration: 2000 ms
result = sound_overlap_effect(audio_segment1, audio_segment2, 2000)

3-10 multichannel audio

Use from_ mono_ Audiorecordings() function, you can create multichannel audio on one track

def create_multichannel_as(self):
    """
    Create multichannel audio (2 or more supported)
    :return:
    """
    # PS: each mono audio segment should have the same duration and frame rate
    one_as = AudioSegment.from_wav("./1.wav")
    other_as = AudioSegment.from_wav("./2.wav")

    # Synthetic multichannel audio
    return AudioSegment.from_mono_audiosegments(one_as, other_as)

3-11 extracting and exporting audio

The AudioSegment method is instantiated in 3-1. The method is also applicable to video, that is, we can extract AudioSegment audio objects from video

Use the export(filename,format) method of the AudioSegment object to save the audio locally

# 1. Extract an AudioSegment audio object from the video
audio_segment = AudioSegment.from_file(video)

def save_audio(audio_segment, filename, format):
    """
    Save audio files locally
    :param audio_segment:
    :param filename:
    :param format:
    :return:
    """
    audio_segment.export(filename, format=format)

# 2. Export audio to local
save_audio(result, './result.wav', 'wav')

4. Practice

For funny short videos, this editing method is often used, that is, the video tail, reduce the speed of the last conversation and play it again

Prepare a video material, and then implement it through pydub

video_path = './raw.mp4'

# Note: you do not need to specify format to load video
audio_sgement = AudioSegment.from_file(video_path)

# Intercept tail content
audio_end = audio_sgement[70 * 1000:70 * 1000 + 3000]

# Slow down the speed. Adjust it according to the video speed
audio_end2 = speed_change(audio_end, 0.55)

# Merge two pieces of audio
audio_result = audio_end + audio_end2

# Tail fade out processing
audio_result.fade_out(1000)

# Video export
audio_result.export("result.wav", format='wav')

Finally, generate audio

5. Finally

This article only explains the common operations of pydub. For more operations, you can read the official documents to unlock

Some common operations of audio and video can be automated to separate themselves from repeated editing work

Full script Download:

https://download.csdn.net/download/huangbangqing12/46065047

Posted by Frame on Mon, 22 Nov 2021 21:31:41 -0800