Skip to content

Instantly share code, notes, and snippets.

@lambdan
Created September 13, 2020 13:08
Show Gist options
  • Save lambdan/ce8adafa938105518994ff00eb733659 to your computer and use it in GitHub Desktop.
Save lambdan/ce8adafa938105518994ff00eb733659 to your computer and use it in GitHub Desktop.
Extract EIA-608 CC embedded in video stream to SRT and remove from original file
# Requires ffmpeg and ffprobe
# Thanks to ffmpeg command from https://stackoverflow.com/a/51439554
delete_original = True # delete the original file?
sub_lang = ".eng"
import os, subprocess, sys, shutil
file = os.path.abspath(sys.argv[1]) # input file
if not os.path.isfile(file):
print("not a file:", file)
sys.exit(1)
cmd = ['ffprobe', '-i', file]
result = subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT).decode()
has_CC = False
for line in result.splitlines():
if "Closed Captions" in line:
has_CC = True
if has_CC:
print("Has CC!")
new_file = os.path.splitext(file)[0] + " [CC Removed]" + os.path.splitext(file)[1]
new_sub = os.path.splitext(file)[0] + " [CC Removed]" + sub_lang + '.srt'
cmd = ['ffmpeg', '-y', '-stats', '-loglevel', 'error', '-i', file, '-map', '0:v', '-map', '0:a', '-c', 'copy', '-bsf:v', 'filter_units=remove_types=6', new_file] # https://stackoverflow.com/a/51439554
sub_cmd = ['ffmpeg', '-y', '-stats', '-loglevel', 'error', '-i', file, '-bsf:v', 'filter_units=pass_types=6', new_sub] # https://stackoverflow.com/a/51439554
print("extracting sub:", *sub_cmd)
subprocess.call(sub_cmd)
print("extracting audio/video:", *cmd)
subprocess.call(cmd)
if delete_original and os.path.isfile(new_file):
print("Replacing the original file...")
os.remove(file)
sub_fixed = os.path.splitext(file)[0] + sub_lang + '.srt'
shutil.move(new_file, file)
shutil.move(new_sub, sub_fixed)
else:
print("No CC found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment