Created
June 9, 2018 12:56
-
-
Save kristjanvariksoo/21b8ba367d7ce07eae6b8664f96d3537 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests, json, multiprocessing | |
GETINFO = False | |
def get_best_playlist(m3ufile): | |
maxquality = 0 | |
bestplaylist = "" | |
lines = m3ufile.split("\n") | |
n = 0 | |
while n < len(lines): | |
line = lines[n] | |
if len(line) > 18: | |
if line[:28] == "#EXT-X-STREAM-INF:BANDWIDTH=": | |
quality = int(line[28:]) | |
print("Found playlist with quality ", str(quality)) | |
if quality > maxquality: | |
maxquality = quality | |
bestplaylist = lines[n+1] | |
n+=1 | |
print("WOOHOO LINE", line) | |
else: | |
print("BOOOO", line) | |
print("") | |
print("") | |
n+=1 | |
return bestplaylist | |
print("Link TV3 lehel on selline https://tv3play.tv3.ee/sisu/vabad-mehed/XXXXX?autostart=true, kus XXXXX on ID.") | |
#id = str(int(raw_input("Sisestage ID:"))) | |
id = "587952" | |
if GETINFO: | |
print("Muretseme video info...") | |
info = requests.get("https://playapi.mtgx.tv/v3/videos/" + id).json() | |
print(json.dumps(info, indent=4)) | |
print("Muretseme video enda...") | |
streams = requests.get("https://playapi.mtgx.tv/v3/videos/stream/" + id).json() | |
print(json.dumps(streams, indent=4)) | |
print("Muretseme playlisti...") | |
stream = streams["streams"]["hls"] | |
playlists = requests.get(stream).text | |
print(playlists) | |
#The stream variable contains a full path. Remove the filename from the end to get the directory full path | |
PLAYLIST_BASE = stream.split("/") | |
PLAYLIST_BASE[-1] = "" | |
PLAYLIST_BASE = "/".join(PLAYLIST_BASE) | |
playlist_fn = get_best_playlist(playlists) | |
print("BEST WE GOT IS: ", PLAYLIST_BASE + playlist_fn) | |
playlist = requests.get(PLAYLIST_BASE + playlist_fn).text | |
pieces = [] | |
for line in playlist.split("\n"): | |
if line[-3:] == ".ts": | |
pieces.append(line) | |
print pieces | |
from multiprocessing import Pool | |
def job(url): | |
print("Starting download.") | |
r = requests.get(url, allow_redirects=True) | |
file_name = str(url.split('/')[-1]) | |
open(file_name, 'wb').write(r.content) | |
print("Done downloading.") | |
return | |
pool = Pool(50) | |
urls = [] | |
for piece in pieces: | |
urls.append(PLAYLIST_BASE + piece) | |
pool.map(job, urls) | |
print("All done?") | |
print("Topime kokku kah.") | |
os.system('ffmpeg -i "concat:' + "|".join(pieces) + '" -c copy VabadMehedX.mp4') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment