[Answered ]-Django PIPE youtube-dl to view for download

1πŸ‘

βœ…

I did a workaround.
I download the file with a specific name, I return the view with HttpResponse, with force-download content-type, and then delete the file using python.
It’s not what I originally had in mind, but it’s the second best solution that I could come up with. I will select this answer as accepted solution until a Python wizard gives a solution to the original question.
The code that I have right now:

def download_clip(request, pk):
    ydl_opts = {
        'outtmpl': f"{pk}.mp4"
    }
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download([f"https://clips.twitch.tv/{pk}"])

    path = f"{pk}.mp4"
    file_path = os.path.join(path)
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="application/force-download")
            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
            os.remove(file_path)
            return response
    raise Http404

Leave a comment