-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathhandbrake_converter.py
59 lines (46 loc) · 2.35 KB
/
handbrake_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import subprocess
import os
import time
import logging
import sys
from shutil import copyfile
from pathlib import Path
master_start_time = time.time()
handbrake_cli_exe = r"C:\Users\franp\Downloads\HandBrakeCLI-1.3.3-win-x86_64\HandBrakeCLI.exe"
root_video_directory = Path(r"path to directory containing input videos")
root_video_output = Path(r"path to output directory")
# noinspection PyArgumentList
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler(r"D:\export_log.log"),
logging.StreamHandler(sys.stdout)
]
)
for path, directories, files in os.walk(root_video_directory):
new_folder = root_video_output.joinpath(*Path(path).parts[1:])
logging.info(f"Creating folder for {new_folder}")
Path(new_folder).mkdir(parents=True, exist_ok=True)
for file in files:
if file.endswith(".avi"):
avi_file = os.path.join(path, file)
logging.info(f"Detected .avi file: {avi_file}")
output_file = str(root_video_output.joinpath(*Path(avi_file).parts[1:])).replace(".avi", ".mp4")
if Path(output_file).exists():
logging.info("deteced mp4 already converted")
continue
else:
handbrake_command = [handbrake_cli_exe, '-i', f'{avi_file}',"-o", output_file, "-e", "x264", "-q", "20", "-B", "160"]
logging.info(f"Converting: {avi_file} to .MP4 with x264. Output MP4: {output_file}")
start_time = time.time()
process = subprocess.Popen(handbrake_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
for line in process.stdout:
print(line)
logging.info("Done")
logging.info('Program took {} seconds to complete..\n'.format(time.time() - start_time))
else:
non_avi_file = os.path.join(path, file)
output_file = str(root_video_output.joinpath(*Path(non_avi_file).parts[1:]))
logging.info(f"Detected Non .avi file. Copying {non_avi_file} to {output_file}")
copyfile(non_avi_file, output_file)
logging.info("Done")
logging.info('Program took {} seconds to complete.'.format(time.time() - master_start_time))