Last active
June 27, 2024 12:51
-
-
Save giraycoskun/dcba1e4731e08f24713813d2c80e4af9 to your computer and use it in GitHub Desktop.
Download parallel from m3u8 files
This file contains hidden or 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
| from subprocess import Popen, PIPE | |
| from multiprocessing import Pool, Value, Lock, Manager | |
| from os import getpid | |
| from loguru import logger | |
| def get_file(link, file_name): | |
| command = ["wget", "-O", file_name, link] | |
| process = Popen(command, stdout=PIPE, stderr=PIPE) | |
| output, error = process.communicate() | |
| logger.error(error) | |
| logger.info(output) | |
| if process.returncode == 0: | |
| print(f"Fetching file for {file_name} is successful") | |
| else: | |
| print(f"Fetching file for {file_name} has failed!") | |
| return process.returncode | |
| def process(input_name, output_name): | |
| command = [ | |
| "ffmpeg", | |
| "-protocol_whitelist", | |
| "file,http,https,tcp,tls,crypto", | |
| "-i", | |
| input_name, | |
| "-c", | |
| "copy", | |
| "-bsf:a", | |
| "aac_adtstoasc", | |
| output_name, | |
| ] | |
| process = Popen(command, stdout=PIPE, stderr=PIPE) | |
| output, error = process.communicate() | |
| logger.error(error) | |
| logger.info(output) | |
| if process.returncode == 0: | |
| print(f"Download process for {input_name} is successful") | |
| else: | |
| print(f"Download process for {input_name} has failed!") | |
| return process.returncode | |
| def task(name, id, download_link): | |
| tmp = str(id) | |
| if len(tmp) == 1: | |
| tmp = "0" + tmp | |
| name += tmp | |
| pid = getpid() | |
| print(f"Task with id: {pid} started for episode {name}") | |
| logger.remove(0) # Remove the default logger | |
| logger.add("m3u8.log", enqueue=True) | |
| if get_file(download_link, name + ".m3u8") == 0: | |
| if process(name + ".m3u8", name + ".mp4") == 0: | |
| return name + "successful" | |
| return name + "failed!" | |
| def main(download_link_list, episode_start, name): | |
| task_args = [ | |
| (name, i + episode_start, download_link_list[i]) | |
| for i in range(len(download_link_list)) | |
| ] | |
| with Pool() as pool: | |
| # Submit tasks to the pool using map | |
| results = pool.starmap(task, task_args) | |
| for result in results: | |
| print(result) | |
| if __name__ == "__main__": | |
| name = "S01E" | |
| episode_start = 1 | |
| download_link_list = [ | |
| ] | |
| main(download_link_list, episode_start, name) | |
| # ffmpeg -protocol_whitelist file,http,https,tcp,tls,crypto -i index.m3u8 -c copy -bsf:a aac_adtstoasc index.mp4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment