在平時的工作中,我們的目錄有很多的視頻文件,如果你沒有一個好的視頻分類習慣,在找視頻素材的時候會很費時,通過對視頻的分辨路進行分類可以在需要的時候快速找到你想要的視頻分辨率。當然人工去分類是一種比較費時費力的工作,通過軟件也好,程序也罷都是為了可以提高我們的工作效率。
代碼分享
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
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視頻文件分辨率分組內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/gzwawj/p/15419078.html