Skip to content

Instantly share code, notes, and snippets.

@JySzE
Last active December 30, 2024 23:29
Show Gist options
  • Save JySzE/2818e3720357e6c272abfbbd52e1834a to your computer and use it in GitHub Desktop.
Save JySzE/2818e3720357e6c272abfbbd52e1834a to your computer and use it in GitHub Desktop.

How to Use the Scripts to Scan and Remove EIA-608 Subtitles from MKV Files

This guide walks you through using two Python scripts for identifying and removing EIA-608 subtitles from MKV video files. The first script scans MKV files in a specified directory for EIA-608 subtitles and logs the file paths. The second script removes the subtitles while backing up the original files.


1. Prerequisites

  • Python: Ensure you have Python installed on your system.
  • MPV Player: Download and install the MPV player. Ensure you update the mpv.com path in the script.
  • FFmpeg: Download and install FFmpeg. Add it to your system's PATH to run it from the command line.

2. Script 1: Scan for EIA-608 Subtitles

The first script identifies MKV files in a specified directory containing EIA-608 subtitles.

Steps to Run:
  1. Open the Find_EIA_608.py script and modify the following paths inside the script:
r"C:\Users\JySzE\Documents\MPV\mpv.com"
# Run the function with your desired scan directory and log file path
scan_files_for_eia_608(r"C:\Users\JySzE\Media_Test", r"C:\Users\JySzE\Desktop\eia_608_scan_log.txt")
  1. Save the script as Find_EIA_608.py.
  2. Run the script:
    python Find_EIA_608.py
    
  3. The script creates a log file listing the paths of all MKV files containing EIA-608 subtitles.

3. Script 2: Remove EIA-608 Subtitles

The second script removes EIA-608 subtitles from the files listed in the log created by the first script. It backs up each original file before processing.

Steps to Run:
  1. Open the script Remove_EIA_608.py and update the log_file variable with the path to the log file created by the first script:
log_file_path = r"C:\Users\JySzE\Desktop\eia_608_scan_log.txt"
  1. Save the modified script.
  2. Run the script:
    python Remove_EIA_608.py
  3. For each file in the log:
    • A backup of the original file is created with the .bak extension (e.g., video.mkv.bak).
    • The processed file is saved with the original file name.

4. Important Notes

  • Backup Files: Ensure enough disk space for backups, as each file will be duplicated before processing.
  • Error Handling:
    • If the MPV player or FFmpeg is not installed, you’ll see an error message. Install the missing tool and rerun the script.
    • If processing fails, the original file is restored from the backup.
  • Custom Paths: Adjust paths in the scripts to match your environment (e.g., locations of MPV and FFmpeg executables).

Follow these steps, and you’ll efficiently process your video files!

import os
import subprocess
def scan_files_for_eia_608(directory, log_filename):
with open(log_filename, 'w', encoding='utf-8') as log:
for root, dirs, files in os.walk(directory):
for filename in files:
if filename.endswith('.mkv'):
file_path = os.path.join(root, filename)
print(f"Scanning: {file_path}")
command = [
r"C:\Users\JySzE\Documents\MPV\mpv.com",
"--term-playing-msg=XXX", "--vo=null", "--ao=null",
"--frames=1", "--quiet", "--no-cache", "--no-config",
file_path
]
try:
result = subprocess.run(command, capture_output=True, text=True, encoding='utf-8', check=True)
if result.stdout and "eia_608" in result.stdout.lower():
log.write(file_path + "\n")
except subprocess.CalledProcessError as e:
print(f"Error running MPV on {file_path}: {e}")
continue
# Run the function with your desired scan directory and log file path
scan_files_for_eia_608(r"C:\Users\JySzE\Media_Test", r"C:\Users\JySzE\Desktop\eia_608_scan_log.txt")
import os
import subprocess
def remove_eia_608_with_ffmpeg_and_backup(log_file):
"""
Removes EIA-608 subtitles (type 6 NAL units) from the video stream of MKV files.
Keeps all other streams, renames the original file to .mkv.bak, and outputs the processed file with the original name.
"""
with open(log_file, "r") as log:
for file_path in log:
file_path = file_path.strip()
if os.path.exists(file_path):
# Generate backup filename
bak_file = f"{file_path}.bak"
# Check if a backup already exists
if os.path.exists(bak_file):
print(f"Backup already exists for {file_path}. Skipping.")
continue
print(f"Processing: {file_path}")
# Rename original file to .bak
try:
os.rename(file_path, bak_file)
print(f"Renamed original file to {bak_file}")
except OSError as e:
print(f"Error renaming {file_path} to {bak_file}: {e}")
continue
# Define the output file (same name as original before renaming)
output_file = file_path
# Run ffmpeg with filter_units to remove type 6 NAL units
try:
subprocess.run(
[
"ffmpeg", "-i", bak_file,
"-codec", "copy",
"-map", "0", # Copy all streams
"-bsf:v", "filter_units=remove_types=6", # Remove type 6 NAL units
output_file
],
check=True
)
print(f"EIA-608 removed. Output restored as {output_file}")
except FileNotFoundError:
print("Error: ffmpeg not found. Ensure it is installed and in your PATH.")
continue
except subprocess.CalledProcessError as e:
print(f"Error processing {bak_file}: {e}")
# Attempt to restore the original file if processing fails
os.rename(bak_file, file_path)
print(f"Restored original file from backup.")
else:
print(f"File not found: {file_path}")
# Update this path to the log file
log_file_path = r"C:\Users\JySzE\Desktop\eia_608_scan_log.txt"
remove_eia_608_with_ffmpeg_and_backup(log_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment