else/Toolbox/ffrename.py
Ethan Dalool 6e64483523 else
2016-12-12 19:53:21 -08:00

46 lines
1.5 KiB
Python

import converter
import glob
import os
import re
import subprocess
import sys
import time
def main(filename):
ffmpeg = converter.Converter()
probe = ffmpeg.probe(filename)
new_name = filename
if '_x_' in filename:
dimensions = '%dx%d' % (probe.video.video_width, probe.video.video_height)
new_name = new_name.replace('_x_', dimensions)
if '___' in filename:
video_codec = probe.video.codec
audios = [stream for stream in probe.streams if stream.type == 'audio' and stream.bitrate]
if audios:
audio = max(audios, key=lambda x: x.bitrate)
audio_codec = probe.audio.codec
else:
audio_codec = None
if any(not x for x in [video_codec, probe.video.bitrate, audio_codec, probe.audio.bitrate]):
print('Could not identify media info')
else:
video_bitrate = probe.video.bitrate // 1000
audio_bitrate = probe.audio.bitrate // 1000
video = '%s-%d' % (video_codec, video_bitrate)
audio = '%s-%d' % (audio_codec, audio_bitrate)
video = video.upper()
audio = audio.upper()
video = video.replace('H264', 'h264')
video = video.replace('HEVC', 'h265')
info = '{v}, {a}'.format(v=video, a=audio)
new_name = new_name.replace('___', info)
print(new_name)
if input('Okay?').lower() in ['y', 'yes']:
os.rename(filename, new_name)
if __name__ == '__main__':
for filename in glob.glob(sys.argv[1]):
main(filename)