Created
July 5, 2018 23:40
-
-
Save Matthew238/652b64e1eb2fbf0ab6a00dd80f17d455 to your computer and use it in GitHub Desktop.
指定されたディレクトリツリー中の全ディレクトリの全mp4ファイルの属性をCSV形式で標準出力するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import shutil | |
import sys | |
import os | |
import subprocess | |
import json | |
import math | |
import re | |
if shutil.which('ffprobe') == None: | |
sys.exit('Not found ffprobe command.') | |
def get_file_paths_recursively(path, ext_list): | |
for root, dirs, files in os.walk(path): | |
for ext in ext_list: | |
# for file in [file for file in files if ext in file]: | |
# 上記の方法ではmp4とmp4hogeは同じ拡張子とされてしまう | |
for file in [file for file in files if ext == file.rsplit(".")[1]]: | |
yield os.path.join(root, file) | |
# Pythonのroundは偶数への丸めになるので独自四捨五入関数を用いる | |
def my_round(val, digit=0): | |
p = 10 ** digit | |
return (val * p * 2 + 1) // 2 / p | |
# mp4以外の形式の動画ファイルを扱いたい場合は次のリストで指定しておく | |
EXT_LIST = ['mp4'] # ['mpg', 'ts', 'mp4'] | |
CMD = 'ffprobe -loglevel quiet -print_format json -show_streams ' | |
KEYS = { | |
'video': [ | |
'codec_name', 'width', 'height', 'sample_aspect_ratio', | |
'display_aspect_ratio', 'pix_fmt', 'r_frame_rate', 'duration', | |
'bit_rate' | |
], | |
'audio': [ | |
'codec_name', 'sample_rate', 'bit_rate' | |
] | |
} | |
# ディレクトリも加えたいときはdirを加えればよい。 | |
#print('dir,file,video codec,width,height,sample aspect,display aspect,pix fmt,'\ | |
print('file,video codec,width,height,sample aspect,display aspect,pix fmt,'\ | |
'frame rate,duration,video bit rate,audio codec,sample rate,audio bit rate') | |
for file_path in get_file_paths_recursively(sys.argv[1], EXT_LIST): | |
abs_file_path = os.path.abspath(file_path) | |
dir_name = os.path.dirname(abs_file_path) | |
# パスの末ディレクトリが2018のような西暦を示す4桁の数字ディレクトリだけを | |
# 対象にしたいときはコメントアウトするとよい。 | |
# if not re.match(r"^\d{4}$", dir_name.rsplit("/",1)[1]): | |
# continue | |
base_name = os.path.basename(abs_file_path) | |
cmd = CMD + '-i "%s"' % abs_file_path | |
result = subprocess.run(cmd, shell = True, stdout = subprocess.PIPE) | |
streams = json.loads(result.stdout)['streams'] | |
# ディレクトリも加えたいときはdir_nameを加えればよい。 | |
# elements = [dir_name, base_name] | |
elements = [base_name] | |
for i in ['video', 'audio']: | |
for stream in streams: | |
if stream['codec_type'] == i: | |
for key in KEYS[stream['codec_type']]: | |
v = stream[key] | |
if i == 'video': | |
if key == 'r_frame_rate': | |
v = my_round(eval(stream[key]), 3) | |
elif key == 'duration': | |
v = int(my_round(float(stream[key])/60.0, 0)) | |
elif key == 'bit_rate': | |
v = int(my_round(float(stream[key])/1000.0, 1)) | |
elif i == 'audio': | |
if key == 'sample_rate': | |
v = my_round(float(stream[key])/1000.0, 2) | |
if key == 'bit_rate': | |
v = int(my_round(float(stream[key])/1000.0, 0)) | |
elements.append(v) | |
print(','.join(map(lambda i: str(i), elements))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment