利用Python脚本自动按分辨率整理视频文件
本文介绍如何使用Python脚本自动对视频文件按分辨率进行分类整理。通过调用ffprobe获取视频元数据,程序可自动创建以宽高命名的目录并将视频移入其中,从而解决视频素材杂乱难找的问题,显著提升工作效率。
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视频处理内容,建议查阅本站往期文章或继续浏览后续内容。感谢大家的支持!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













