I have tired to create a python script to identify and remove forced subs. The script identifies the correct sub ids to remove but fails to remove them. Here is the script, anybody have any ideas?
import subprocess
import os
import re
# Get the directory of the currently running script
script_dir = os.path.dirname(os.path.realpath(__file__))
# Function to check for forced subtitles and return their track IDs
def get_forced_subtitle_track_ids(mkv_file):
"""Get a list of forced subtitle track IDs from the MKV file."""
result = subprocess.run(['mkvinfo', mkv_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output = result.stdout.decode('utf-8')
# Enhanced regex to find all forced subtitle tracks by matching the "Forced display" flag
forced_subtitle_tracks = re.findall(r'\+ Track.*?Forced display.*?track ID for mkvmerge & mkvextract: (\d+)', output, re.DOTALL)
# Check for a possible single line description of forced subtitles
if not forced_subtitle_tracks:
forced_subtitle_tracks = re.findall(r'Forced display: Yes.*?track ID for mkvmerge & mkvextract: (\d+)', output, re.DOTALL)
# Convert the track IDs to integers and subtract 1
adjusted_subtitle_tracks = [str(int(track_id) - 1) for track_id in forced_subtitle_tracks]
return adjusted_subtitle_tracks
# Function to exclude forced subtitles in the MKV file
def exclude_forced_subtitles(mkv_file):
"""Remove forced subtitle tracks from the MKV file."""
# Get the list of forced subtitle track IDs
forced_subtitles = get_forced_subtitle_track_ids(mkv_file)
# If there are no forced subtitles, skip processing
if not forced_subtitles:
print(f"No forced subtitles found in {mkv_file}. Skipping...")
return
# Create a comma-separated string of forced subtitle track IDs
forced_subtitles_str = ','.join(forced_subtitles)
# Build the mkvmerge command to exclude the forced subtitle tracks using the -s !
mkvmerge_command = ['mkvmerge', '-o', f'no_forced_{os.path.basename(mkv_file)}', mkv_file, '-s', f'!{forced_subtitles_str}']
# Debugging: Print the command being executed
print(f"Running command: {' '.join(mkvmerge_command)}")
# Run the mkvmerge command and check for errors
result = subprocess.run(mkvmerge_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Print the result of the mkvmerge command for debugging
if result.returncode != 0:
print(f"Error excluding forced subtitles from {mkv_file}: {result.stderr.decode('utf-8')}")
else:
print(f"Forced subtitles excluded and saved as 'no_forced_{os.path.basename(mkv_file)}'")
# Process all MKV files in the current directory
for file_name in os.listdir(script_dir):
if file_name.endswith(".mkv"):
mkv_file = os.path.join(script_dir, file_name)
print(f"Processing {mkv_file}...")
# Exclude forced subtitles from the file
exclude_forced_subtitles(mkv_file)
+ Reply to Thread
Results 1 to 2 of 2
-
-
Python indentation disappears if printed outside of code tags, put your text into a CODE tags.
Example: [ CODE ]your script here[ /CODE ] , no gaps in those square brackets,
so just re-posting it, not correcting it:
Code:import subprocess import os import re # Get the directory of the currently running script script_dir = os.path.dirname(os.path.realpath(__file__)) # Function to check for forced subtitles and return their track IDs def get_forced_subtitle_track_ids(mkv_file): """Get a list of forced subtitle track IDs from the MKV file.""" result = subprocess.run(['mkvinfo', mkv_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = result.stdout.decode('utf-8') # Enhanced regex to find all forced subtitle tracks by matching the "Forced display" flag forced_subtitle_tracks = re.findall(r'\+ Track.*?Forced display.*?track ID for mkvmerge & mkvextract: (\d+)', output, re.DOTALL) # Check for a possible single line description of forced subtitles if not forced_subtitle_tracks: forced_subtitle_tracks = re.findall(r'Forced display: Yes.*?track ID for mkvmerge & mkvextract: (\d+)', output, re.DOTALL) # Convert the track IDs to integers and subtract 1 adjusted_subtitle_tracks = [str(int(track_id) - 1) for track_id in forced_subtitle_tracks] return adjusted_subtitle_tracks # Function to exclude forced subtitles in the MKV file def exclude_forced_subtitles(mkv_file): """Remove forced subtitle tracks from the MKV file.""" # Get the list of forced subtitle track IDs forced_subtitles = get_forced_subtitle_track_ids(mkv_file) # If there are no forced subtitles, skip processing if not forced_subtitles: print(f"No forced subtitles found in {mkv_file}. Skipping...") return # Create a comma-separated string of forced subtitle track IDs forced_subtitles_str = ','.join(forced_subtitles) # Build the mkvmerge command to exclude the forced subtitle tracks using the -s ! mkvmerge_command = ['mkvmerge', '-o', f'no_forced_{os.path.basename(mkv_file)}', mkv_file, '-s', f'!{forced_subtitles_str}'] # Debugging: Print the command being executed print(f"Running command: {' '.join(mkvmerge_command)}") # Run the mkvmerge command and check for errors result = subprocess.run(mkvmerge_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # Print the result of the mkvmerge command for debugging if result.returncode != 0: print(f"Error excluding forced subtitles from {mkv_file}: {result.stderr.decode('utf-8')}") else: print(f"Forced subtitles excluded and saved as 'no_forced_{os.path.basename(mkv_file)}'") # Process all MKV files in the current directory for file_name in os.listdir(script_dir): if file_name.endswith(".mkv"): mkv_file = os.path.join(script_dir, file_name) print(f"Processing {mkv_file}...") # Exclude forced subtitles from the file exclude_forced_subtitles(mkv_file)
Last edited by _Al_; 3rd Jan 2025 at 13:50.
Similar Threads
-
Script to remove telecine/interlacing from DVD rips
By UtahJohnnyMontana in forum LinuxReplies: 0Last Post: 7th Dec 2024, 02:39 -
Remove all app data sample subs
By loninappleton in forum SubtitleReplies: 4Last Post: 25th Jul 2024, 00:00 -
Help with a script to convert .ass subs to .srt amd merge them
By Ea$tgate in forum SubtitleReplies: 0Last Post: 22nd Dec 2023, 11:42 -
Remove stabilize from script
By lordsmurf in forum RestorationReplies: 9Last Post: 22nd Sep 2020, 00:39 -
How to print id number of default/forced subtitles in batch script
By midelpo in forum Newbie / General discussionsReplies: 0Last Post: 27th Apr 2020, 13:41