使用python對視頻文件分辨率進行分組的實例代碼
在平時的工作中,我們的目錄有很多的視頻文件,如果你沒有一個好的視頻分類習(xí)慣,在找視頻素材的時候會很費時,通過對視頻的分辨路進行分類可以在需要的時候快速找到你想要的視頻分辨率。當(dāng)然人工去分類是一種比較費時費力的工作,通過軟件也好,程序也罷都是為了可以提高我們的工作效率。
代碼分享
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/
到此這篇關(guān)于使用python對視頻文件分辨率進行分組的文章就介紹到這了,更多相關(guān)python視頻文件分辨率分組內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信