【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获取视频信息

标签

发表评论