Downloading several videos at the same time using pytube
Question:
I made a program that is able to download and convert the youtube playlist video into mp3. However, this program does one task at a time. Is it possible to make so that it can download and convert several videos at the same time?
I tried searching up in the Google but could not find a method that satisfy my needs.
May you give me some clue or things that I need to look it up to help me breakthrough this problem?
Answers:
You can try python Threading. Create a list of url’s that u need to download and create a thread instance for each of the URL. U can refer to this site for more clarity.
https://www.shanelynn.ie/using-python-threading-for-multiple-results-queue/
First, i created a list of videos ids.
then i create a download function as follow
from pytube import YouTube
import time
def func(idx):
file_name=idx+'.mp4'
video_link='https://www.youtube.com/watch?v={}'.format(idx)
if not os.path.isfile(os.path.join('videos',file_name)):#if file dont exist already, download it
try:#if video fail to download such a private video or delted video
yt = YouTube(video_link)
yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').asc().first().download(filename=file_name,output_path='videos')
time.sleep(5)
except Exception as e:
print(e)
pass
Then i call this function in parallel
from multiprocessing import Pool
import os
with Pool(4) as p:
features = p.map(func, ids_list)
I made a program that is able to download and convert the youtube playlist video into mp3. However, this program does one task at a time. Is it possible to make so that it can download and convert several videos at the same time?
I tried searching up in the Google but could not find a method that satisfy my needs.
May you give me some clue or things that I need to look it up to help me breakthrough this problem?
You can try python Threading. Create a list of url’s that u need to download and create a thread instance for each of the URL. U can refer to this site for more clarity.
https://www.shanelynn.ie/using-python-threading-for-multiple-results-queue/
First, i created a list of videos ids.
then i create a download function as follow
from pytube import YouTube
import time
def func(idx):
file_name=idx+'.mp4'
video_link='https://www.youtube.com/watch?v={}'.format(idx)
if not os.path.isfile(os.path.join('videos',file_name)):#if file dont exist already, download it
try:#if video fail to download such a private video or delted video
yt = YouTube(video_link)
yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').asc().first().download(filename=file_name,output_path='videos')
time.sleep(5)
except Exception as e:
print(e)
pass
Then i call this function in parallel
from multiprocessing import Pool
import os
with Pool(4) as p:
features = p.map(func, ids_list)