diff options
Diffstat (limited to 'python/ytdl')
| -rw-r--r-- | python/ytdl/download_audio.py | 33 | ||||
| -rw-r--r-- | python/ytdl/download_playlist.py | 27 | ||||
| -rw-r--r-- | python/ytdl/download_video.py | 19 | ||||
| -rw-r--r-- | python/ytdl/live.py | 35 |
4 files changed, 114 insertions, 0 deletions
diff --git a/python/ytdl/download_audio.py b/python/ytdl/download_audio.py new file mode 100644 index 0000000..5f6bd74 --- /dev/null +++ b/python/ytdl/download_audio.py @@ -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("filename for ripped audio: ") +download_and_convert_to_opus(url, filename + ".opus") diff --git a/python/ytdl/download_playlist.py b/python/ytdl/download_playlist.py new file mode 100644 index 0000000..e2a371e --- /dev/null +++ b/python/ytdl/download_playlist.py @@ -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!") diff --git a/python/ytdl/download_video.py b/python/ytdl/download_video.py new file mode 100644 index 0000000..dbd8bff --- /dev/null +++ b/python/ytdl/download_video.py @@ -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) diff --git a/python/ytdl/live.py b/python/ytdl/live.py new file mode 100644 index 0000000..eaa78cf --- /dev/null +++ b/python/ytdl/live.py @@ -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) |
