There for I created a script that will:
- check if the download is a mkv
- if it is a mkv, chec how many streams it contains using ffprobe
- if it contains 30 or more streams, create a new mkv and copy all video and audio streams into it (not reencoding) and only the english and dutch subtitles using ffmpeg
- finish it up by removing the original mkv and replacing it with the new
Code: Select all
import sys
import os
import subprocess
from os import listdir
from os.path import isfile, join
try:
(scriptname, directory, orgnzbname, jobname, reportnumber, category, group, postprocstatus, url) = sys.argv
except:
print("No commandline parameters found")
sys.exit(1)
ffprobe = "/usr/bin/ffprobe"
ffmpeg = "/usr/bin/ffmpeg"
def handle_mkv(directory, filename, extension):
full_path = join(directory, filename)+extension
print("")
print("######################")
print("Filename: " + full_path)
ffprobe_commands_list = [ffprobe,"-hide_banner","-loglevel", "error", "-show_entries", "stream=index:stream=codec_type:stream_tags=language","-of", "csv=p=0", full_path]
print("Command list: " + str(ffprobe_commands_list))
#execute process
ffprobe_result = subprocess.run(ffprobe_commands_list, capture_output=True, text=True, check=True)
print("")
print(ffprobe_result.stdout)
nr_of_streams = len(ffprobe_result.stdout.splitlines())
print("File contains " + str(nr_of_streams) + " streams.")
if nr_of_streams >=30:
print("Stripping subtitles from file...")
tmp_filename = filename+"_CLEAN"
tmp_full_path = join(directory, tmp_filename)+extension
ffmpeg_commands_list = [ffmpeg,"-hide_banner","-i", full_path, "-c", "copy", "-map", "0:v", "-map", "0:a", "-map", "0:s:m:language:eng?", "-map", "0:s:m:language:dut?", tmp_full_path]
print("Command list: " + str(ffmpeg_commands_list))
ffmpeg_result = subprocess.run(ffmpeg_commands_list, capture_output=True, text=True, check=True)
print("")
print(ffmpeg_result.stdout)
print("")
print("Swapping old file with cleaned file and setting access rights")
os.remove(full_path)
os.rename(tmp_full_path, full_path)
os.chmod(full_path, 0o664)
print("######################")
print("Searching:" + directory)
onlyfiles = [f for f in listdir(directory) if isfile(join(directory, f))]
for f in onlyfiles:
print(f)
if os.path.splitext(f)[1].lower() == '.mkv':
print ("Found mkv: " + f)
handle_mkv(directory, os.path.splitext(f)[0], os.path.splitext(f)[1] )
# Success code
sys.exit(0)