使用python对视频文件分辨率进行分组的实例代码
发布日期:2021-12-24 09:52 | 文章来源:源码中国
在平时的工作中,我们的目录有很多的视频文件,如果你没有一个好的视频分类习惯,在找视频素材的时候会很费时,通过对视频的分辨路进行分类可以在需要的时候快速找到你想要的视频分辨率。当然人工去分类是一种比较费时费力的工作,通过软件也好,程序也罢都是为了可以提高我们的工作效率。
代码分享
import os import subprocess import json import shutil import datetime def get_files(file_dir): for root, dirs, files in os.walk(file_dir): if len(files) > 0: # 获取图片路径 for f in files: if f.endswith(".mp4"): p = os.path.join(root, f) h, w, t = get_video_info(p) new_dir = os.path.realpath("{}\{}x{}".format(file_dir, h, w)) if not os.path.exists(new_dir):os.makedirs(new_dir) shutil.move(p, os.path.join(new_dir, "{}.mp4".format(t))) def get_video_info(file_path): cmd = "ffprobe -v quiet -print_format json -show_streams -i {}".format( file_path) with open('output.json', 'w') as f: subprocess.call(cmd, stdout=f) with open('output.json', 'r') as f: streams = json.load(f) for i in streams["streams"]: if i['codec_type'] == "video": print(file_path) t2 = "" try: t1 = datetime.datetime.strptime(i['tags']['creation_time'], "%Y-%m-%dT%H:%M:%S.%f%z") t2 = datetime.datetime.strftime(t1, '%Y%m%d%H%M%S') except KeyError: t2 = datetime.datetime.now().strftime('%Y%m%d%H%M%S') return i['height'], i['width'], t2 else: continue if __name__ == "__main__": file_dir = input("dir:") get_files(file_dir)
代码使用了ffprobe
获取视频信息
原文:http://www.rencaixiu.cn/archives/811/
到此这篇关于使用python对视频文件分辨率进行分组的文章就介绍到这了,更多相关python视频文件分辨率分组内容请搜索本站以前的文章或继续浏览下面的相关文章希望大家以后多多支持本站!
版权声明:本站文章来源标注为YINGSOO的内容版权均为本站所有,欢迎引用、转载,请保持原文完整并注明来源及原文链接。禁止复制或仿造本网站,禁止在非www.yingsoo.com所属的服务器上建立镜像,否则将依法追究法律责任。本站部分内容来源于网友推荐、互联网收集整理而来,仅供学习参考,不代表本站立场,如有内容涉嫌侵权,请联系alex-e#qq.com处理。
相关文章