Repository: youtube-dl
Issue: Downloading youtube videos into a NamedTemporaryFile
Labels: question

<!--

######################################################################
  WARNING!
  IGNORING THE FOLLOWING TEMPLATE WILL RESULT IN ISSUE CLOSED AS INCOMPLETE
######################################################################

-->


## Checklist

<!--
Carefully read and work through this check list in order to prevent the most common mistakes and misuse of youtube-dl:
- Look through the README (http://yt-dl.org/readme) and FAQ (http://yt-dl.org/faq) for similar questions
- Search the bugtracker for similar questions: http://yt-dl.org/search-issues
- Finally, put x into all relevant boxes (like this [x])
-->

- [x] I'm asking a question
- [x] I've looked through the README and FAQ for similar questions
- [x] I've searched the bugtracker for similar questions including closed ones


## Question

<!--
Ask your question in an arbitrary form. Please make sure it's worded well enough to be understood, see https://github.com/ytdl-org/youtube-dl#is-the-description-of-the-issue-itself-sufficient.
-->

I'm using `youtube_dl` from `pip` version `2020.11.12` in `python3.6`.

My goal is to download videos into `NamedTemporaryFile` or `TemporaryFile` to avoid leaking files. Some other libraries support this by accepting a file handler parameter e.g. `download(urls, file_handle)`. I didn't find this method in `YouTubeDL` so I decided to create a `NamedTemporaryFile` (named `test.mp4`) and have `youtube_dl` overwrite that file by setting `outtmpl: "test.mp4"`. Unfortunately, for some reason `youtube_dl` doesn't overwrite files at all and I'm not sure how to make it overwrite if there's a file with the same name. See below what I get when attempting to overwrite a file with the same name.

I had another attempt where I wanted to stream the download into `stdout` and redirect that `stdout` into a file handler of my own (i.e. `TemporaryFile`). I know this can be done in the terminal, but I'm not sure how to this in the python script. If I set `outtmpl: "-"`, I simply get an `-.mp4` file.

I'm open to suggestions, in the meanwhile I'll probably write some workaround where the normal file is downloaded, copied into a temporary file and the original is deleted.

The snippet below is a minimal script that demonstrates `youtube_dl` not overwriting existing files:
```python3
import youtube_dl

ydl_opts = {
    "format": "bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best", # I really need .mp4
    "merge-output-format": "mp4",
    "recode-video": "mp4",
    "outtmpl": "test",
    "quiet": False,
    "cache_dir": False, # Tested with and without this dict entry
    "nooverwrites": False, # Tested with and without this dict entry
    "no-post-overwrites": False, # Tested with and without this dict entry
    "skip_download": False, # Tested with and without this dict entry
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(["https://www.youtube.com/watch?v=BaW_jenozKc&ab_channel=PhilippHagemeister"])
```

I simply run the script in the terminal:
```
$ python random_script.py
[youtube] BaW_jenozKc: Downloading webpage
[download] Destination: test.f137
[download] 100% of 2.11MiB in 00:00
[download] Destination: test.f140
[download] 100% of 154.06KiB in 00:00
[ffmpeg] Merging formats into "test.mp4"
Deleting original file test.f137 (pass -k to keep)
Deleting original file test.f140 (pass -k to keep)

$ python random_script.py
[youtube] BaW_jenozKc: Downloading webpage
[download] test.mp4 has already been downloaded and merged
```