Spaces:
Paused
Paused
Upload 3 files
Browse files- Dockerfile +16 -0
- app.py +41 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
+
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
|
| 4 |
+
FROM python:3.9
|
| 5 |
+
|
| 6 |
+
RUN useradd -m -u 1000 user
|
| 7 |
+
USER user
|
| 8 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 9 |
+
|
| 10 |
+
WORKDIR /app
|
| 11 |
+
|
| 12 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 13 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 14 |
+
|
| 15 |
+
COPY --chown=user . /app
|
| 16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import StreamingResponse
|
| 3 |
+
import yt_dlp
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Temporary download directory
|
| 9 |
+
DOWNLOAD_DIR = "downloads/"
|
| 10 |
+
|
| 11 |
+
# Ensure the directory exists
|
| 12 |
+
if not os.path.exists(DOWNLOAD_DIR):
|
| 13 |
+
os.makedirs(DOWNLOAD_DIR)
|
| 14 |
+
|
| 15 |
+
@app.get("/")
|
| 16 |
+
def greet_json():
|
| 17 |
+
return {"Hello": "World!"}
|
| 18 |
+
|
| 19 |
+
@app.get("/download_video")
|
| 20 |
+
def download_video(url: str):
|
| 21 |
+
try:
|
| 22 |
+
# Set up yt-dlp options to avoid downloading .webm files
|
| 23 |
+
ydl_opts = {
|
| 24 |
+
'format': 'bestvideo+bestaudio/best', # Download the best video and audio
|
| 25 |
+
'outtmpl': DOWNLOAD_DIR + '%(id)s.%(ext)s', # Output path template
|
| 26 |
+
'noplaylist': True, # Avoid downloading playlists, if it's a playlist URL
|
| 27 |
+
'quiet': True, # Suppress unnecessary output
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
# Download video using yt-dlp
|
| 31 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 32 |
+
info_dict = ydl.extract_info(url, download=True)
|
| 33 |
+
video_filename = ydl.prepare_filename(info_dict)
|
| 34 |
+
|
| 35 |
+
# Return the video file as a response
|
| 36 |
+
video_file = open(video_filename, "rb")
|
| 37 |
+
return StreamingResponse(video_file, media_type="video/mp4")
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 41 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
yt-dlp
|