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)
相关推荐
qq_404265836 分钟前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
qq_4523962314 分钟前
【Python × AI】Prompt Engineering 深度工程化:打造大模型的“确定性”控制链路
人工智能·python·ai·prompt
皙然15 分钟前
深入理解 Java HashSet
java·开发语言
Ralph_Y15 分钟前
C++:static
开发语言·c++
Bright Data16 分钟前
Pinterest 数据集示例
后端·python·flask
佩奇大王37 分钟前
P2408 特殊日期
java·开发语言
YMH.40 分钟前
Day3.14c++
开发语言·c++
威联通网络存储43 分钟前
某新能源汽车研发中心:基于威联通的全闪存数据治理实践
python·汽车