-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaststream.py
113 lines (94 loc) · 3.41 KB
/
faststream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import sys
import subprocess
import time
import os
import urllib.parse
import time
import math
import configparser
import tempfile
from send2trash import send2trash
from babelfish import Language
from subliminal import *
configp = configparser.ConfigParser()
if configp.read('config.ini'):
config = configp['DEFAULT']
else:
print("config file not found")
sys.exit()
def main(filePath):
targetPath = tempfile.gettempdir()
filename = os.path.basename(filePath).split("?")[0]
videoPath = urllib.parse.unquote(os.path.join(targetPath, filename))
languages = ["eng"]
downloadBufferTime = 5 # In seconds
ariaProcess = subprocess.Popen(
["aria2c", "-x 8", "--file-allocation=none", "--continue=true", "--stream-piece-selector=inorder", "--dir=" + targetPath, filePath],
stdout=sys.stdout,
stderr=sys.stderr)
# Wait for the video to be created
while not os.path.exists(videoPath):
time.sleep(1)
startDownloadingVideoTime = time.time()
time.sleep(1)
# Quick and simple subtitle download without checking its format
"""
subliminalProcess = subprocess.Popen(
["subliminal", "download", "-l", "en", "-s", videoPath],
stdout=sys.stdout,
stderr=sys.stderr)
"""
region.configure('dogpile.cache.dbm', arguments={'filename': 'cachefile.dbm'})
video = scan_video(videoPath)
subtitles = download_best_subtitles([video], {Language(languages[0])})
hasFoundSubtitle = False
if len(subtitles) > 0:
print("A subtitle has been found!")
hasFoundSubtitle = True
save_subtitles(video, subtitles[video])
else:
print("No subtitles found.")
subtitlePath = os.path.splitext(videoPath)[0] + "." + languages[0][:2] + ".srt"
elapsedDownloadTime = time.time() - startDownloadingVideoTime
while elapsedDownloadTime < downloadBufferTime:
remainingTime = math.floor(downloadBufferTime - elapsedDownloadTime)
print("Waiting {} seconds.".format(str(remainingTime)))
time.sleep(1)
elapsedDownloadTime = time.time() - startDownloadingVideoTime
if hasFoundSubtitle and os.path.exists(subtitlePath):
vlcProcess = subprocess.Popen(["vlc", "--fullscreen", "--sub-file=" + subtitlePath, videoPath])
else:
vlcProcess = subprocess.Popen(["vlc", "--fullscreen", videoPath])
vlcProcess.wait()
print("VLC process finished.")
print("Terminating aria2c.")
ariaProcess.terminate()
if config['alwaysDeleteCache'] == 'True':
os.remove(videoPath)
if os.path.exists(subtitlePath):
os.remove(subtitlePath)
return 0
while True:
res = input("Move the video to the (t)rash, (d)elete or do (n)othing? (default 'd') [t/d/n] ")
if (res == "t"):
send2trash(videoPath)
if os.path.exists(subtitlePath):
send2trash(subtitlePath)
return 0
elif (res == "n"):
return 0
elif (res == "d" or res == ""):
os.remove(videoPath)
if os.path.exists(subtitlePath):
os.remove(subtitlePath)
return 0
else:
print("Please enter 't', 'd' or 'n'.")
if __name__ == "__main__":
while True:
if len(sys.argv) < 2:
filePath = input("Enter video URL/Torrent/Magnet: ")
else:
filePath = sys.argv[1]
if filePath:
main(filePath)