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)
相关推荐
维吉斯蔡7 小时前
【VS Code / Cursor】文件夹右键快捷打开与文件类型自动关联
开发语言·程序人生·学习方法
幻影123!7 小时前
从零训练一个会下五子棋的AI
python·深度学习·神经网络·强化学习·五子棋·alpha zero·mokugo
Python私教9 小时前
Django 接入 AI 大模型实战:从零做一个流式聊天网站
人工智能·python·django
Python私教9 小时前
Django 接入 MCP 实战:让 AI 安全调用数据库和业务接口
人工智能·python·django
Python私教9 小时前
Django 搭建 AI 本地知识库:文档上传、向量检索与智能问答
人工智能·python·django
luj_17689 小时前
星火科技助力边远地区防病攻坚
c语言·开发语言·c++·经验分享·算法
always_TT9 小时前
【Python 日志记录:logging 模块入门】
开发语言·python·php
xcLeigh10 小时前
Go入门:变量声明的五种方式详解
java·开发语言·golang
zmzb010310 小时前
C++课后习题训练记录Day175
开发语言·c++
脱胎换骨-军哥11 小时前
C++ 代码规范与格式化指南
开发语言·c++·代码规范