init
This commit is contained in:
33
python/ytdl/download_audio.py
Normal file
33
python/ytdl/download_audio.py
Normal file
@ -0,0 +1,33 @@
|
||||
import yt_dlp
|
||||
import os
|
||||
|
||||
def download_and_convert_to_opus(url, output_filename):
|
||||
# Use yt-dlp to download the video with audio only
|
||||
ydl_opts = {
|
||||
'format': 'bestaudio/best',
|
||||
'outtmpl': '%(extractor)s-%(id)s.%(ext)s', # Save as a temporary file
|
||||
'restrictfilenames': True,
|
||||
}
|
||||
|
||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||
info_dict = ydl.extract_info(url, download=True)
|
||||
temp_file = ydl.prepare_filename(info_dict)
|
||||
|
||||
# Convert to OPUS using FFmpeg
|
||||
ffmpeg_command = [
|
||||
'ffmpeg',
|
||||
'-i', temp_file,
|
||||
'-c:a', 'libopus',
|
||||
'-b:a', '128k',
|
||||
output_filename
|
||||
]
|
||||
|
||||
os.system(' '.join(ffmpeg_command))
|
||||
|
||||
# Clean up the temporary file
|
||||
os.remove(temp_file)
|
||||
|
||||
# Example usage:
|
||||
url = input("input video url: ")
|
||||
filename = input("filname for ripped audio: ")
|
||||
download_and_convert_to_opus(url, filename + ".opus")
|
||||
27
python/ytdl/download_playlist.py
Normal file
27
python/ytdl/download_playlist.py
Normal file
@ -0,0 +1,27 @@
|
||||
from yt_dlp import YoutubeDL
|
||||
import os
|
||||
|
||||
def download_playlist(playlist_url, output_path):
|
||||
ydl_opts = {
|
||||
'format': 'bestaudio/best',
|
||||
'outtmpl': os.path.join(output_path, '%(title)s.%(ext)s'),
|
||||
'postprocessors': [{
|
||||
'key': 'FFmpegExtractAudio',
|
||||
'preferredcodec': 'opus',
|
||||
'preferredquality': '128',
|
||||
}],
|
||||
'quiet': True,
|
||||
}
|
||||
|
||||
with YoutubeDL(ydl_opts) as ydl:
|
||||
ydl.download([playlist_url])
|
||||
|
||||
if __name__ == "__main__":
|
||||
playlist_url = input("Enter the YouTube Playlist URL: ")
|
||||
output_path = input("Enter the directory to save audio files: ")
|
||||
|
||||
if not os.path.exists(output_path):
|
||||
os.makedirs(output_path)
|
||||
|
||||
download_playlist(playlist_url, output_path)
|
||||
print("Download complete!")
|
||||
19
python/ytdl/download_video.py
Normal file
19
python/ytdl/download_video.py
Normal file
@ -0,0 +1,19 @@
|
||||
import yt_dlp
|
||||
|
||||
def download_video(url, path='.'):
|
||||
# Options for downloading the best quality video and audio
|
||||
ydl_opts = {
|
||||
'format': 'bestvideo+bestaudio/best', # Selects the best quality of video and audio available
|
||||
'outtmpl': f'{path}/%(title)s.%(ext)s', # Template for output file name
|
||||
}
|
||||
|
||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||
try:
|
||||
ydl.download([url])
|
||||
print("Video downloaded successfully.")
|
||||
except Exception as e:
|
||||
print(f"An error occurred while downloading the video: {e}")
|
||||
|
||||
# Example usage
|
||||
video_url = input("Video URL: ")
|
||||
download_video(video_url)
|
||||
35
python/ytdl/live.py
Normal file
35
python/ytdl/live.py
Normal file
@ -0,0 +1,35 @@
|
||||
import yt_dlp
|
||||
import subprocess
|
||||
|
||||
def stream_video(url):
|
||||
ydl_opts = {
|
||||
'format': 'bestvideo+bestaudio/best',
|
||||
'quiet': True,
|
||||
'no_warnings': True,
|
||||
'noplaylist': True,
|
||||
'forceurl': True,
|
||||
'skip_download': True,
|
||||
}
|
||||
|
||||
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
||||
try:
|
||||
info = ydl.extract_info(url, download=False)
|
||||
if 'url' in info:
|
||||
video_url = info['url']
|
||||
else:
|
||||
# For some videos (like YouTube), it's nested in 'formats'
|
||||
formats = info.get('formats', [])
|
||||
if formats:
|
||||
video_url = formats[-1]['url']
|
||||
else:
|
||||
print("Could not extract video URL.")
|
||||
return
|
||||
|
||||
print("Streaming to mpv...")
|
||||
subprocess.run(['mpv', video_url])
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
|
||||
# Example usage
|
||||
video_url = input("Video URL: ")
|
||||
stream_video(video_url)
|
||||
Reference in New Issue
Block a user