diff --git a/main.py b/main.py index 8a8b30e..be88bb3 100644 --- a/main.py +++ b/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 -import urllib.request from tqdm import tqdm from dotenv import load_dotenv 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 vtt_to_srt import convert -download_dir = "%s\out_dir" % os.getcwd() -working_dir = "%s\working_dir" % os.getcwd( -) # set the folder to download segments for DRM videos +download_dir = os.path.join(os.getcwd(), "out_dir") +working_dir = os.path.join(os.getcwd(), "working_dir") retry = 3 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] if not os.path.exists(working_dir): @@ -86,7 +80,7 @@ def download_media(filename, url, lecture_working_dir, epoch=0): unit='B', unit_scale=True, desc=filename) - with open(f"{lecture_working_dir}\\{filename}", + with open(os.path.join(lecture_working_dir, filename), 'wb') as video_file: for chunk in media.iter_content(chunk_size=1024): if chunk: @@ -135,9 +129,15 @@ def mux_process(video_title, lecture_working_dir, outfile): @author Jayapraveen """ 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: - 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) @@ -150,13 +150,19 @@ def decrypt(kid, filename, lecture_working_dir): except KeyError as error: exit("Key not found") if (os.name == "nt"): - os.system( - f"mp4decrypt --key 1:{key} \"{lecture_working_dir}\\encrypted_{filename}.mp4\" \"{lecture_working_dir}\\decrypted_{filename}.mp4\"" - ) + os.system("mp4decrypt --key 1:{} \"{}\" \"{}\"".format( + key, + os.path.join(lecture_working_dir, + "encrypted_{}.mp4".format(filename)), + os.path.join(lecture_working_dir, + "decrypted{}.mp4".format(filename)))) else: - os.system( - f"nice -n 7 mp4decrypt --key 1:{key} \"{lecture_working_dir}\\encrypted_{filename}.mp4\" \"{lecture_working_dir}\\decrypted_{filename}.mp4\"" - ) + os.system("nice -n 7 mp4decrypt --key 1:{} \"{}\" \"{}\"".format( + 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, @@ -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 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) 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) for count in range(1, no_segment): video_segment_url = video_url.replace("$Number$", str(count)) @@ -313,7 +321,7 @@ def process_caption(caption, caption.get("locale_id"), caption.get("ext")) filename_no_ext = f"%s. %s_%s" % (lecture_index, sanitize(lecture_title), caption.get("locale_id")) - filepath = f"%s\\%s" % (lecture_dir, filename) + filepath = os.path.join(lecture_dir, filename) if os.path.isfile(filepath): print("> Captions '%s' already downloaded." % filename) @@ -374,8 +382,8 @@ def process_lecture(lecture, lecture_index, lecture_path, lecture_dir, quality, # encrypted print(f"> Lecture '%s' has DRM, attempting to download" % lecture_title) - lecture_working_dir = "%s\%s" % ( - working_dir, lecture_asset["id"] + lecture_working_dir = os.path.join( + working_dir, str(lecture_asset["id"]) ) # set the folder to download ephemeral files media_sources = lecture_asset["media_sources"] 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: try: download(download_url, - f"%s\\%s" % (lecture_dir, asset_filename), + os.path.join(lecture_dir, asset_filename), asset_filename) except Exception as e: print( @@ -420,19 +428,18 @@ def process_lecture(lecture, lecture_index, lecture_path, lecture_dir, quality, continue elif asset["asset_type"] == "Article": assets.append(asset) - asset_path = f"%s\\%s.html" % (lecture_dir, - sanitize(lecture_title)) + asset_path = os.path.join(lecture_dir, sanitize(lecture_title)) with open(asset_path, 'w') as f: f.write(asset["body"]) elif asset["asset_type"] == "ExternalLink": assets.append(asset) - asset_path = f"%s\\%s. External URLs.txt" % (lecture_dir, - lecture_index) + asset_path = os.path.join( + lecture_dir, "{}. External URLs.txt".format(lecture_index)) # with open(asset_path, 'a') as f: # f.write(f"%s : %s\n" % # (asset["title"], asset["external_url"])) - text_assets += f"%s: %s\n" % (asset["title"], - asset["external_url"]) + text_assets += "{}: {}\n".format(asset["title"], + asset["external_url"]) if not text_assets == "": 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, 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): os.mkdir(course_dir) 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 lectures.append(obj) lecture_index = lectures.index(obj) + 1 - lecture_path = f"%s\\%s. %s.mp4" % (course_dir, lecture_index, - sanitize(obj["title"])) + lecture_path = os.path.join( + course_dir, "{}. {}.mp4".format(lecture_index, + sanitize(obj["title"]))) process_lecture(obj, lecture_index, lecture_path, download_dir, quality, skip_lectures, dl_assets, dl_captions, caption_locale) for chapter in chapters: - chapter_dir = f"%s\\%s. %s" % (course_dir, chapters.index(chapter) + 1, - sanitize(chapter["title"])) + chapter_dir = os.path.join( + course_dir, "{}. {}".format( + chapters.index(chapter) + 1, sanitize(chapter["title"]))) if not os.path.exists(chapter_dir): os.mkdir(chapter_dir) for lecture in chapter["lectures"]: lecture_index = chapter["lectures"].index(lecture) + 1 - lecture_path = f"%s\\%s. %s.mp4" % (chapter_dir, lecture_index, - sanitize(lecture["title"])) + lecture_path = os.path.join( + chapter_dir, "{}. {}.mp4".format(lecture_index, + sanitize(lecture["title"]))) process_lecture(lecture, lecture_index, lecture_path, chapter_dir, quality, skip_lectures, dl_assets, dl_captions, caption_locale) diff --git a/requirements.txt b/requirements.txt index 23940f4..18e0dae 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,6 @@ requests python-dotenv protobuf webvtt-py -pysrt \ No newline at end of file +pysrt +aiohttp +aiofiles \ No newline at end of file