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)
相关推荐
御形封灵29 分钟前
基于原生table实现单元格合并、增删
开发语言·javascript·ecmascript
ney1878190247433 分钟前
分类网络LeNet + FashionMNIST 准确率92.9%
python·深度学习·分类
应茶茶1 小时前
从 C 到 C++:详解不定参数的两种实现方式(va_args 与参数包)
c语言·开发语言·c++
Data_agent1 小时前
1688获得1688店铺列表API,python请求示例
开发语言·python·算法
2401_871260021 小时前
Java学习笔记(二)面向对象
java·python·学习
2301_764441332 小时前
使用python构建的应急物资代储博弈模型
开发语言·python·算法
丿BAIKAL巛2 小时前
Java前后端传参与接收全解析
java·开发语言
code bean2 小时前
【C++】Scoop 包管理器与 MinGW 工具链详解
开发语言·c++
yanghuashuiyue2 小时前
Java过滤器-拦截器-AOP-Controller
java·开发语言
喏喏心2 小时前
深度强化学习:价值迭代与Bellman方程实践
人工智能·python·学习·机器学习