මට ඔන උනා ඔක්කොම බන කතා බාවනා කරන දෙවල් ටැබ් එකකට ඩවුන් ලොඩ් කරලා අහන්න දෙන්න. එකට මන් මෙවගේ එකක් ලියාගත්ත videos tika youtube eken eka parata download karaganna.
ඔන කෙනෙකුට යුස් කරන්න මන් මෙතන දානවා.
video_ids.txt kiyala file ekak hadala ekata mehema dagattahama hari.
https://pytube.io/en/latest/user/install.html
ඔන කෙනෙකුට යුස් කරන්න මන් මෙතන දානවා.
Python:
from pytube import YouTube, Playlist
from pytube.exceptions import VideoUnavailable
from multiprocessing import Pool, freeze_support
import os
import time
def remove_invalid_characters(text):
# Replace or remove invalid characters
invalid_characters = '<>:"/\\|?*'
for char in invalid_characters:
text = text.replace(char, '')
return text
def download_video(video_url):
def on_progress(stream, chunk, remaining):
total_size = stream.filesize
bytes_downloaded = total_size - remaining
# Calculate the download percentage
progress = (bytes_downloaded / total_size) * 100
print(f"Download progress for {video_url}: {progress:.2f}%")
try:
if "playlist" in video_url.lower():
playlist = Playlist(video_url)
playlist_title = remove_invalid_characters(playlist.title)[:50] # Truncate title for file name
output_path = 'videos/playlists'
# Ensure the 'videos/playlists' directory exists
if not os.path.exists(output_path):
os.makedirs(output_path)
for video in playlist.videos:
file_name = "{}.mp4".format(remove_invalid_characters(video.title)[:50])
file_path = os.path.join(output_path, file_name)
# Check if the file already exists, if not, download it
if not os.path.isfile(file_path):
video.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').asc().first().download(filename=file_name, output_path=output_path)
print(f"Downloaded {file_name}")
else:
print(f"Video {file_name} already exists, skipping download.")
else:
yt = YouTube(video_url)
video_title = remove_invalid_characters(yt.title)[:50] # Truncate title for file name
file_name = "{}.mp4".format(video_title)
output_path = 'videos/singles'
# Ensure the 'videos/singles' directory exists
if not os.path.exists(output_path):
os.makedirs(output_path)
# Check if the file already exists, if not, download it
if not os.path.isfile(os.path.join(output_path, file_name)):
yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').asc().first().download(filename=file_name, output_path=output_path)
print(f"Downloaded {file_name}")
else:
print(f"Video {file_name} already exists, skipping download.")
except VideoUnavailable as e:
print(f"Video {video_url} is unavailable: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == '__main__':
# Read video URLs from a file
with open('video_ids.txt', 'r', encoding='utf-8') as file:
urls_list = [line.strip() for line in file]
# Use multiprocessing to download videos concurrently
freeze_support() # needed for Windows
with Pool(4) as p:
p.map(download_video, urls_list)
video_ids.txt kiyala file ekak hadala ekata mehema dagattahama hari.
Code:
video_ids.txt
https://www.youtube.com/watch?v=gcEIKp0glw4
https://www.youtube.com/watch?v=Gx7OCySdFHg
https://www.youtube.com/watch?v=7dOzEUt47_w
https://www.youtube.com/watch?v=bmC64eZOnOw
https://www.youtube.com/watch?v=xsOCUouw_94
https://www.youtube.com/playlist?list=PLF26CCB1679908248
Code:
pip3 install pytube
then run python3 yourscriptname.py
https://pytube.io/en/latest/user/install.html