mirror of
https://cdm-project.com/Download-Tools/udemy-downloader.git
synced 2025-04-30 17:44:33 +02:00
Path updates
- Removed a few unused imports + Reworked the way paths are formed so they shouldn't be a problem on other operating systems NOTE: the new dependencies in requirements.txt are NOT required at this time
This commit is contained in:
parent
f62bb52816
commit
88a411d708
86
main.py
86
main.py
@ -1,22 +1,16 @@
|
|||||||
import os, requests, shutil, json, glob, urllib.request, argparse, sys, re
|
import os, requests, json, glob, argparse, sys, re
|
||||||
from sanitize_filename import sanitize
|
from sanitize_filename import sanitize
|
||||||
import urllib.request
|
|
||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from mpegdash.parser import MPEGDASHParser
|
from mpegdash.parser import MPEGDASHParser
|
||||||
from mpegdash.nodes import Descriptor
|
|
||||||
from mpegdash.utils import (parse_attr_value, parse_child_nodes,
|
|
||||||
parse_node_value, write_attr_value,
|
|
||||||
write_child_node, write_node_value)
|
|
||||||
from utils import extract_kid
|
from utils import extract_kid
|
||||||
from vtt_to_srt import convert
|
from vtt_to_srt import convert
|
||||||
|
|
||||||
download_dir = "%s\out_dir" % os.getcwd()
|
download_dir = os.path.join(os.getcwd(), "out_dir")
|
||||||
working_dir = "%s\working_dir" % os.getcwd(
|
working_dir = os.path.join(os.getcwd(), "working_dir")
|
||||||
) # set the folder to download segments for DRM videos
|
|
||||||
retry = 3
|
retry = 3
|
||||||
home_dir = os.getcwd()
|
home_dir = os.getcwd()
|
||||||
keyfile_path = "%s\keyfile.json" % os.getcwd()
|
keyfile_path = os.path.join(os.getcwd(), "keyfile.json")
|
||||||
valid_qualities = [144, 360, 480, 720, 1080]
|
valid_qualities = [144, 360, 480, 720, 1080]
|
||||||
|
|
||||||
if not os.path.exists(working_dir):
|
if not os.path.exists(working_dir):
|
||||||
@ -86,7 +80,7 @@ def download_media(filename, url, lecture_working_dir, epoch=0):
|
|||||||
unit='B',
|
unit='B',
|
||||||
unit_scale=True,
|
unit_scale=True,
|
||||||
desc=filename)
|
desc=filename)
|
||||||
with open(f"{lecture_working_dir}\\{filename}",
|
with open(os.path.join(lecture_working_dir, filename),
|
||||||
'wb') as video_file:
|
'wb') as video_file:
|
||||||
for chunk in media.iter_content(chunk_size=1024):
|
for chunk in media.iter_content(chunk_size=1024):
|
||||||
if chunk:
|
if chunk:
|
||||||
@ -135,9 +129,15 @@ def mux_process(video_title, lecture_working_dir, outfile):
|
|||||||
@author Jayapraveen
|
@author Jayapraveen
|
||||||
"""
|
"""
|
||||||
if os.name == "nt":
|
if os.name == "nt":
|
||||||
command = f"ffmpeg -y -i \"{lecture_working_dir}\\decrypted_audio.mp4\" -i \"{lecture_working_dir}\\decrypted_video.mp4\" -acodec copy -vcodec copy -fflags +bitexact -map_metadata -1 -metadata title=\"{video_title}\" \"{outfile}\""
|
command = "ffmpeg -y -i \"{}\" -i \"{}\" -acodec copy -vcodec copy -fflags +bitexact -map_metadata -1 -metadata title=\"{}\" \"{}\"".format(
|
||||||
|
os.path.join(lecture_working_dir, "decrypted_audio.mp4"),
|
||||||
|
os.path.join(lecture_working_dir, "decrypted_video.mp4"),
|
||||||
|
video_title, outfile)
|
||||||
else:
|
else:
|
||||||
command = f"nice -n 7 ffmpeg -y -i \"{lecture_working_dir}\\decrypted_audio.mp4\" -i \"{lecture_working_dir}\\decrypted_video.mp4\" -acodec copy -vcodec copy -fflags +bitexact -map_metadata -1 -metadata title=\"{video_title}\" \"{outfile}\""
|
command = "nice -n 7 ffmpeg -y -i \"{}\" -i \"{}\" -acodec copy -vcodec copy -fflags +bitexact -map_metadata -1 -metadata title=\"{}\" \"{}\"".format(
|
||||||
|
os.path.join(lecture_working_dir, "decrypted_audio.mp4"),
|
||||||
|
os.path.join(lecture_working_dir, "decrypted_video.mp4"),
|
||||||
|
video_title, outfile)
|
||||||
os.system(command)
|
os.system(command)
|
||||||
|
|
||||||
|
|
||||||
@ -150,13 +150,19 @@ def decrypt(kid, filename, lecture_working_dir):
|
|||||||
except KeyError as error:
|
except KeyError as error:
|
||||||
exit("Key not found")
|
exit("Key not found")
|
||||||
if (os.name == "nt"):
|
if (os.name == "nt"):
|
||||||
os.system(
|
os.system("mp4decrypt --key 1:{} \"{}\" \"{}\"".format(
|
||||||
f"mp4decrypt --key 1:{key} \"{lecture_working_dir}\\encrypted_{filename}.mp4\" \"{lecture_working_dir}\\decrypted_{filename}.mp4\""
|
key,
|
||||||
)
|
os.path.join(lecture_working_dir,
|
||||||
|
"encrypted_{}.mp4".format(filename)),
|
||||||
|
os.path.join(lecture_working_dir,
|
||||||
|
"decrypted{}.mp4".format(filename))))
|
||||||
else:
|
else:
|
||||||
os.system(
|
os.system("nice -n 7 mp4decrypt --key 1:{} \"{}\" \"{}\"".format(
|
||||||
f"nice -n 7 mp4decrypt --key 1:{key} \"{lecture_working_dir}\\encrypted_{filename}.mp4\" \"{lecture_working_dir}\\decrypted_{filename}.mp4\""
|
key,
|
||||||
)
|
os.path.join(lecture_working_dir,
|
||||||
|
"encrypted_{}.mp4".format(filename)),
|
||||||
|
os.path.join(lecture_working_dir,
|
||||||
|
"decrypted{}.mp4".format(filename))))
|
||||||
|
|
||||||
|
|
||||||
def handle_irregular_segments(media_info, video_title, lecture_working_dir,
|
def handle_irregular_segments(media_info, video_title, lecture_working_dir,
|
||||||
@ -166,10 +172,12 @@ def handle_irregular_segments(media_info, video_title, lecture_working_dir,
|
|||||||
"""
|
"""
|
||||||
no_segment, video_url, video_init, video_extension, no_segment, audio_url, audio_init, audio_extension = media_info
|
no_segment, video_url, video_init, video_extension, no_segment, audio_url, audio_init, audio_extension = media_info
|
||||||
download_media("video_0.seg.mp4", video_init, lecture_working_dir)
|
download_media("video_0.seg.mp4", video_init, lecture_working_dir)
|
||||||
video_kid = extract_kid(f"{lecture_working_dir}\\video_0.seg.mp4")
|
video_kid = extract_kid(
|
||||||
|
os.path.join(lecture_working_dir, "video_0.seg.mp4"))
|
||||||
print("KID for video file is: " + video_kid)
|
print("KID for video file is: " + video_kid)
|
||||||
download_media("audio_0.seg.mp4", audio_init, lecture_working_dir)
|
download_media("audio_0.seg.mp4", audio_init, lecture_working_dir)
|
||||||
audio_kid = extract_kid(f"{lecture_working_dir}\\audio_0.seg.mp4")
|
audio_kid = extract_kid(
|
||||||
|
os.path.join(lecture_working_dir, "audio_0.seg.mp4"))
|
||||||
print("KID for audio file is: " + audio_kid)
|
print("KID for audio file is: " + audio_kid)
|
||||||
for count in range(1, no_segment):
|
for count in range(1, no_segment):
|
||||||
video_segment_url = video_url.replace("$Number$", str(count))
|
video_segment_url = video_url.replace("$Number$", str(count))
|
||||||
@ -313,7 +321,7 @@ def process_caption(caption,
|
|||||||
caption.get("locale_id"), caption.get("ext"))
|
caption.get("locale_id"), caption.get("ext"))
|
||||||
filename_no_ext = f"%s. %s_%s" % (lecture_index, sanitize(lecture_title),
|
filename_no_ext = f"%s. %s_%s" % (lecture_index, sanitize(lecture_title),
|
||||||
caption.get("locale_id"))
|
caption.get("locale_id"))
|
||||||
filepath = f"%s\\%s" % (lecture_dir, filename)
|
filepath = os.path.join(lecture_dir, filename)
|
||||||
|
|
||||||
if os.path.isfile(filepath):
|
if os.path.isfile(filepath):
|
||||||
print("> Captions '%s' already downloaded." % filename)
|
print("> Captions '%s' already downloaded." % filename)
|
||||||
@ -374,8 +382,8 @@ def process_lecture(lecture, lecture_index, lecture_path, lecture_dir, quality,
|
|||||||
# encrypted
|
# encrypted
|
||||||
print(f"> Lecture '%s' has DRM, attempting to download" %
|
print(f"> Lecture '%s' has DRM, attempting to download" %
|
||||||
lecture_title)
|
lecture_title)
|
||||||
lecture_working_dir = "%s\%s" % (
|
lecture_working_dir = os.path.join(
|
||||||
working_dir, lecture_asset["id"]
|
working_dir, str(lecture_asset["id"])
|
||||||
) # set the folder to download ephemeral files
|
) # set the folder to download ephemeral files
|
||||||
media_sources = lecture_asset["media_sources"]
|
media_sources = lecture_asset["media_sources"]
|
||||||
if not os.path.exists(lecture_working_dir):
|
if not os.path.exists(lecture_working_dir):
|
||||||
@ -411,7 +419,7 @@ def process_lecture(lecture, lecture_index, lecture_path, lecture_dir, quality,
|
|||||||
if download_url:
|
if download_url:
|
||||||
try:
|
try:
|
||||||
download(download_url,
|
download(download_url,
|
||||||
f"%s\\%s" % (lecture_dir, asset_filename),
|
os.path.join(lecture_dir, asset_filename),
|
||||||
asset_filename)
|
asset_filename)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(
|
print(
|
||||||
@ -420,19 +428,18 @@ def process_lecture(lecture, lecture_index, lecture_path, lecture_dir, quality,
|
|||||||
continue
|
continue
|
||||||
elif asset["asset_type"] == "Article":
|
elif asset["asset_type"] == "Article":
|
||||||
assets.append(asset)
|
assets.append(asset)
|
||||||
asset_path = f"%s\\%s.html" % (lecture_dir,
|
asset_path = os.path.join(lecture_dir, sanitize(lecture_title))
|
||||||
sanitize(lecture_title))
|
|
||||||
with open(asset_path, 'w') as f:
|
with open(asset_path, 'w') as f:
|
||||||
f.write(asset["body"])
|
f.write(asset["body"])
|
||||||
elif asset["asset_type"] == "ExternalLink":
|
elif asset["asset_type"] == "ExternalLink":
|
||||||
assets.append(asset)
|
assets.append(asset)
|
||||||
asset_path = f"%s\\%s. External URLs.txt" % (lecture_dir,
|
asset_path = os.path.join(
|
||||||
lecture_index)
|
lecture_dir, "{}. External URLs.txt".format(lecture_index))
|
||||||
# with open(asset_path, 'a') as f:
|
# with open(asset_path, 'a') as f:
|
||||||
# f.write(f"%s : %s\n" %
|
# f.write(f"%s : %s\n" %
|
||||||
# (asset["title"], asset["external_url"]))
|
# (asset["title"], asset["external_url"]))
|
||||||
text_assets += f"%s: %s\n" % (asset["title"],
|
text_assets += "{}: {}\n".format(asset["title"],
|
||||||
asset["external_url"])
|
asset["external_url"])
|
||||||
|
|
||||||
if not text_assets == "":
|
if not text_assets == "":
|
||||||
with open(asset_path, 'w') as f:
|
with open(asset_path, 'w') as f:
|
||||||
@ -470,7 +477,7 @@ def process_lecture(lecture, lecture_index, lecture_path, lecture_dir, quality,
|
|||||||
|
|
||||||
def parse(data, course_id, course_name, skip_lectures, dl_assets, dl_captions,
|
def parse(data, course_id, course_name, skip_lectures, dl_assets, dl_captions,
|
||||||
quality, caption_locale):
|
quality, caption_locale):
|
||||||
course_dir = f"%s\\%s" % (download_dir, course_name)
|
course_dir = os.path.join(download_dir, course_name)
|
||||||
if not os.path.exists(course_dir):
|
if not os.path.exists(course_dir):
|
||||||
os.mkdir(course_dir)
|
os.mkdir(course_dir)
|
||||||
chapters = []
|
chapters = []
|
||||||
@ -488,22 +495,25 @@ def parse(data, course_id, course_name, skip_lectures, dl_assets, dl_captions,
|
|||||||
# This is caused by there not being a starting chapter
|
# This is caused by there not being a starting chapter
|
||||||
lectures.append(obj)
|
lectures.append(obj)
|
||||||
lecture_index = lectures.index(obj) + 1
|
lecture_index = lectures.index(obj) + 1
|
||||||
lecture_path = f"%s\\%s. %s.mp4" % (course_dir, lecture_index,
|
lecture_path = os.path.join(
|
||||||
sanitize(obj["title"]))
|
course_dir, "{}. {}.mp4".format(lecture_index,
|
||||||
|
sanitize(obj["title"])))
|
||||||
process_lecture(obj, lecture_index, lecture_path, download_dir,
|
process_lecture(obj, lecture_index, lecture_path, download_dir,
|
||||||
quality, skip_lectures, dl_assets, dl_captions,
|
quality, skip_lectures, dl_assets, dl_captions,
|
||||||
caption_locale)
|
caption_locale)
|
||||||
|
|
||||||
for chapter in chapters:
|
for chapter in chapters:
|
||||||
chapter_dir = f"%s\\%s. %s" % (course_dir, chapters.index(chapter) + 1,
|
chapter_dir = os.path.join(
|
||||||
sanitize(chapter["title"]))
|
course_dir, "{}. {}".format(
|
||||||
|
chapters.index(chapter) + 1, sanitize(chapter["title"])))
|
||||||
if not os.path.exists(chapter_dir):
|
if not os.path.exists(chapter_dir):
|
||||||
os.mkdir(chapter_dir)
|
os.mkdir(chapter_dir)
|
||||||
|
|
||||||
for lecture in chapter["lectures"]:
|
for lecture in chapter["lectures"]:
|
||||||
lecture_index = chapter["lectures"].index(lecture) + 1
|
lecture_index = chapter["lectures"].index(lecture) + 1
|
||||||
lecture_path = f"%s\\%s. %s.mp4" % (chapter_dir, lecture_index,
|
lecture_path = os.path.join(
|
||||||
sanitize(lecture["title"]))
|
chapter_dir, "{}. {}.mp4".format(lecture_index,
|
||||||
|
sanitize(lecture["title"])))
|
||||||
process_lecture(lecture, lecture_index, lecture_path, chapter_dir,
|
process_lecture(lecture, lecture_index, lecture_path, chapter_dir,
|
||||||
quality, skip_lectures, dl_assets, dl_captions,
|
quality, skip_lectures, dl_assets, dl_captions,
|
||||||
caption_locale)
|
caption_locale)
|
||||||
|
@ -5,4 +5,6 @@ requests
|
|||||||
python-dotenv
|
python-dotenv
|
||||||
protobuf
|
protobuf
|
||||||
webvtt-py
|
webvtt-py
|
||||||
pysrt
|
pysrt
|
||||||
|
aiohttp
|
||||||
|
aiofiles
|
Loading…
x
Reference in New Issue
Block a user