Added Aria2c

Aria2c is now used for downloading segments as fast as possible (Closes #20)
This commit is contained in:
Puyodead1 2021-05-27 21:18:26 -04:00
parent 67748301de
commit 3cc22520c8
2 changed files with 68 additions and 51 deletions

3
.gitignore vendored
View File

@ -120,4 +120,5 @@ out_dir
working_dir working_dir
manifest.mpd manifest.mpd
.vscode .vscode
saved saved
*.aria2

116
main.py
View File

@ -8,6 +8,7 @@ from requests.exceptions import ConnectionError as conn_error
from html.parser import HTMLParser as compat_HTMLParser from html.parser import HTMLParser as compat_HTMLParser
from sanitize import sanitize, slugify, SLUG_OK from sanitize import sanitize, slugify, SLUG_OK
from pyffmpeg import FFMPeg as FFMPEG from pyffmpeg import FFMPeg as FFMPEG
import subprocess
home_dir = os.getcwd() home_dir = os.getcwd()
download_dir = os.path.join(os.getcwd(), "out_dir") download_dir = os.path.join(os.getcwd(), "out_dir")
@ -830,66 +831,81 @@ def handle_segments(video_source, audio_source, video_title,
""" """
@author Jayapraveen @author Jayapraveen
""" """
no_segments = video_source.get("segment_count") no_vid_segments = video_source.get("segment_count")
no_aud_segments = audio_source.get("segment_count")
audio_url = audio_source.get("media") audio_media = audio_source.get("media")
audio_init = audio_source.get("initialization") audio_init = audio_source.get("initialization")
audio_extension = audio_source.get("extension") audio_extension = audio_source.get("extension")
video_url = video_source.get("media") video_media = video_source.get("media")
video_init = video_source.get("initialization") video_init = video_source.get("initialization")
video_extension = video_source.get("extension") video_extension = video_source.get("extension")
no_segments += 10 # because the download_media function relies on hitting a 404 to know when to finish audio_urls = audio_init + "\n dir={}\n out=audio_0.mp4\n".format(
lecture_working_dir)
video_urls = video_init + "\n dir={}\n out=video_0.mp4\n".format(
lecture_working_dir)
download_media("video_0.seg.mp4", video_init, lecture_working_dir) list_path = os.path.join(lecture_working_dir, "list.txt")
video_kid = extract_kid(
os.path.join(lecture_working_dir, "video_0.seg.mp4")) for i in range(1, no_aud_segments):
audio_urls += audio_media.replace(
"$Number$", str(i)) + "\n dir={}\n out=audio_{}.mp4\n".format(
lecture_working_dir, i)
for i in range(1, no_vid_segments):
video_urls += video_media.replace(
"$Number$", str(i)) + "\n dir={}\n out=video_{}.mp4\n".format(
lecture_working_dir, i)
with open(list_path, 'w') as f:
f.write("{}\n{}".format(audio_urls, video_urls))
f.close()
print("> Downloading Lecture Segments...")
ret_code = subprocess.Popen([
"aria2c", "-i", list_path, "-j16", "-s20", "-x16", "-c",
"--auto-file-renaming=false", "--summary-interval=0"
]).wait()
print("> Lecture Segments Downloaded")
print("Return code: " + str(ret_code))
os.remove(list_path)
video_kid = extract_kid(os.path.join(lecture_working_dir, "video_0.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)
audio_kid = extract_kid( audio_kid = extract_kid(os.path.join(lecture_working_dir, "audio_0.mp4"))
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_segments):
video_segment_url = video_url.replace("$Number$", str(count)) os.chdir(lecture_working_dir)
audio_segment_url = audio_url.replace("$Number$", str(count))
video_status = download_media( if os.name == "nt":
f"video_{str(count)}.seg.{video_extension}", video_segment_url, video_concat_command = "copy /b " + "+".join([
lecture_working_dir) f"video_{i}.{video_extension}" for i in range(0, no_vid_segments)
audio_status = download_media( ]) + " encrypted_video.mp4"
f"audio_{str(count)}.seg.{audio_extension}", audio_segment_url, audio_concat_command = "copy /b " + "+".join([
lecture_working_dir) f"audio_{i}.{audio_extension}" for i in range(0, no_aud_segments)
os.chdir(lecture_working_dir) ]) + " encrypted_audio.mp4"
if (video_status): else:
if os.name == "nt": video_concat_command = "cat " + " ".join([
video_concat_command = "copy /b " + "+".join([ f"video_{i}.{video_extension}" for i in range(0, no_aud_segments)
f"video_{i}.seg.{video_extension}" ]) + " > encrypted_video.mp4"
for i in range(0, count) audio_concat_command = "cat " + " ".join([
]) + " encrypted_video.mp4" f"audio_{i}.{audio_extension}" for i in range(0, no_vid_segments)
audio_concat_command = "copy /b " + "+".join([ ]) + " > encrypted_audio.mp4"
f"audio_{i}.seg.{audio_extension}" os.system(video_concat_command)
for i in range(0, count) os.system(audio_concat_command)
]) + " encrypted_audio.mp4" os.chdir(home_dir)
else: try:
video_concat_command = "cat " + " ".join([ decrypt(video_kid, "video", lecture_working_dir)
f"video_{i}.seg.{video_extension}" decrypt(audio_kid, "audio", lecture_working_dir)
for i in range(0, count) os.chdir(home_dir)
]) + " > encrypted_video.mp4" mux_process(video_title, lecture_working_dir, output_path)
audio_concat_command = "cat " + " ".join([ cleanup(lecture_working_dir)
f"audio_{i}.seg.{audio_extension}" except Exception as e:
for i in range(0, count) print(f"Error: " + e)
]) + " > encrypted_audio.mp4"
os.system(video_concat_command)
os.system(audio_concat_command)
try:
decrypt(video_kid, "video", lecture_working_dir)
decrypt(audio_kid, "audio", lecture_working_dir)
os.chdir(home_dir)
mux_process(video_title, lecture_working_dir, output_path)
cleanup(lecture_working_dir)
except Exception as e:
print(f"Error: " + e)
break
def download(url, path, filename): def download(url, path, filename):