Last active
February 16, 2020 18:35
-
-
Save nkpro2000sr/40040ef63507942a6d0e8a24fbb42232 to your computer and use it in GitHub Desktop.
To change dueration of audio file. we can also generate different voices with changing dueration and frame_rate.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pydub import AudioSegment as As | |
import os | |
def change(p_af, p_maf, duer, fr= None) : | |
"""$p_af is path of input audio file | |
$p_maf is path for output audio file | |
$duer = output audio file duration (in seconds) | |
$fr = output audio file frame rate""" | |
with open(p_af, 'rb') as af : | |
sound = As.from_file(af) | |
new_fr = int(sound.frame_count() / duer) | |
m_sound = sound._spawn(sound.raw_data, #modified_sound | |
overrides={'frame_rate':new_fr}) | |
if fr != -1 : m_sound = m_sound.set_frame_rate(fr if fr else sound.frame_rate) | |
m_sound.export(p_maf, format=os.path.splitext(p_maf)[1].replace('.','')) | |
return len(m_sound), m_sound.frame_count(), m_sound.frame_rate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment