Solution: Python subtitle downloader

Development | Programming languages
Description
Python script to automatically download subtitles for any movie/tv series episode.
Attachments
No attachments.
Solutions
__author__ = 'aman'
import hashlib
import os
import sys
import urllib.request, urllib.parse
import urllib3

def get_hash(name):
    readsize = 64 * 1024
    with open(name, 'rb') as f:
        size = os.path.getsize(name)
        data = f.read(readsize)
        f.seek(-readsize, os.SEEK_END)
        data += f.read(readsize)
    return hashlib.md5(data).hexdigest()
# Add as necessary
videoExtensions = [".avi",".mp4",".mkv",".mpg",".mpeg",".mov",".rm",".vob",".wmv",".flv",".3gp"]
def subGetter(filename):
    try:
        originalFileName = filename
        for videoExt in videoExtensions:
            filename = filename.replace(videoExt, "")
        if originalFileName == filename:
            return "Not a media file."
        hash = get_hash(originalFileName)
        headers = { 'User-Agent' : 'SubDB/1.0 (subtitle-downloader/1.0; http://github.com/amanthedorkknight/subtitleDownloader)' }
        link = "http://api.thesubdb.com/?action=download&hash=" + hash + "&language=en"
        req = urllib.request.Request(link, None, headers)
        response = urllib.request.urlopen(req).read()
        with open (filename + ".srt", "wb") as subtitle:
            subtitle.write(response)
            print("Success! " + filename)
    except:
        print ("Not found! " + filename)
# Replace with the path to your movie directory        
rootdir = "c:\\Movies\\"
# Iterate the root directory recursively using os.walk and for each video file present get the subtitle
for root, subFolders, files in os.walk(rootdir):
    for file in files:
        fname = os.path.join(root, file)
        subGetter(fname)


on May 06th, 2019 (9:44 pm)
What masters said
You must sign in to comment!!
All comments
This solution has not been commented yet.
We use cookies to give you the best possible experience on our site. By continuing to use the site you agree to our use of cookies. Find out more Accept