Python script: mp3 file split

自用代码备份

python 复制代码
import sys
import re
import os

patterns = [
    # 00:00:00 - xxxxx 
    re.compile(r"(\d{2}:\d{2}:\d{2})\s+-\s+([^\n\r]*)"), 
    # 00:00 xxxxx 
    re.compile(r"(\d{1,2}:\d{2})\s+([^\n\r]*)")]

if len(sys.argv) < 3:
    print(f'''Usage: pythonScriptFilename.py listTextFileName audioBinaryFileName [last-to]
          Example: python script.py music.lst music.mp3 01:00:23
          --{"by mostone"}{"@"}hotmail{"."}com''')
    quit()

listFileName = sys.argv[1]
inputFileName = sys.argv[2]
lastToTime = "" if len(sys.argv)==3 else f"-to {sys.argv[3]}"

lines = [] # [ss, to, outputFileName]
print("open list file")
with open(listFileName, "rb") as lst:
    print("read to buff")
    txt = lst.read().decode()

    for ptn in patterns:
        matches = ptn.findall(txt)
        if len(matches) != 0: break

    if len(matches) == 0:
        print("No item found in the list file.")
        quit()

    ss = ""
    fn = ""
    for m in matches:
        if ss != "":
            lines.append([f"-ss {ss}", f"-to {m[0]}", fn])

        ss = m[0]
        fn = m[1]

    # last one
    lines.append([f"-ss {ss}", lastToTime, fn])

ptnIndex = re.compile(r"^\d+\.")
addIndex = True if ptnIndex.match(lines[0][2])==None else False
for i in range(1, len(lines)):
    paras = lines[i - 1]
    paras[2] = f"\"{f"{i:02g}.{paras[2]}" if addIndex else paras[2]}\".mp3"
    cmd = f"ffmpeg -nostdin -i \"{inputFileName}\" -c copy {" ".join(paras)}"
    # print(cmd)
    os.system(cmd)
相关推荐
就不掉头发6 分钟前
C++函数模板
java·开发语言·c++
梦想三三11 分钟前
Python从零实现AI Agent电商客服(Qwen/Ollama+SQLite源码解析)
人工智能·python·sqlite
鹿鹿学长14 分钟前
国赛工业应用数学题:从31省规上工业数据到生产排程,四类高频赛题的破题打法
python·自动化
笨鸟先飞,勤能补拙19 分钟前
密码学深度指南 — SecOps 工程师实战手册
人工智能·vscode·python·安全·github·密码学·visual studio
淼澄研学22 分钟前
PyTorch核心实操:从张量计算到混合精度训练的5个关键步骤
人工智能·pytorch·python
我是苏苏23 分钟前
WPF开发01:基础控件及常用模板篇
开发语言·c#·html·wpf
wuyk55523 分钟前
89.嵌入式内存管理的“稳定利器”:内存池原理与C语言实现
c语言·开发语言·stm32·单片机·嵌入式硬件
梦想三三27 分钟前
Qwen Function Calling实战:重构电商客服AI Agent
人工智能·python·langchain·大模型·rag
05664630 分钟前
agent学习Day23——文件上传与 Embedding 语义检索
python·学习·embedding
leisoo809731 分钟前
筹码分布数据分析实战:用Python构建主力建仓成本分析系统
python·数据挖掘·数据分析