其實這是第一篇的延伸應用,一些下載下來的歌曲是大串燒的歌曲,時間長度以小時來計算,並且包含許多歌曲在裡面,而這篇主要是將該歌曲做分割。
當然,我們分割的首要條件是必須要有song list,我們建立在txt文字檔中就行,格式範例如下:(請注意,有的作者提供的時間軸沒有一開始的小時的時間只有00:00,請自己改成0:00:00)
0:00:00 歌曲1的檔名
0:04:02 歌曲2的檔名
0:09:26 歌曲3的檔名
0:13:52 歌曲4的檔名
0:18:30 歌曲5的檔名
- 先選擇我們的歌曲

- 接著選擇輸出的資料夾

- 最後選擇我們的song list時間軸就行了

- 以下為AI生成的學習程式:
import os
import subprocess
import tkinter as tk
from tkinter import filedialog, messagebox
# ===== tkinter 初始化(不顯示主視窗)=====
root = tk.Tk()
root.withdraw()
# ===== 選擇要分割的 MP3 檔案 =====
input_mp3 = filedialog.askopenfilename(
title="選擇要分割的 MP3",
filetypes=[("MP3 檔案", "*.mp3")]
)
if not input_mp3:
messagebox.showwarning("取消", "未選擇 MP3,程式結束")
exit()
# ===== 選擇輸出資料夾(可新建)=====
output_dir = filedialog.askdirectory(
title="選擇輸出資料夾(可自行新建)"
)
if not output_dir:
messagebox.showwarning("取消", "未選擇資料夾,程式結束")
exit()
os.makedirs(output_dir, exist_ok=True)
# ===== 選擇歌單檔案(song list.txt)=====
tracklist_file = filedialog.askopenfilename(
title="選擇歌單檔案 (song list.txt)",
filetypes=[("文字檔", "*.txt")]
)
if not tracklist_file:
messagebox.showwarning("取消", "未選擇歌單,程式結束")
exit()
# ===== 讀取歌單 =====
tracks = []
with open(tracklist_file, "r", encoding="utf-8") as f:
for line in f:
if line.strip():
# 假設歌單格式: 00:01:23 Song Title
parts = line.strip().split(" ", 1)
if len(parts) == 2:
start_time, title = parts
tracks.append((start_time, title))
if not tracks:
messagebox.showerror("錯誤", "歌單檔案沒有有效的資料")
exit()
# ===== 將時間轉成秒數 =====
def time_to_seconds(t):
# 支援 hh:mm:ss 或 mm:ss 格式
parts = t.split(":")
if len(parts) == 3:
h, m, s = parts
elif len(parts) == 2:
h = 0
m, s = parts
else:
raise ValueError(f"時間格式錯誤: {t}")
return int(h)*3600 + int(m)*60 + int(s)
# ===== 逐一分割 MP3 =====
for i, (start_time, title) in enumerate(tracks):
start_sec = time_to_seconds(start_time)
if i + 1 < len(tracks):
end_sec = time_to_seconds(tracks[i+1][0])
duration = end_sec - start_sec
cmd = [
"ffmpeg",
"-y",
"-i", input_mp3,
"-ss", start_time,
"-t", str(duration),
"-acodec", "copy",
os.path.join(output_dir, f"{i+1:02d}_{title}.mp3")
]
else:
# 最後一首,切到結尾
cmd = [
"ffmpeg",
"-y",
"-i", input_mp3,
"-ss", start_time,
"-acodec", "copy",
os.path.join(output_dir, f"{i+1:02d}_{title}.mp3")
]
subprocess.run(cmd)
messagebox.showinfo("完成", f"MP3 分割完成!\n共 {len(tracks)} 首歌曲\n輸出資料夾: {output_dir}") - 或在此下載.py檔
https://drive.google.com/file/d/1bClOQf95g9xRWeEXd0GVuIzpmTMzY4ga/view?usp=sharing














