Skip to content

Instantly share code, notes, and snippets.

@gchavez2
Created July 3, 2016 12:28
Show Gist options
  • Save gchavez2/53148cdf7490ad62699385791816b1ea to your computer and use it in GitHub Desktop.
Save gchavez2/53148cdf7490ad62699385791816b1ea to your computer and use it in GitHub Desktop.
Cut mp3 file with Python and pydub
# 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")
@WHdev711
Copy link

WHdev711 commented Jan 7, 2021

Okey let me check

@debjyoti003
Copy link

I am also getting same error, please someone tell me what's the actual issue, and how to resolve it

@truccm2001
Copy link

truccm2001 commented Aug 1, 2021

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.

@smkplus
Copy link

smkplus commented Jun 16, 2023

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