Skip to content

Instantly share code, notes, and snippets.

@YuriyGuts
Created January 1, 2020 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuriyGuts/9f5fc269a1754f47f60915396e3f67af to your computer and use it in GitHub Desktop.
Save YuriyGuts/9f5fc269a1754f47f60915396e3f67af to your computer and use it in GitHub Desktop.
Converts subtitles in a video from any format to plain text using ffmpeg
"""
Converts subtitles in a video from any format to plain text using ffmpeg.
Reads all files that match a glob pattern.
Writes output to current directory unless specified otherwise.
Usage: convert-subtitles.py <input_pattern> [output_folder]
Example: convert-subtitles.py ~/Videos/*.mkv
"""
import glob
import os
import sys
def convert_subtitles(input_pattern, output_folder):
input_filenames = [
filename
for filename in glob.glob(input_pattern)
if os.path.isfile(filename)
]
if len(input_filenames) == 0:
print('No files to process. Exiting.')
return
print('Converting subtitles in the following files:')
for filename in input_filenames:
print(' {}'.format(filename))
cmd_template = 'ffmpeg -i "{}" -c:v copy -c:a copy -c:s text -map 0 "{}"'
for i, filename in enumerate(input_filenames):
print('[{}/{}] Processing "{}"'.format(i + 1, len(input_filenames), filename))
cmd = cmd_template.format(filename, os.path.join(output_folder, os.path.basename(filename)))
print('Executing: {}'.format(cmd))
os.system(cmd)
def main():
if len(sys.argv) < 2:
print('Usage: convert-subtitles.py <input_pattern> [output_folder]')
sys.exit(1)
args = sys.argv[1:]
input_pattern = args[0]
output_folder = os.curdir if len(args) <= 1 else args[1]
convert_subtitles(input_pattern, os.path.abspath(output_folder))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment