change downloader to aria2c for better speed

This commit is contained in:
tnt2402
2023-02-20 19:10:45 +07:00
parent 2a13613d9a
commit 66b3c1fa6b
3 changed files with 79 additions and 66 deletions

BIN
aria2c.exe Normal file

Binary file not shown.

View File

@@ -10,15 +10,19 @@ import json
from time import sleep
from tqdm import tqdm
#############
def ascii_banner(text):
print('\n\n########################################################\n\n\n')
ascii_banner = pyfiglet.figlet_format(text)
print(ascii_banner)
################################
# duration = 1000
download_dir = os.getcwd()
def ph_config_dl_dir(dir):
global download_dir
if (dir != None):
@@ -31,26 +35,43 @@ def ph_config_dl_dir(dir):
if (os.path.exists(download_dir) != True):
os.mkdir(download_dir)
def run_command(command):
process = subprocess.Popen(command, stdout=subprocess.PIPE)
pbar = tqdm(total=100, unit= " percents")
state = 0
while True:
output = process.stdout.readline().decode("utf-8")
if output == '' and process.poll() is not None:
break
if '[download' in output:
# print(output.strip())
if 'frag' in output:
current_per = float(output.split('%')[0].replace('[download]', '').strip())
try:
process = subprocess.Popen(
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=False)
pbar = tqdm(total=100, unit=" percents")
state = 0
current_frag = 0
total_frag = 1
while True:
output = process.stdout.readline().decode("utf-8")
if "[download] 100" in output:
result_txt = 'Result: ' + output.split("[download]")[-1].strip()
if output == '' and process.poll() is not None:
break
if 'Total fragments' in output:
total_frag = int(output.split(':')[1].strip())
if 'FILE:' in output:
current_frag = int(output.split('.part-Frag')[1].strip())
current_per = int(current_frag / total_frag * 100)
pbar.update(current_per - state)
state = current_per
sleep(0.00001)
pbar.close()
rc = process.poll()
return rc
pbar.update(100 - state)
pbar.close()
rc = process.poll()
print(result_txt)
return rc
except Exception as e:
print(e)
sys.exit(0)
# Main functions
### Main functions
def ph_check_valid_pornhub_url(url):
if ('pornhub.com' not in url):
print('Invalid Pornhub URL')
@@ -59,40 +80,17 @@ def ph_check_valid_pornhub_url(url):
url = 'https://www.' + url
return url
def download_video(url, filename):
# global duration
state = 0
print('[+] Save as: ' + filename + '\n')
try:
text = ''
# pbar = tqdm(total=duration, unit= " fragments")
# p = subprocess.Popen(['.\yt-dlp', '--no-warnings', '-R', '5', '--fragment-retries', '5', '--hls-prefer-ffmpeg', '--ffmpeg-location', os.getcwd(), '-o', filename, url], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell = False)
# p = subprocess.Popen(['.\yt-dlp', '--no-warnings', '--newline', '-R', '5', '-N', '5', '--windows-filenames', '--no-part', '--concurrent-fragments', '5','--fixup', 'never', '-o', filename, url], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell = False)
run_command(['.\yt-dlp', '--no-warnings', '--newline', '-R', '5', '-N', '5', '--windows-filenames', '--no-part', '--concurrent-fragments', '2','--fixup', 'never', '-o', filename, url])
# for line in iter(p.stdout.readline, b''):
# line = line.decode('utf-8').strip()
# # text = text + line
# # if ("[ffmpeg] " in line):
# # print('\n' + line.strip())
# # if "time=" in line:
# # time = line.split("time=")[1][:8]
# # hour, minute, second = time.split(":")
# # progress_time = 3600 * int(hour) + 60 * int(minute) + int(second)
# # pbar.update(progress_time - state)
# # state = progress_time
# # sleep(0.001)
# # elif "Failed" in line:
# # print("\nFailed while processing")
# # print(text)
# # sys.exit(1)
# if "download" in line:
# print(line)
run_command(['.\yt-dlp', '--downloader', 'aria2c', '--downloader-args',
'aria2c:-x 8 --log-level=info --file-allocation=none --summary-interval=1', '--no-warnings', '--newline', '-o', filename, url])
# pbar.update(duration)
# pbar.close()
print("\n[$] Video download successfully!")
print("[$] Video download successfully!")
except KeyboardInterrupt:
os.kill(p.pid, signal.CTRL_C_EVENT)
sys.exit()
@@ -105,12 +103,14 @@ def fix_title(s):
decoded_unicode = decoded_unicode.replace(i, '')
return decoded_unicode
def check_output_dir(model_name):
global download_dir
download_dir = download_dir
if (os.path.exists(download_dir + model_name) != True):
os.mkdir(download_dir+model_name)
def ph_download_video(url, model_name, filename):
try:
@@ -120,6 +120,7 @@ def ph_download_video(url, model_name, filename):
except:
print('Cannot download video')
def ph_download_playlist(url, model_name, limit):
global download_dir
# global duration
@@ -127,9 +128,11 @@ def ph_download_playlist(url, model_name, limit):
# tmp_playlist download
print('... Getting playlist information...')
playlist_download_command = [".\yt-dlp", "-j", "--flat-playlist", "--no-check-certificate", url]
res = subprocess.run(playlist_download_command, capture_output=True, text=True).stdout.split("\n")
if (limit != 0 ):
playlist_download_command = [".\yt-dlp", "-j",
"--flat-playlist", "--no-check-certificate", url]
res = subprocess.run(playlist_download_command,
capture_output=True, text=True).stdout.split("\n")
if (limit != 0):
print("[!] Limit: {} videos".format(limit))
count = 0
@@ -142,23 +145,28 @@ def ph_download_playlist(url, model_name, limit):
# video_dict = ast.literal_eval(res[i])
video_dict = json.loads(res[i])
url = video_dict['url']
p = subprocess.Popen(['.\yt-dlp', '--no-warnings', '--dump-json', '--skip-download', url], stdout=subprocess.PIPE, stderr=None, shell = True)
p = subprocess.Popen(['.\yt-dlp', '--no-warnings', '--dump-json',
'--skip-download', url], stdout=subprocess.PIPE, stderr=None, shell=True)
output = p.communicate()[0]
info = json.loads(output.decode('utf-8'))
# duration = int(info['duration'])
filename = os.path.join(download_dir, model_name, fix_title(str(info["title"])) + '.' + str(info['ext']))
print("\n\n==========================================\n[+] File #{}: {}".format(count, fix_title(str(info["title"])) + '.' + str(info['ext'])))
filename = os.path.join(download_dir, model_name, fix_title(
str(info["title"])) + '.' + str(info['ext']))
print("\n\n==========================================\n[+] File #{}: {}".format(
count, fix_title(str(info["title"])) + '.' + str(info['ext'])))
ph_download_video(url, model_name, filename)
count = count + 1
except KeyboardInterrupt:
print("Cannot download video")
sys.exit()
def fix_url(url, type):
url = ph_check_valid_pornhub_url(url)
if '/model/' not in url:
p = subprocess.Popen(['.\yt-dlp', '--no-warnings', '--dump-json', '--skip-download', url], stdout=subprocess.PIPE, stderr=None, shell = True)
p = subprocess.Popen(['.\yt-dlp', '--no-warnings', '--dump-json',
'--skip-download', url], stdout=subprocess.PIPE, stderr=None, shell=True)
output = p.communicate()[0]
info = json.loads(output.decode('utf-8'))
model_name = info['uploader']
@@ -175,20 +183,25 @@ def fix_url(url, type):
print("[+] Model: " + model_name)
return (url, model_name)
def ph_get_video(url):
# global duration
url = ph_check_valid_pornhub_url(url)
p = subprocess.Popen(['.\yt-dlp', '--no-warnings', '--dump-json', '--skip-download', url], stdout=subprocess.PIPE, stderr=None, shell = True)
p = subprocess.Popen(['.\yt-dlp', '--no-warnings', '--dump-json',
'--skip-download', url], stdout=subprocess.PIPE, stderr=None, shell=True)
output = p.communicate()[0]
info = json.loads(output.decode('utf-8'))
model_name = info['uploader']
# duration = int(info['duration'])
filename = os.path.join(download_dir, model_name, fix_title(str(info["title"])) + '.' + str(info['ext']))
filename = os.path.join(download_dir, model_name, fix_title(
str(info["title"])) + '.' + str(info['ext']))
print("[+] Model: " + model_name)
print("[+] Filename: " + fix_title(str(info["title"])) + '.' + str(info['ext']))
print("[+] Filename: " + fix_title(str(info["title"])) +
'.' + str(info['ext']))
ph_download_video(url, model_name, filename)
def ph_get_playlist(url, type, limit):
url, model_name = fix_url(url, type)
ph_download_playlist(url, model_name, limit)

View File

@@ -16,7 +16,7 @@ else:
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--url', type=str, help = 'URL of Pornhub video')
parser.add_argument('--playlist', type=str, default='best', const='best', nargs='?', choices = ['most-viewed', 'best', 'top-rated', 'longest'], help = 'Optional ordering of videos')
parser.add_argument('--playlist', nargs='?', const='best', choices = ['most-viewed', 'best', 'top-rated', 'longest'], help = 'Optional ordering of videos')
parser.add_argument('--limit', type=int, help = 'Maximum number of videos', default=0)
parser.add_argument('--dir', type=str, help = 'Output directory')
args = parser.parse_args()