From ae0f022880c2a19264442989fe3d2ac35124cdbc Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Thu, 2 Sep 2021 13:19:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auto_convert_flv_to_mp4.py | 11 ++++ auto_merge_video.py | 129 +++++++++++++++++++++++++++++++++++++ auto_split_video.py | 55 ++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 auto_convert_flv_to_mp4.py create mode 100644 auto_merge_video.py create mode 100644 auto_split_video.py diff --git a/auto_convert_flv_to_mp4.py b/auto_convert_flv_to_mp4.py new file mode 100644 index 0000000..848492f --- /dev/null +++ b/auto_convert_flv_to_mp4.py @@ -0,0 +1,11 @@ +from glob import glob +import os + + +for file in glob("*.flv"): + new_file = file.replace(".flv", ".mp4") + os.system(" ".join([ + "ffmpeg", "-i", file, + "-c copy", "-f mp4", + new_file, + ])) diff --git a/auto_merge_video.py b/auto_merge_video.py new file mode 100644 index 0000000..edec02e --- /dev/null +++ b/auto_merge_video.py @@ -0,0 +1,129 @@ +import os +import subprocess +import re +import copy +import time + +template_video_part = { + "filename": "", + "file_ext": "flv", + "start": "", + "end": "", + "part_start": "", + "duration": "", +} +template_current_video = { + "parts": [], + "pack_ext": "mp4", + "out_ext": "mp4", + "suffix": "", + "target_name": "", +} + + +def merge_video_parts_and_split(video): + if len(video["parts"]) <= 1: + raise Exception("视频片段不够") + prev_part = video["parts"].pop(0) + if prev_part["part_start"] is None: + raise Exception("视频片段part_start读取异常") + ts_part = [] + for part in video["parts"]: + if part["part_start"] is None: + raise Exception("视频片段part_start读取异常") + if prev_part["duration"] == "": + duration = float(part["part_start"]) - float(prev_part["part_start"]) + if duration <= 60: + raise Exception("视频片段duration过小") + prev_part["duration"] = str(duration) + new_filename = str(time.time()) + os.system(" ".join([ + "ffmpeg", "-y", "-i", "{filename}.{file_ext}".format_map(prev_part), + "-c copy", "-f mpegts", "-t {duration}".format_map(prev_part), + "{}.ts".format(new_filename) + ])) + os.system(" ".join([ + "ffmpeg", "-y", "-i", "{}.ts".format(new_filename), + "-c copy", "-f mpegts", "-ss {start}".format_map(prev_part) if prev_part["start"] != "" else "", + "-to {end}".format_map(prev_part) if prev_part["end"] != "" else "", + "{filename}.ts".format_map(prev_part) + ])) + os.remove("{}.ts".format(new_filename)) + ts_part.append("{filename}.ts".format_map(prev_part)) + prev_part = part + os.system(" ".join([ + "ffmpeg", "-y", "-i", "{filename}.{file_ext}".format_map(prev_part), + "-c copy", "-f mpegts", "-ss {start}".format_map(prev_part) if prev_part["start"] != "" else "", + "-to {end}".format_map(prev_part) if prev_part["end"] != "" else "", + "{filename}.ts".format_map(prev_part) + ])) + ts_part.append("{filename}.ts".format_map(prev_part)) + os.system(" ".join([ + "ffmpeg", "-y", "-i", "\"concat:{}\"".format("|".join(ts_part)), + "-c copy", "-f {pack_ext}".format_map(video), + "{target_name}{suffix}.{out_ext}".format_map(video), + ])) + for _delete in ts_part: + os.remove(_delete) + + +def get_video_part_start(file): + s = subprocess.Popen([ + "ffprobe", file + ], bufsize=0, stderr=subprocess.PIPE, universal_newlines=True) + while True: + line = s.stderr.readline() + if line == "" and not s.poll(): + break + line = line.strip() + match = re.search(r", start: (\d+\.\d+)", line) + if match: + return match.group(1) + + +with open("../a.txt", "r", encoding="gbk") as f: + skip_current = False + current_video = copy.deepcopy(template_current_video) + for current_line in f.readlines(): + current_line = current_line.strip() + if current_line == "": + if skip_current: + skip_current = False + else: + merge_video_parts_and_split(current_video) + current_video = copy.deepcopy(template_current_video) + continue + if current_line.startswith("=" * 5): + break + if "-" in current_line: + skip_current = True + continue + if "~" in current_line: + file_params = current_line.split("~") + for file_param in file_params: + video_part = copy.deepcopy(template_video_part) + bias_time = "" + if "+" in file_param: + filename, bias_time = file_param.split("+", 1) + else: + filename = file_param + if "." in filename: + filename, file_ext = filename.split(".", 1) + video_part['file_ext'] = file_ext + video_part["filename"] = filename + if current_video["suffix"] == "": + suffix, _ = filename.split("_") + current_video['suffix'] = "_" + suffix[4:] + if len(current_video["parts"]) == 0: + video_part['start'] = bias_time + else: + video_part['end'] = bias_time + video_part['part_start'] = get_video_part_start("{filename}.{file_ext}".format_map(video_part)) + current_video["parts"].append(video_part) + continue + else: + if len(current_video["parts"]) == 0: + skip_current = True + continue + current_video['target_name'] = current_line + continue diff --git a/auto_split_video.py b/auto_split_video.py new file mode 100644 index 0000000..4dd6339 --- /dev/null +++ b/auto_split_video.py @@ -0,0 +1,55 @@ +import os +import copy + +template_current_video = { + "filename": "", + "file_ext": "mp4", + "pack_ext": "mp4", + "out_ext": "mp4", + "start": "", + "end": "", + "suffix": "", + "target_name": "", +} + +current_line = "" +with open("../a.txt", "r", encoding="GB2312") as f: + skip_current = False + current_video_params = copy.deepcopy(template_current_video) + for current_line in f.readlines(): + current_line = current_line.strip() + if current_line == "": + if skip_current: + skip_current = False + else: + os.system(" ".join([ + "ffmpeg", "-y", "-i", "{filename}.{file_ext}".format_map(current_video_params), + "-c copy", "-f {pack_ext}".format_map(current_video_params), + "-ss","{start}".format_map(current_video_params), + "-to","{end}".format_map(current_video_params), + "{target_name}{suffix}.{out_ext}".format_map(current_video_params), + ])) + current_video_params = copy.deepcopy(template_current_video) + continue + if current_line.startswith("=" * 5): + break + if "~" in current_line: + skip_current = True + continue + if "+" in current_line and "-" in current_line: + filename, time_series = current_line.split("+", 1) + if "." in filename: + filename, file_ext = filename.split(".", 1) + current_video_params['file_ext'] = file_ext + current_video_params['filename'] = filename + suffix, _ = filename.split("_") + current_video_params['suffix'] = "_"+suffix[4:] + start, end = time_series.split("-", 1) + current_video_params['start'] = start + current_video_params['end'] = end + else: + if current_video_params['filename'] == "": + skip_current = True + continue + current_video_params['target_name'] = current_line + continue