使用python对视频文件分辨率进行分组的实例代码

通过对视频的分辨路进行分类可以在需要的时候快速找到你想要的视频分辨率。当然人工去分类是一种比较费时费力的工作,通过软件也好,程序也罢都是为了可以提高我们的工作效率。下面通过代码给大家分享使用python对视频文件分辨率进行分组的方法,一起看看吧

在平时的工作中,我们的目录有很多的视频文件,如果你没有一个好的视频分类习惯,在找视频素材的时候会很费时,通过对视频的分辨路进行分类可以在需要的时候快速找到你想要的视频分辨率。当然人工去分类是一种比较费时费力的工作,通过软件也好,程序也罢都是为了可以提高我们的工作效率。

代码分享

 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视频文件分辨率分组内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是使用python对视频文件分辨率进行分组的实例代码的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python