Created
July 3, 2016 12:28
-
-
Save gchavez2/53148cdf7490ad62699385791816b1ea to your computer and use it in GitHub Desktop.
Cut mp3 file with Python and pydub
This file contains hidden or 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
# https://github.com/jiaaro/pydub | |
from pydub import AudioSegment | |
files_path = '' | |
file_name = '' | |
startMin = 9 | |
startSec = 50 | |
endMin = 13 | |
endSec = 30 | |
# Time to miliseconds | |
startTime = startMin*60*1000+startSec*1000 | |
endTime = endMin*60*1000+endSec*1000 | |
# Opening file and extracting segment | |
song = AudioSegment.from_mp3( files_path+file_name+'.mp3' ) | |
extract = song[startTime:endTime] | |
# Saving | |
extract.export( file_name+'-extract.mp3', format="mp3") |
Okey let me check
I am also getting same error, please someone tell me what's the actual issue, and how to resolve it
I work in the python virtual environment, I had the same problem "WinError 2" and here how I solve it, I downloaded ffmeg from this page https://ffmpeg.org and copied paste file ffmpeg.exe, ffplay.exe, ffprobe.exe from bin folder to my Scripts folder and added these lines to my code:
AudioSegment.converter = r"path_to_your_Scripts\ffmpeg.exe"
AudioSegment.ffmpeg = r"path_to_your_Scripts\ffmpeg.exe"
AudioSegment.ffprobe = r"path_to_your_Scripts\ffprobe.exe"
Hope this can help.
from moviepy.editor import AudioFileClip
from tkinter import Tk, filedialog
# Create Tkinter root window
root = Tk()
root.withdraw() # Hide the root window
# Open file dialog to select the audio file
file_path = filedialog.askopenfilename()
# Get the file name from the selected file path
file_name = file_path.split('/')[-1].split('.')[0]
start_time_str = '00:07'
end_time_str = '00:19'
# Calculate start and end times in seconds
start_time_sec = int(start_time_str.split(':')[0]) * 60 + int(start_time_str.split(':')[1])
end_time_sec = int(end_time_str.split(':')[0]) * 60 + int(end_time_str.split(':')[1])
# Open the MP3 file
audio = AudioFileClip(file_path)
# Cut the audio segment
extract = audio.subclip(start_time_sec, end_time_sec)
# Open file dialog to select the save location
save_path = filedialog.asksaveasfilename(defaultextension='.mp3')
# Export the extracted segment as MP3 to the specified save location
extract.write_audiofile(save_path, codec="mp3")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This usually happens when the file you're trying to process is not in mp3 format. Try using AudioSegment.from_file(fileName) instead. I hope this helps.