diff --git a/quick_split_video.py b/quick_split_video.py new file mode 100644 index 0000000..ceb80c7 --- /dev/null +++ b/quick_split_video.py @@ -0,0 +1,53 @@ +import os +import sys +import re +import subprocess + +from datetime import datetime, timedelta + +EACH_SEC = 6000 +OVERFLOW_SEC = 4 + + +def get_video_real_duration(filename): + ffmpeg_process = subprocess.Popen([ + "ffmpeg", "-i", filename, "-c", "copy", "-f", "null", "-" + ], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + result = "0:0:0.0" + for line in ffmpeg_process.stderr.readlines(): + match_result = re.findall("(?<= time=).+?(?= )", line.decode()) + if len(match_result) == 0: + continue + result = match_result.pop() + return result + + +if __name__ == '__main__': + if len(sys.argv) < 2: + print("请输入文件") + exit(1) + input_file = sys.argv[1] + if not os.path.isfile(input_file): + raise FileNotFoundError(input_file) + file_name = os.path.split(input_file)[-1] + _create_dt = os.path.splitext(file_name)[0] + create_dt = datetime.strptime(_create_dt, "%Y%m%d_%H%M") + print(create_dt) + _duration_str = get_video_real_duration(input_file) + _duration = datetime.strptime(_duration_str, "%H:%M:%S.%f") - datetime(1900, 1, 1) + duration = _duration.seconds + print(duration) + current_sec = 0 + while current_sec < duration: + current_dt = (create_dt + timedelta(seconds=current_sec)).strftime("%Y%m%d_%H%M") + print("CUR_DT", current_dt) + print("BIAS_T", current_sec) + split_process = subprocess.Popen([ + "ffmpeg", "-y", "-i", input_file, + "-c", "copy", "-f", "mp4", + "-t", str(EACH_SEC + OVERFLOW_SEC), + "-ss", str(current_sec), + "{}.mp4".format(current_dt) + ]) + split_process.wait() + current_sec += EACH_SEC