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)
相关推荐
weixin_4462608536 分钟前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask
我的xiaodoujiao1 小时前
API 接口自动化测试详细图文教程学习系列32--Allure测试报告2
python·学习·测试工具·pytest
人邮异步社区1 小时前
怎么把C语言学到精通?
c语言·开发语言
qetfw1 小时前
MXU:Tauri 2 + React 的 MaaFramework 跨平台 GUI 源码
前端·python·react.js·前端框架·开源项目·效率工具
心平气和量大福大2 小时前
C#-WPF-控件-TextBox 数据绑定
开发语言·c#·wpf
ttwuai2 小时前
Cursor 生成 CRUD 后,Go 后台接口别只测 200:JWT、RBAC 和 tenant_id 怎么验
开发语言·后端·golang
用户8356290780512 小时前
Python 实现 Excel 页面布局与打印设置自动化
后端·python
এ慕ོ冬℘゜2 小时前
前端基础:什么是时间戳?JS获取时间戳三种方法与实战用途
开发语言·前端·javascript
执明wa2 小时前
LayoutInflater详解: XML是如何变成View的?
android·xml·开发语言·android studio
一次旅行2 小时前
Python+大模型端到端自动化日报系统
开发语言·python·自动化