Skip to content
Snippets Groups Projects
downloader.py 566 B
Newer Older
  • Learn to ignore specific revisions
  • import youtube_dl
    from django.conf import settings
    
    
    def download(
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        url, target_directory=settings.MEDIA_ROOT, name="%(id)s.%(ext)s", bitrate=192
    ):
    
        target_path = os.path.join(target_directory, name)
        ydl_opts = {
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            "quiet": True,
            "outtmpl": target_path,
            "postprocessors": [{"key": "FFmpegExtractAudio", "preferredcodec": "vorbis"}],
    
        }
        _downloader = youtube_dl.YoutubeDL(ydl_opts)
        info = _downloader.extract_info(url)
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        info["audio_file_path"] = target_path % {"id": info["id"], "ext": "ogg"}