I'm using python embedded youtube_dl
and I'd like to download video content directly into a temporary file. I attempted to create a NamedTemporaryFile
and have youtube_dl
write into it, but I always get a prompt that the file was already downloaded (the temporary file has that name and it thinks the download already happened).
I also attempted to have youtube_dl
stream downloaded data to stdout
and redirect stdout
to the temporary file but I can't get the python embedded version to do that. It doesn't output to stdout
it simply creates a file named -.mp4
.
import youtube_dl
ydl_opts = {
"format": "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best",
"merge-output-format": "mp4",
"recode-video": "mp4",
"outtmpl": "-",
"verbose": True,
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(["https://www.youtube.com/watch?v=h3h035Eyz5A&ab_channel=Loku"])
To summarize, the code above won't override -.mp4
files if they already exist and it won't stream the download into stdout
so I can redirect it into a temp file.
The temp file is needed because it's an intermediary step for further processing. At this point it feels like I'll need to copy the file to temp file and delete the original or I'll need to use youtube_dl
with a subprocess which feels silly.
youtube_dl
devs to take this issue into consideration (posted an issue some time ago) but they didn't respond yet. Anyway I'll accept your answer, welcome to stackoverflow. – Garbage