Created
March 4, 2022 03:09
-
-
Save depfryer/b219a103eb7847ce46e74d257298398a to your computer and use it in GitHub Desktop.
automate handbrake with python work with windows, pretty sure linux too
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
from re import A | |
import subprocess | |
import os | |
import time | |
import logging | |
import sys | |
from shutil import copyfile | |
from pathlib import Path | |
from pydantic import FilePath | |
handbrake_cli_exe = r"C:\Users\XXX\\HandBrakeCLI.exe" | |
path_video_input = r"C:\XXX" # path where you want to scan all under | |
path_video_output = r"D:\XXX" # final path (replicate the folder tree) | |
file_format = 'mpg' | |
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s", | |
handlers=[ | |
logging.FileHandler(r".\export_log.log"), | |
logging.StreamHandler(sys.stdout) | |
] | |
) | |
def main(): | |
for path, directories, files in os.walk(path_video_input): | |
new_path = path.removeprefix(path_video_input) | |
if directories: | |
create_folder_tree(new_path, directories) | |
if not files: | |
continue | |
for file in files: | |
format_file = file.split('.')[-1] | |
if format_file != file_format: | |
continue | |
old_file = str(Path(path).joinpath(file)) | |
new_file = str(get_new_file_path(new_path, file)) | |
handbrake_command = [handbrake_cli_exe, '-i', old_file,"-o", new_file, "-e", "x264", "-q", "20", "-B", "160"] | |
logging.info(f'start conversion from {old_file}') | |
process = subprocess.Popen(handbrake_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) | |
for line in process.stdout: | |
try: | |
iteration = float(line.split(',')[1].split('%')[0]) | |
printProgressBar(iteration, 100) | |
except IndexError as e : | |
pass | |
except ValueError as e : | |
pass | |
logging.info("Done") | |
def create_folder_tree(path, directories): | |
for dire in directories: | |
logging.info(f'create directory {dire} in {path_video_output+path}') | |
Path(path_video_output+path).joinpath(dire).mkdir(exist_ok=True) | |
def get_new_file_path(path, file): | |
filename = '.'.join(file.split('.')[:-1]) +'.mp4' #remove extension | |
FilePath = Path(path_video_output+path).joinpath(filename) | |
return FilePath | |
# Path(path_video_output+path).joinpath('.'.join(file.split('.')[:-1])).mkdir(exist_ok=True) | |
# Print iterations progress | |
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"): | |
""" | |
Call in a loop to create terminal progress bar | |
@params: | |
iteration - Required : current iteration (Int) | |
total - Required : total iterations (Int) | |
prefix - Optional : prefix string (Str) | |
suffix - Optional : suffix string (Str) | |
decimals - Optional : positive number of decimals in percent complete (Int) | |
length - Optional : character length of bar (Int) | |
fill - Optional : bar fill character (Str) | |
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str) | |
""" | |
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total))) | |
filledLength = int(length * iteration // total) | |
bar = fill * filledLength + '-' * (length - filledLength) | |
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd) | |
# Print New Line on Complete | |
if iteration == total: | |
print() | |
if __name__ == '__main__': | |
master_start_time = time.time() | |
main() | |
logging.info(f'Program took {time.time() - master_start_time} seconds to complete.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment