Skip to content

Commit 01eaaf9

Browse files
author
shashi
committed
get subtitles of movies/tv series/
1 parent 704d96a commit 01eaaf9

8 files changed

+987
-0
lines changed

subtitle-downloader/.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
5+
# C extensions
6+
*.so
7+
8+
# Distribution / packaging
9+
.Python
10+
env/
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
*.egg-info/
23+
.installed.cfg
24+
*.egg
25+
26+
# PyInstaller
27+
# Usually these files are written by a python script from a template
28+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
29+
*.manifest
30+
*.spec
31+
32+
# Installer logs
33+
pip-log.txt
34+
pip-delete-this-directory.txt
35+
36+
# Unit test / coverage reports
37+
htmlcov/
38+
.tox/
39+
.coverage
40+
.coverage.*
41+
.cache
42+
nosetests.xml
43+
coverage.xml
44+
45+
# Translations
46+
*.mo
47+
*.pot
48+
49+
# Django stuff:
50+
*.log
51+
52+
# Sphinx documentation
53+
docs/_build/
54+
55+
# PyBuilder
56+
target/
57+
58+
# IDE
59+
.idea/

subtitle-downloader/LICENSE.md

Lines changed: 674 additions & 0 deletions
Large diffs are not rendered by default.

subtitle-downloader/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
Subtitle Downloader
2+
===================
3+
4+
Python script to download english subtitles of any movie/tv series episode.
5+
6+
7+
Get the latest version of Python at http://www.python.org/getit/.
8+
9+
### Usage:
10+
If you have python in your path, simply drag and drop files and folders you wish to get subtitles from on subtitle-downloader.py
11+
12+
#### Windows:
13+
* Install Python
14+
Follow the steps at http://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-7 to set the python path.
15+
16+
* Place subtitle-downloader.py file in C:\
17+
18+
* Place the Subtitle.cmd in sendto folder in windows (can be accessed by typing shell:sendto in address bar)
19+
20+
* Right click the movie file (not the movie folder). You can also select multiple files.
21+
and click sendto -> Subtitle.cmd
22+
23+
* If you want to download subtitles for all movies in a folder, you can use a folder as parameter to subtitle-downloader.py.
24+
25+
#### Mac:
26+
* Install python and make sure it is in your path
27+
28+
* Open Automator and create a new document of type "Service"
29+
30+
* Add an action called "Run Shell Script"
31+
32+
* On the top, under 'Service receives selected' drop-down, select 'files or folder' and in the next drop-down, for 'in' select 'Finder'
33+
34+
* On the right top of the Run Shell Script action, make sure you selected "as arguments" for "Pass Input"
35+
36+
* Copy the contents of subtitle.sh in the action
37+
38+
* Edit the path to "Subtitle_downloader.py" to wherever you have downloaded
39+
40+
* Press `Command + S` to save it and give some name like "Download Subtitle"
41+
42+
* Now, Right click the movie file (not the movie folder). You can also select multiple files. Click Services -> Download Subtitle
43+
44+
45+
#####Linux with Nautilus file manager(Tested on Debian Based with nautilus as file manager):
46+
* Install python
47+
48+
* Go to ~/.local/share/nautilus/scripts/ (Ubuntu 13.04 or above) OR ~/.gnome2/nautilus-scripts/ (Ubuntu 12.10 and below) folder and add Subtitles_in_english.sh in the folder.
49+
50+
* The path for Subtitle_downloader.py is hardcoded to Desktop ...U can change it accordingly.
51+
52+
* Now Right Click on the movie file (not the movie folder). You can also select multiple files. Click Services -> Subtitles_in_english.
53+
54+
55+
![ScreenShot](https://cloud.githubusercontent.com/assets/1637697/3078931/1a693b9a-e487-11e3-8d51-64dce970ad9d.gif)
56+
57+
Voila. the .srt subtitle file will be created right next to your movie file.
58+
59+
Enjoy the show!
60+
61+
More details can be found here : qr.ae/GxOcx
62+
A how to video can be found here: http://www.youtube.com/watch?v=Q5YWEqgw9X8
63+
64+
#####Linux with NEMO file manager(Tested on Debian Based with NEMO as file manager):
65+
* Install python
66+
67+
* Go to ~/.gnome2/nemo-scripts folder and add Subtitles_In_English_for_nemo.sh in the folder.
68+
69+
* The path for Subtitle_downloader.py is hardcoded to Desktop ...You can change it accordingly.
70+
71+
* Open terminal and execute following command :
72+
```
73+
chmod +x ~/.gnome2/nemo-scripts/Subtitles_In_English_for_nemo.sh
74+
```
75+
76+
* Now Right Click on the movie file (not the movie folder). You can also select multiple files. Click Services -> Subtitles_In_English_for_nemo.
77+
78+
Voila. the .srt subtitle file will be created right next to your movie file.

subtitle-downloader/Subtitle.cmd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@echo off
2+
cls
3+
set PATH=%PATH%;C:\Python27\
4+
:my_loop
5+
IF %1=="" GOTO completed
6+
python C:\subtitle-downloader.py %1
7+
SHIFT
8+
GOTO my_loop
9+
:completed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
IFS_BAK=$IFS
2+
IFS="
3+
"
4+
5+
for line in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS; do
6+
full_path="/home/"$USER"/Desktop/"subtitle-downloader.py
7+
python $full_path $line
8+
notify-send $line
9+
done
10+
11+
IFS=$IFS_BAK
12+
13+
14+
15+
16+
##to do::#give a check here to see if the files coming are .mp4........videoformat files only otherwise exit.....
17+
#to do::also we can give languagae as second parameter
18+
#path to save in ~/.gnome2/nautilus-scripts
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
IFS_BAK=$IFS
2+
IFS="
3+
"
4+
5+
for line in $NEMO_SCRIPT_SELECTED_FILE_PATHS; do
6+
full_path="/home/"$USER"/Desktop/"subtitle-downloader.py
7+
python $full_path $line
8+
notify-send $line
9+
done
10+
11+
IFS=$IFS_BAK
12+
13+
14+
15+
16+
##to do::#give a check here to see if the files coming are .mp4........videoformat files only otherwise exit.....
17+
#to do::also we can give languagae as second parameter
18+
#path to save in ~/.gnome2/nautilus-scripts
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env python
2+
#-------------------------------------------------------------------------------
3+
# Name : subtitle downloader
4+
# Purpose : One step subtitle download
5+
#
6+
# Authors : manoj m j, arun shivaram p, Valentin Vetter, niroyb
7+
# Edited by : Valentin Vetter
8+
# Created :
9+
# Copyright : (c) www.manojmj.com
10+
# Licence : GPL v3
11+
#-------------------------------------------------------------------------------
12+
13+
# TODO: use another DB if subs are not found on subDB
14+
import hashlib
15+
import os
16+
import shutil
17+
import sys
18+
import logging
19+
import requests,time,re,zipfile
20+
from bs4 import BeautifulSoup
21+
PY_VERSION = sys.version_info[0]
22+
if PY_VERSION == 2:
23+
import urllib2
24+
if PY_VERSION == 3:
25+
import urllib.request
26+
27+
28+
def get_hash(file_path):
29+
read_size = 64 * 1024
30+
with open(file_path, 'rb') as f:
31+
data = f.read(read_size)
32+
f.seek(-read_size, os.SEEK_END)
33+
data += f.read(read_size)
34+
return hashlib.md5(data).hexdigest()
35+
36+
37+
def sub_downloader(file_path):
38+
# Put the code in a try catch block in order to continue for other video files, if it fails during execution
39+
try:
40+
# Skip this file if it is not a video
41+
root, extension = os.path.splitext(file_path)
42+
if extension not in [".avi", ".mp4", ".mkv", ".mpg", ".mpeg", ".mov", ".rm", ".vob", ".wmv", ".flv", ".3gp",".3g2"]:
43+
return
44+
45+
if not os.path.exists(root + ".srt"):
46+
headers = {'User-Agent': 'SubDB/1.0 (subtitle-downloader/1.0; http://github.com/manojmj92/subtitle-downloader)'}
47+
url = "http://api.thesubdb.com/?action=download&hash=" + get_hash(file_path) + "&language=en"
48+
if PY_VERSION == 3:
49+
req = urllib.request.Request(url, None, headers)
50+
response = urllib.request.urlopen(req).read()
51+
if PY_VERSION == 2:
52+
req = urllib2.Request(url, '', headers)
53+
response = urllib2.urlopen(req).read()
54+
55+
with open(root + ".srt", "wb") as subtitle:
56+
subtitle.write(response)
57+
logging.info("Subtitle successfully downloaded for " + file_path)
58+
except:
59+
#download subs from subscene if not found in subdb
60+
sub_downloader2(file_path)
61+
def sub_downloader2(file_path):
62+
try:
63+
root, extension = os.path.splitext(file_path)
64+
if extension not in [".avi", ".mp4", ".mkv", ".mpg", ".mpeg", ".mov", ".rm", ".vob", ".wmv", ".flv", ".3gp",".3g2"]:
65+
return
66+
if os.path.exists(root + ".srt"):
67+
return
68+
j=-1
69+
root2=root
70+
for i in range(0,len(root)):
71+
if(root[i]=="\\" or root[i] =="/"):
72+
j=i
73+
break
74+
root=root2[j+1:]
75+
root2=root2[:j+1]
76+
r=requests.get("http://subscene.com/subtitles/release?q="+root);
77+
soup=BeautifulSoup(r.content,"lxml")
78+
atags=soup.find_all("a")
79+
href=""
80+
for i in range(0,len(atags)):
81+
spans=atags[i].find_all("span")
82+
if(len(spans)==2 and spans[0].get_text().strip()=="English"):
83+
href=atags[i].get("href").strip()
84+
if(len(href)>0):
85+
r=requests.get("http://subscene.com"+href);
86+
soup=BeautifulSoup(r.content,"lxml")
87+
lin=soup.find_all('a',attrs={'id':'downloadButton'})[0].get("href")
88+
r=requests.get("http://subscene.com"+lin);
89+
soup=BeautifulSoup(r.content,"lxml")
90+
subfile=open(root2+".zip", 'wb')
91+
for chunk in r.iter_content(100000):
92+
subfile.write(chunk)
93+
subfile.close()
94+
time.sleep(1)
95+
zip=zipfile.ZipFile(root2+".zip")
96+
zip.extractall(root2)
97+
zip.close()
98+
os.unlink(root2+".zip")
99+
shutil.move(root2+zip.namelist()[0], os.path.join(root2, root + ".srt"))
100+
except:
101+
#Ignore exception and continue
102+
print("Error in fetching subtitle for " + file_path)
103+
print("Error", sys.exc_info())
104+
logging.error("Error in fetching subtitle for " + file_path + str(sys.exc_info()))
105+
106+
107+
def main():
108+
root, _ = os.path.splitext(sys.argv[0])
109+
logging.basicConfig(filename=root + '.log', level=logging.INFO)
110+
logging.info("Started with params " + str(sys.argv))
111+
112+
if len(sys.argv) == 1:
113+
print("This program requires at least one parameter")
114+
sys.exit(1)
115+
116+
for path in sys.argv:
117+
if os.path.isdir(path):
118+
# Iterate the root directory recursively using os.walk and for each video file present get the subtitle
119+
for dir_path, _, file_names in os.walk(path):
120+
for filename in file_names:
121+
file_path = os.path.join(dir_path, filename)
122+
sub_downloader(file_path)
123+
else:
124+
sub_downloader(path)
125+
126+
if __name__ == '__main__':
127+
main()

subtitle-downloader/subtitle.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
for i in "$@"
2+
do
3+
python path/to/the/repo/subtitle-downloader.py "$i"
4+
done

0 commit comments

Comments
 (0)