I am trying to develop a code that does the following.
- Takes a file from a directory and processes it in Handbrake
- Adds the file creation time from the metadata, and adds it at the beginning of the file name
- Copies this file, encoded and with the new name, into another directory.
So far I have this code, it mostly works, but its not really processing Handbrake, and I am not sure if after handbrake it will keep the date creating time.
My goal with this is, encode videos that are taking too much space to upload them to google photos, I am using a drone to record them and the file names are automated in a way that can conflict in the long term, that's why I am adding timestamp at the beginning.
Here is the code so far....
import os
import subprocess
from datetime import datetime
import shutil
import re
def encode_and_copy_files(source_directory, destination_directory):
# Get the list of files in the source directory
files = os.listdir(source_directory)
# Sort the files based on their modification time in ascending order
files.sort(key=lambda x: os.path.getctime(os.path.join(source_directory, x)))
# Iterate over the sorted files
for index, filename in enumerate(files):
# Skip directories
if os.path.isdir(os.path.join(source_directory, filename)):
continue
# Get current timestamp
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
# Generate a unique identifier as an 8-digit number
unique_id = str(index).zfill(8)
# Split filename and extension
name, extension = os.path.splitext(filename)
# Construct new filename with timestamp and unique identifier
new_filename = f"{timestamp}_{unique_id}_{name}{extension}"
# Get the source and destination paths
source_path = os.path.join(source_directory, filename)
destination_path = os.path.join(destination_directory, new_filename)
# Perform the encoding with Handbrake
encoded_file_path = encode_with_handbrake(source_path)
# Create the destination directory if it doesn't exist
os.makedirs(destination_directory, exist_ok=True)
# Copy the encoded file to the destination directory with the new name
shutil.copy2(encoded_file_path, destination_path)
def encode_with_handbrake(input_file_path):
# Specify the output file path
output_file_path = f"{input_file_path}"
# Normalize the file paths and replace backslashes with forward slashes
input_file_path = os.path.normpath(input_file_path).replace("\\", "/")
output_file_path = os.path.normpath(output_file_path).replace("\\", "/")
# Specify the HandbrakeCLI command with the full path to the executable
handbrake_executable = r"pythondir/HandBrakeCLI.exe"
handbrake_preset = "--encoder x265 --preset Slow --quality 20"
handbrake_command = f'"{handbrake_executable}" -i "{input_file_path}" -o "{output_file_path}" {handbrake_preset}'
# Create a subprocess and capture the output in real-time
process = subprocess.Popen(handbrake_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
# Monitor the progress of the encoding process
progress_regex = re.compile(r"\b(\d+(\.\d+)?) %")
for line in iter(process.stdout.readline, ''):
match = progress_regex.search(line)
if match:
progress = float(match.group(1))
print(f"Encoding progress: {progress:.2f}%")
# Wait for the encoding process to complete
process.wait()
return output_file_path
# Specify the source and destination directories
source_directory_path = "pythondir/source_encode"
destination_directory_path = "pythondir/target_encode"
# Encode and copy files with timestamp to the destination directory
encode_and_copy_files(source_directory_path, destination_directory_path)