网络编程
位置:首页>> 网络编程>> Python编程>> 使用python对视频文件分辨率进行分组的实例代码

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

作者:gzwawj  发布时间:2022-06-06 21:16:44 

标签: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/

来源:https://www.cnblogs.com/gzwawj/p/15419078.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com