Skip to content

Instantly share code, notes, and snippets.

@gchavez2
Created July 3, 2016 12:28
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • 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")
@bergpb
Copy link

bergpb commented Jan 18, 2020

awesome thank you

@ibrahim-dogan
Copy link

thank you very much 👍

@WHdev711
Copy link

Hi
I have some errors in your code
Please check it and reply to me
image

@gnai
Copy link

gnai commented Aug 14, 2020

Hi
I have some errors in your code
Please check it and reply to me
image

You need to re-install your ffmpeg library or update using pip

@FlyingThunder
Copy link

says that pydub doesnt have AudioSegment?

@sutgeorge
Copy link

Screenshot from 2020-08-28 09-24-36
Does any one of you guys know what this error refers to? I don't understand what's wrong, it should work as expected but it doesn't.
What is the error "Invalid data found when processing input" supposed to mean?

@andrew-houghton
Copy link

Works for me. Thanks!

@bucc-sanyam
Copy link

Screenshot from 2020-08-28 09-24-36
Does any one of you guys know what this error refers to? I don't understand what's wrong, it should work as expected but it doesn't.
What is the error "Invalid data found when processing input" supposed to mean?

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.

@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