else/Toolbox/ffrename.py

47 lines
1.5 KiB
Python
Raw Normal View History

2016-11-18 06:24:52 +00:00
import converter
2016-12-13 03:53:21 +00:00
import glob
2016-11-18 06:24:52 +00:00
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
2016-12-13 03:53:21 +00:00
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
2016-11-18 06:24:52 +00:00
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__':
2016-12-13 03:53:21 +00:00
for filename in glob.glob(sys.argv[1]):
main(filename)