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)
相关推荐
Highcharts.js2 小时前
倒置百分比堆叠面积图表示列详解|Highcharts大气成分图表代码
开发语言·信息可视化·highcharts·图表开发·面积图·图表示例·推叠图
Li emily2 小时前
解决了加密货币api多币种订阅时的数据乱序问题
人工智能·python·api·fastapi
csdn_aspnet3 小时前
C语言 Lomuto分区算法(Lomuto Partition Algorithm)
c语言·开发语言·算法
晨曦中的暮雨3 小时前
4.15腾讯 CSIG云服务产线 一面
java·开发语言
2301_781571423 小时前
Golang格式化输出占位符都有什么_Golang fmt占位符教程【通俗】
jvm·数据库·python
存在morning3 小时前
【GO语言开发实践】二 GO 并发快速上手
大数据·开发语言·golang
asdzx673 小时前
使用 Python 为 PDF 添加页码 (详细教程)
python·pdf·页码
AI技术控3 小时前
《Transformers are Inherently Succinct》论文解读:从“能表达什么”到“多紧凑地表达”
人工智能·python·深度学习·机器学习·自然语言处理
xiaoerbuyu12334 小时前
开源Java 邮箱 基于SpringBoot+Vue前后端分离的电子邮件
java·开发语言
sparEE5 小时前
c++值类别、右值引用和移动语义
开发语言·c++