From ddd13510a8d12a1580bc2d842bdfbcf7209a92db Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Tue, 19 Mar 2024 09:37:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/merge.py | 65 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/test/merge.py b/test/merge.py index 9e63bf4..8552858 100644 --- a/test/merge.py +++ b/test/merge.py @@ -1,21 +1,66 @@ +import re import subprocess import os +import os.path as path +import sys + +work_path = sys.path[0] ffmpeg_path = "ffmpeg" +ffprobe_path = "ffprobe" -cmd = f"{ffmpeg_path} -y -loglevel error -f concat -safe 0 -i %s -c copy test.mp4" +record_dir = "record" +record_path = path.join(work_path, record_dir) -tmp_merge_file = "merge.tmp" +if not path.exists(record_path): + os.mkdir(record_path) + +merge_file = path.join(work_path, "test.mp4") +cmd = f"{ffmpeg_path} -y -loglevel error -f concat -safe 0 -i %s -c copy {merge_file}" + + +def natural_sort_key(s): + """ + 按文件名中的自然数排序 + """ + # 将字符串按照数字和非数字部分分割,返回分割后的子串列表 + sub_strings = re.split(r'(\d+)', s) + # 如果当前子串由数字组成,则将它转换为整数;否则将其替换成空字符串 + sub_strings = [int(c) if c.isdigit() else '' for c in sub_strings] + # 返回子串列表 + return sub_strings + +file_list = [] +for item in os.listdir(record_path): + p = path.join(record_path, item) + if not path.isfile(p) or (not p.endswith(".mp4") and not p.endswith(".rec")): + continue + else: + file_list.append(p) + +sorted_file_list = sorted(file_list, key=natural_sort_key) + +tmp_merge_file = path.join(work_path, "merge.tmp") with open(tmp_merge_file, mode="w", encoding="utf8") as f: - f.write("file './72439149X00E26C681B09_20240311000015_20240311000030.mp4'\n") - # f.write("file ./72439149X00E26C681B09_20240311001015_20240311001030.mp4") - + for record in sorted_file_list: + f.write(f"file '{record}'\n") + proc = subprocess.Popen( - cmd % tmp_merge_file, - stdin=subprocess.PIPE, - stdout=None, - shell=True + cmd % tmp_merge_file, + stdin=None, + stdout=None, + shell=True ) proc.communicate() -os.remove(tmp_merge_file) \ No newline at end of file +os.remove(tmp_merge_file) + +meta_cmd = f"{ffprobe_path} -v error -i {merge_file} -print_format json -show_format -show_streams -pretty > {merge_file}.meta.json" + +proc = subprocess.Popen( + meta_cmd, + stdin=None, + stdout=None, + shell=True +) +proc.communicate() \ No newline at end of file