Advertisement
Guest User

Plex/MKVToolNix mp4 to mkv Converter

a guest
Apr 9th, 2024
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.61 KB | Source Code | 0 0
  1. import os
  2. import subprocess
  3. import time
  4.  
  5. # Define the directory to scan for MP4 files (CHANGE THIS)
  6. source_dir = r'\\PlexBox\data\movies'
  7.  
  8. # Define the location of MKVToolNix's mkvmerge.exe (CHANGE THIS)
  9. mkvmerge_exe = r'C:\Program Files\MKVToolNix\mkvmerge.exe'
  10.  
  11. # Get a list of MP4 files in the source directory
  12. mp4_files = [file for file in os.listdir(source_dir) if file.lower().endswith('.mp4')]
  13.  
  14. # List to hold the names of deleted files
  15. deleted_files_list = []
  16.  
  17. # Start index for batch processing
  18. start_index = 0
  19.  
  20. # Maximum batch size (CAN CHANGE THIS)
  21. batch_size = 50
  22.  
  23. # Batch counter
  24. batch_counter = 0
  25.  
  26. while start_index < len(mp4_files):
  27.     # Determine the end index for the current batch
  28.     end_index = min(start_index + batch_size, len(mp4_files))
  29.  
  30.     # Get the MP4 files for the current batch
  31.     current_batch = mp4_files[start_index:end_index]
  32.  
  33.     # Flag to check if any files were converted and deleted in the current batch
  34.     batch_processed = False
  35.  
  36.     # Start time for the current batch
  37.     batch_start_time = time.time()
  38.  
  39.     # Loop through the current batch of MP4 files and convert them to MKV using MKVToolNix
  40.     for mp4_file in current_batch:
  41.         mp4_path = os.path.join(source_dir, mp4_file)
  42.         mkv_file = mp4_file[:-4] + '.mkv'  # Create the corresponding MKV file name
  43.         mkv_path = os.path.join(source_dir, mkv_file)
  44.  
  45.         # Command to convert MP4 to MKV using MKVToolNix
  46.         convert_command = f'"{mkvmerge_exe}" -o "{mkv_path}" "{mp4_path}"'
  47.  
  48.         # Execute the conversion command using subprocess
  49.         subprocess.run(convert_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  50.  
  51.         # Check if the MKV file was created successfully
  52.         if os.path.isfile(mkv_path):
  53.             # If conversion successful, remove the original MP4 file
  54.             os.remove(mp4_path)
  55.             batch_processed = True
  56.             deleted_files_list.append(mp4_file)
  57.             print(f"Converted and removed: {mp4_file}")
  58.  
  59.     # End time for the current batch
  60.     batch_end_time = time.time()
  61.  
  62.     # Calculate total time taken for the current batch
  63.     batch_total_time = batch_end_time - batch_start_time
  64.  
  65.     # Output batch log to a text file
  66.     log_file_path = os.path.join(source_dir, '!conversion_log.txt')
  67.     with open(log_file_path, 'a') as log_file:
  68.         if batch_counter > 0:
  69.             log_file.write("\n\n")
  70.  
  71.         timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  72.         log_file.write(f"=== {timestamp} ===\n")
  73.         log_file.write(f"Total Time Taken for Batch: {batch_total_time:.2f} seconds\n")
  74.  
  75.         if batch_processed:
  76.             # If any files were converted and removed in the current batch, list them in the log
  77.             log_file.write("Files that were deleted:\n")
  78.             for file_name in deleted_files_list[-batch_size:]:
  79.                 log_file.write(f"- {file_name}\n")
  80.             log_file.write(f"Total Number of Files Deleted in Batch: {len(deleted_files_list[-batch_size:])}\n")
  81.         else:
  82.             log_file.write("No files were converted and removed in this batch.\n")
  83.  
  84.         # Add a blank line after each log entry
  85.         log_file.write("\n")
  86.  
  87.         # Increment batch counter
  88.         batch_counter += 1
  89.  
  90.     # Increment the start index for the next batch
  91.     start_index += batch_size
  92.  
  93.     # Prompt user to continue or exit
  94.     if start_index < len(mp4_files):
  95.         user_input = input("Continue processing next batch? (Y/N): ").strip().lower()
  96.         if user_input != 'y':
  97.             break
  98.  
  99. print("Conversion and batch processing completed.")
  100.  
Tags: Plex
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement