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)
相关推荐
Ticnix9 分钟前
RAG 的坑不在检索,在"对齐":pgvector 实战手记
python·agent·全栈
kyrie_sakura9 分钟前
python学习笔记14 -- FastAPI
笔记·python·学习·fastapi
雪落漂泊20 分钟前
C++11(上)
开发语言·c++
春涧草茶42 分钟前
慢就是快12-2三种访问权限|getter与setter|魔法方法
开发语言·python
無限進步D1 小时前
Java Web 前端 简介
java·开发语言·前端·css·html·css3·web
夜雪一千1 小时前
如何使用Python做舆情分析
人工智能·python·数据分析
qq_401700411 小时前
Qt QUrl 详解与代码示例
开发语言·数据库·qt
小灰灰搞电子2 小时前
Rust Attribute(属性标记)完整整理
开发语言·后端·rust
Ticnix2 小时前
MCP 工具拿不到 user_id?用 contextvars 做请求级用户隔离
python·agent·mcp
gb42152872 小时前
WPS 里的 Word快速合成照片
python