Advertisement
hoobird

Untitled

Oct 22nd, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. from pytubefix import YouTube
  2. from moviepy.editor import *
  3. import sys
  4. import os
  5.  
  6. def get_best_1080p_stream(streams):
  7. # Filter streams for 1080p or lower
  8. filtered_streams = streams.filter(adaptive=True,
  9. file_extension='mp4',
  10. only_video=True,
  11. resolution="1080p")
  12.  
  13. # If no 1080p stream is found, get the next best quality below 1080p
  14. if not filtered_streams:
  15. filtered_streams = streams.filter(adaptive=True,
  16. file_extension='mp4',
  17. only_video=True)\
  18. .order_by('resolution')\
  19. .desc()
  20.  
  21. # Filter out streams above 1080p
  22. filtered_streams = [s for s in filtered_streams if not s.resolution or
  23. int(s.resolution[:-1]) <= 1080]
  24.  
  25. return filtered_streams[0] if filtered_streams else None
  26.  
  27. def download_video(url):
  28. try:
  29. yt = YouTube(url)
  30. print(f"Title: {yt.title}")
  31. print("Starting download...")
  32.  
  33. # Get the best video stream at 1080p or lower
  34. video_stream = get_best_1080p_stream(yt.streams)
  35. if not video_stream:
  36. print("Error: No suitable video stream found")
  37. sys.exit(1)
  38.  
  39. # Get the highest quality audio stream
  40. audio_stream = yt.streams.filter(only_audio=True)\
  41. .order_by('abr')\
  42. .desc()\
  43. .first()
  44.  
  45. if video_stream and audio_stream:
  46. print(f"Selected video quality: {video_stream.resolution}")
  47. print(f"Selected audio quality: {audio_stream.abr}")
  48.  
  49. # Create temporary filenames
  50. temp_video_path = 'temp_video2.mp4'
  51. temp_audio_path = 'temp_audio2.mp4'
  52.  
  53. # Download both streams
  54. print(f"Downloading video...")
  55. video_stream.download(filename=temp_video_path)
  56. print(f"Downloading audio...")
  57. audio_stream.download(filename=temp_audio_path)
  58.  
  59. # Prepare the final filename
  60. final_path = f"{yt.title}.mp4"
  61. # Remove invalid characters from filename
  62. final_path = "".join(c for c in final_path if c.isalnum() or c in (' ', '-', '_', '.'))
  63.  
  64. print("Merging video and audio...")
  65. # Combine video and audio using moviepy
  66. video_clip = VideoFileClip(temp_video_path)
  67. audio_clip = AudioFileClip(temp_audio_path)
  68.  
  69. final_clip = video_clip.set_audio(audio_clip)
  70. final_clip.write_videofile(final_path, codec='libx264', audio_codec='aac')
  71.  
  72. # Clean up
  73. video_clip.close()
  74. audio_clip.close()
  75. os.remove(temp_video_path)
  76. os.remove(temp_audio_path)
  77.  
  78. print(f"Download completed: {final_path}")
  79.  
  80. except Exception as e:
  81. print(f"Error: Failed to download video: {str(e)}")
  82. sys.exit(1)
  83.  
  84. if __name__ == "__main__":
  85. if len(sys.argv) != 2:
  86. print("Usage: python script.py <youtube_url>")
  87. sys.exit(1)
  88.  
  89. url = sys.argv[1]
  90. download_video(url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement