• PYTHON > télécharger des vidéos youtube avec le module yt_dlp

      INSTALLATION

      pip install -U yt-dlp

      FORMAT TYPE

      import yt_dlp
      
      def download_video(url):
          options = {
              'format': 'bestvideo+bestaudio/best',
              'outtmpl': 'downloaded_video.%(ext)s',
          }
          with yt_dlp.YoutubeDL(options) as ydl:
              ydl.download([url])
      
      if __name__ == "__main__":
          video_url = "https://www.youtube.com/watch?v=xxxxxxxxxxx"
          download_video(video_url)

      OPTIONS

      format: format vidéo et audio. You can use formats like 'bestvideo+bestaudio/best' to download the best quality available.

      outtmpl: Template du fichier. On peut utiliser des placeholders (%(title)s, %(id)s, %(ext)s, etc.). Ex: ma_video.%(ext)s donnera ma_video.mp4.

      postprocessors: Liste de dictionaries specifying post-processing options. Exemple, utiliser FFmpeg pour extraire l’audio d’un fichier avec preferedformat set to ‘mp4′.

      progress_hooks: A list of functions to be called during the download to report the download progress. In the example, the my_hook function is used to print information about the download status, such as the percentage downloaded and total bytes.

      post_download: A function to be called after the download is complete. Ex: the my_post_download function is called, which simply prints a message indicating that post-download processing is being performed for the downloaded video.

       

       

      import yt_dlp
      
      def download_video(url):
          options = {
              'format': 'bestvideo+bestaudio/best',
              'outtmpl': 'downloaded_video.%(ext)s',
              'postprocessors': [{
                  'key': 'FFmpegVideoConvertor',
                  'preferedformat': 'mp4',
              }],
              'progress_hooks': [my_hook],
              'post_download': my_post_download,
          }
      
          with yt_dlp.YoutubeDL(options) as ydl:
              ydl.download([url])
      
      def my_hook(d):
          if d['status'] == 'finished':
              print('Download complete!')
          elif d['status'] == 'downloading':
              print(f'Downloaded {d["_percent_str"]} of {d["_total_bytes_str"]}')
      
      def my_post_download(info):
          print(f'Post download processing for video: {info["title"]}')
      
      if __name__ == "__main__":
          video_url = "https://www.youtube.com/watch?v=your_video_id_here"
          download_video(video_url)

      Documentation

      https://github.com/yt-dlp/yt-dlp#options

       

 

Aucun commentaire

 

Laissez un commentaire