Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import subprocess
- import time
- # Define the directory to scan for MP4 files (CHANGE THIS)
- source_dir = r'\\PlexBox\data\movies'
- # Define the location of MKVToolNix's mkvmerge.exe (CHANGE THIS)
- mkvmerge_exe = r'C:\Program Files\MKVToolNix\mkvmerge.exe'
- # Get a list of MP4 files in the source directory
- mp4_files = [file for file in os.listdir(source_dir) if file.lower().endswith('.mp4')]
- # List to hold the names of deleted files
- deleted_files_list = []
- # Start index for batch processing
- start_index = 0
- # Maximum batch size (CAN CHANGE THIS)
- batch_size = 50
- # Batch counter
- batch_counter = 0
- while start_index < len(mp4_files):
- # Determine the end index for the current batch
- end_index = min(start_index + batch_size, len(mp4_files))
- # Get the MP4 files for the current batch
- current_batch = mp4_files[start_index:end_index]
- # Flag to check if any files were converted and deleted in the current batch
- batch_processed = False
- # Start time for the current batch
- batch_start_time = time.time()
- # Loop through the current batch of MP4 files and convert them to MKV using MKVToolNix
- for mp4_file in current_batch:
- mp4_path = os.path.join(source_dir, mp4_file)
- mkv_file = mp4_file[:-4] + '.mkv' # Create the corresponding MKV file name
- mkv_path = os.path.join(source_dir, mkv_file)
- # Command to convert MP4 to MKV using MKVToolNix
- convert_command = f'"{mkvmerge_exe}" -o "{mkv_path}" "{mp4_path}"'
- # Execute the conversion command using subprocess
- subprocess.run(convert_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- # Check if the MKV file was created successfully
- if os.path.isfile(mkv_path):
- # If conversion successful, remove the original MP4 file
- os.remove(mp4_path)
- batch_processed = True
- deleted_files_list.append(mp4_file)
- print(f"Converted and removed: {mp4_file}")
- # End time for the current batch
- batch_end_time = time.time()
- # Calculate total time taken for the current batch
- batch_total_time = batch_end_time - batch_start_time
- # Output batch log to a text file
- log_file_path = os.path.join(source_dir, '!conversion_log.txt')
- with open(log_file_path, 'a') as log_file:
- if batch_counter > 0:
- log_file.write("\n\n")
- timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
- log_file.write(f"=== {timestamp} ===\n")
- log_file.write(f"Total Time Taken for Batch: {batch_total_time:.2f} seconds\n")
- if batch_processed:
- # If any files were converted and removed in the current batch, list them in the log
- log_file.write("Files that were deleted:\n")
- for file_name in deleted_files_list[-batch_size:]:
- log_file.write(f"- {file_name}\n")
- log_file.write(f"Total Number of Files Deleted in Batch: {len(deleted_files_list[-batch_size:])}\n")
- else:
- log_file.write("No files were converted and removed in this batch.\n")
- # Add a blank line after each log entry
- log_file.write("\n")
- # Increment batch counter
- batch_counter += 1
- # Increment the start index for the next batch
- start_index += batch_size
- # Prompt user to continue or exit
- if start_index < len(mp4_files):
- user_input = input("Continue processing next batch? (Y/N): ").strip().lower()
- if user_input != 'y':
- break
- print("Conversion and batch processing completed.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement