Skip to content

Instantly share code, notes, and snippets.

@smkplus
Last active June 16, 2023 14:32
Show Gist options
  • Save smkplus/4dd18e9b7eee8e3226e92f44fe3cf943 to your computer and use it in GitHub Desktop.
Save smkplus/4dd18e9b7eee8e3226e92f44fe3cf943 to your computer and use it in GitHub Desktop.
Python Cut MP3
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")
#*******************************************************
# multiple files
#*******************************************************
from moviepy.editor import AudioFileClip
from tkinter import Tk, filedialog
import os
import datetime
# Create Tkinter root window
root = Tk()
root.withdraw() # Hide the root window
# Open file dialog to select the audio file
file_path = filedialog.askopenfilename()
# Extract the file name from the selected file path
file_name = file_path.split('/')[-1].split('.')[0]
time_ranges = [
('10:36', '10:48')
]
# Open the MP3 file
audio = AudioFileClip(file_path)
# Open file dialog to select the destination folder for extracted segments
folder_path = filedialog.askdirectory()
# Process each time range
for i, (start_time_str, end_time_str) in enumerate(time_ranges, 1):
# Convert start and end time strings to datetime objects
start_time = datetime.datetime.strptime(start_time_str, '%M:%S')
end_time = datetime.datetime.strptime(end_time_str, '%M:%S')
# Calculate start and end times in seconds
start_time_sec = start_time.minute * 60 + start_time.second
end_time_sec = end_time.minute * 60 + end_time.second
# Check if the end time exceeds the duration of the audio clip
if end_time_sec > audio.duration:
print(f"Invalid time range for segment {i}. Skipping.")
continue
# Cut the audio segment
extract = audio.subclip(start_time_sec, end_time_sec)
# Generate the export file name based on the iteration number
export_file_name = f"{i}.mp3"
# Get the full path for the destination file
destination_path = os.path.join(folder_path, export_file_name)
# Export the extracted segment as MP3 to the specified destination folder
extract.write_audiofile(destination_path, codec="mp3")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment