Blu-Rays can come with a variety of metadata already baked into them - and encoding them doesn't touch them by default.
I prefer my files to have only the metadata necessary for proper playback and identification, and removing all of it
is the most straightforward way to do that. That's where this script comes in - its main function is to remove all
metadata so nothing is inaccurate, like bitrate and language.
The other function of this is to leave a 'watermark' of sorts in a more useless part of the metadata, so that upon inspection
I can always know if it's something I encoded right off the bat. For this I just put my initials in the 'Title' field of the video stream.
The Code
import os, subprocess
import tkinter as tk
from tkinter import filedialog
root = tk.Tk() # suppress Tkinter dialog box
root.withdraw()
root_path = filedialog.askdirectory(title="Select Source Directory")
add_signature = input('Add Signature? ')
add_sig = ''
if add_signature == 'y': # add signature data if applicable
add_sig = '-metadata:s:v:0 title="JR"'
for root, directory, files in os.walk(root_path): # find all video files in directory tree
for f in files:
if f[-4:] in ['.mp4', '.mkv', '.mov', '.MKV', '.avi']:
path = os.path.join(root,f)
new_path = os.path.join(root, 'new '+f)
if os.path.exists(new_path): # remove old temp file if it exists
os.remove(new_path)
result = subprocess.getoutput(f'ffprobe "{path}"')
if 'Subtitle' in result:
# after removing metadata, it's important to add it back (label audio and subs as English, add signature, turn off default flag on subtitles)
os.system(f'ffmpeg -i "{path}" -map_metadata -1 -c copy -c:s copy -f matroska -| ffmpeg -i - -c copy -c:s copy {add_sig} -metadata:s:a:0 language=eng -metadata:s:s:0 language=eng -f matroska -| ffmpeg -i - -c copy -c:s copy -map 0 -disposition:s:0 none -default_mode infer_no_subs "{new_path}"')
else:
os.system(f'ffmpeg -i "{path}" -map_metadata -1 -c copy -f matroska -| ffmpeg -i - -c copy {add_sig} -metadata:s:a:0 language=eng "{new_path}"')
if os.path.exists(new_path): # change temp file to original
os.remove(path)
os.rename(new_path,path)