-
-
Save lepig/89d9d3b8f21af8d4048c62e812a2efb2 to your computer and use it in GitHub Desktop.
合并米家摄像头监控视频,生成以天为单位的视频文件。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
from pathlib import Path | |
from loguru import logger | |
def merge_vids(vidlist_file: str, tofile: str): | |
"""执行 ffmpeg 命令合并视频。""" | |
cmd = f"ffmpeg -f concat -safe 0 -i {vidlist_file} -c:v copy -c:a flac -strict -2 {tofile}" | |
subprocess.run(cmd) | |
def merge_dirs(directory: str): | |
"""合并目录下的监控文件,在当前目录生成以天为单位的视频。 | |
directory 结构: | |
directory | |
2021051001 | |
2021051002 | |
2021051003 | |
... | |
即,子目录结构为:年月日时。 | |
""" | |
date_dict = {} | |
for d in Path(directory).iterdir(): | |
if not d.is_dir(): | |
continue | |
date = d.stem[:8] | |
if date not in date_dict: | |
date_dict[date] = [] | |
date_dict[date].append(d) | |
for date, ds in date_dict.items(): | |
videos = [] | |
for d in ds: | |
videos.extend(list(Path(d).glob("*.mp4"))) | |
logger.info(f"{date}, {len(videos)} videos") | |
if not videos: | |
continue | |
videos = sorted(videos, key=lambda f: int(f.stem.split("_")[-1])) | |
videos = [ | |
"file " + str(f.resolve(strict=True)).replace("\\", "/") for f in videos | |
] | |
Path("vidslist.txt").write_text("\n".join(videos), encoding="utf8") | |
merge_vids("vidslist.txt", f"{date}.mp4") | |
if __name__ == "__main__": | |
merge_dirs(r"D:\Mijia\MIJIA_RECORD_VIDEO") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment