You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.3 KiB
62 lines
2.3 KiB
9 months ago
|
from glob import glob
|
||
|
import os
|
||
|
import subprocess
|
||
|
|
||
|
|
||
|
base_folder = f'C:{os.sep}Users{os.sep}Zoltan{os.sep}Videos{os.sep}'
|
||
|
save_file = "converted.txt"
|
||
|
|
||
|
def get_video_length(filename):
|
||
|
result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
|
||
|
"format=duration", "-of",
|
||
|
"default=noprint_wrappers=1:nokey=1", filename],
|
||
|
stdout=subprocess.PIPE,
|
||
|
stderr=subprocess.STDOUT)
|
||
|
return float(result.stdout)
|
||
|
|
||
|
def create_temp_file_name(split):
|
||
|
return f'{split[0]}{os.sep}TEMP_{split[1]}'
|
||
|
|
||
|
|
||
|
|
||
|
file_names = glob(f'{base_folder}*{os.sep}*.mp4')
|
||
|
|
||
|
f = open(save_file, "r+")
|
||
|
converted_files = [line.rstrip(os.linesep) for line in f]
|
||
|
for file_name in file_names:
|
||
|
if(file_name not in converted_files):
|
||
|
split = file_name.rsplit(os.sep,1)
|
||
|
temp_name = create_temp_file_name(split)
|
||
|
print(f'Convert started: {file_name}')
|
||
|
os.system(f'ffmpeg -v quiet -stats -hwaccel cuda -i \"{file_name}\" -preset veryfast -c:v libx264 -crf 18 -c:a copy \"{temp_name}\"')
|
||
|
os.remove(file_name)
|
||
|
os.rename(temp_name,f'{split[0]}{os.sep}{split[1]}')
|
||
|
f.write(file_name+os.linesep)
|
||
|
|
||
|
clips = glob(f'{base_folder}*.mp4')
|
||
|
for clip in clips:
|
||
|
if(clip not in converted_files):
|
||
|
split = clip.rsplit(os.sep,1)
|
||
|
temp_name = create_temp_file_name(split)
|
||
|
file_stats = os.stat(clip)
|
||
|
duration = get_video_length(clip)
|
||
|
tries = 0
|
||
|
under_25 = False
|
||
|
while not under_25:
|
||
|
bitrate = ( 24 * 8192.0 ) / ( 1.048576 * duration ) * (1- tries*0.05)
|
||
|
|
||
|
print(f'Duration: {duration} Bitrate: {bitrate} Tries: {tries+1}')
|
||
|
print(f'Convert started: {clip}')
|
||
|
|
||
|
os.system(f'ffmpeg -v quiet -stats -i \"{clip}\" -c:v libx264 -preset veryfast -b {bitrate:.0f}k -c:a copy \"{temp_name}\"')
|
||
|
|
||
|
temp_file_stat = os.stat(temp_name)
|
||
|
print(f'file size stats: {temp_file_stat.st_size}, ------> {(temp_file_stat.st_size / (1024 * 1024))}')
|
||
|
if(25>(temp_file_stat.st_size / (1024 * 1024))):
|
||
|
under_25 = True
|
||
|
os.remove(clip)
|
||
|
os.rename(temp_name,f'{split[0]}{os.sep}{split[1]}')
|
||
|
else:
|
||
|
tries +=1
|
||
|
os.remove(temp_name)
|
||
|
f.write(clip+os.linesep)
|