-
Notifications
You must be signed in to change notification settings - Fork 2
/
mymedia-downloader.py
executable file
·88 lines (79 loc) · 2.67 KB
/
mymedia-downloader.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
#! /usr/bin/env python3
import ffmpeg
import sys
import re
playlist_url = (
"https://stream.library.utoronto.ca:1935"
"/MyMedia/play/mp4:1/{}.mp4/playlist.m3u8"
)
# This is pretty terrible performance-wise but honestly I don't care that much
video_id_pattern = "(?<=embed/)|(?<=watch/)[0-9a-zA-Z]*"
# Check if argc is correct
if len(sys.argv) != 3:
print(
"Usage: py " +
sys.argv[0] +
(
" (file containing one URL per line) "
"(output folder with trailing slash)"
)
)
exit()
print(
(
"This will not work unless ffmpeg is installed and in your path. "
"Read the GitHub page if you do not know how to do that.")
)
yn = input("Is ffmpeg installed and in your path? [Y/n] ")
if yn != "Y" and yn != "y":
print("Please go do that and run me again.")
exit()
# Get MyMedia URLs
fp = open(sys.argv[1], "r")
# Set globals
# Video URLs
video_urls = fp.readlines()
# Output path
slash = '\\' if sys.platform == "win32" else '/'
# Add trailing back/slash if it isn't there
outpath = sys.argv[2] if sys.argv[2][-1] == slash else (sys.argv[2] + slash)
# We're done with this file
fp.close()
# List to hold all the video URLs
playlists = []
# List to hold all the video titles
titles = []
# Get video URLs and titles
for url in video_urls:
# Get video ID
video_id = re.search(video_id_pattern, url, flags=re.MULTILINE)
if video_id is None:
continue
video_id = video_id.group(0)
playlists.append(playlist_url.format(video_id))
# print(video_id)
print("Read {} video URLs, extracted {} playlists.".format(
len(video_urls),
len(playlists)))
print("")
print("Reading video titles is currently not supported.")
print("I'm going to fix this at some point but I need to study for finals")
print("So the files are just going to be numbered.")
titles = [str(x) for x in range(len(playlists))]
print("")
print("Also another thing -I promise I'll fix it after finals, but for now")
print("you need to specify beforehand if you want to overwrite files in")
print("the output directory. The file names will be:")
print("\n".join([x+".mp4" for x in titles]))
print("If these exist in the output directory, would you like them to")
yn = input("be overwritten? [Y/n] ")
overwrite_output = (yn == "Y" or yn == "y")
# Download videos
print("Starting download...")
for i, playlist in enumerate(playlists):
# print(playlist)
stream = ffmpeg.input(playlist)
stream = ffmpeg.output(stream, outpath + titles[i] + ".mp4", c="copy")
ffmpeg.run(stream, overwrite_output=overwrite_output, quiet=True)
print(".", end="")
print("\nDone.")