Spaces:
Running

File size: 7,355 Bytes
db01b23
 
 
 
 
 
 
 
 
 
9ed20e4
 
db01b23
9ed20e4
db01b23
 
 
da7d3a3
db01b23
 
9ed20e4
db01b23
 
9ed20e4
db01b23
 
9ed20e4
 
 
 
 
 
db01b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da7d3a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db01b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ed20e4
 
db01b23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9ed20e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db01b23
 
 
 
 
 
9ed20e4
 
 
 
da7d3a3
 
 
 
 
 
 
 
9ed20e4
 
 
 
 
 
 
 
da7d3a3
db01b23
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

"""HTTP routes to upload videos into ``VIDEO_ENCODE_VIDEOS_DIR`` (e.g. Hugging Face Space)."""

from __future__ import annotations

import asyncio
import logging
import os
from functools import partial
from pathlib import Path

from fastapi import APIRouter, File, HTTPException, UploadFile
from pydantic import BaseModel, Field

try:
    from ..video_analysis import analyze_video, forget_whole_video_analysis, store_whole_video_analysis
    from ..video_paths import VIDEO_EXTENSIONS, list_video_files
except ImportError:
    from video_analysis import analyze_video, forget_whole_video_analysis, store_whole_video_analysis
    from video_paths import VIDEO_EXTENSIONS, list_video_files

logger = logging.getLogger(__name__)

# ``analyze_video`` (ffmpeg luma/complexity) can be slow; outer wait allows ffprobe + I/O headroom.
_DEFAULT_UPLOAD_ANALYSIS_TIMEOUT_SEC = 600.0
_UPLOAD_WAIT_HEADROOM_SEC = 180.0


def videos_dir() -> Path:
    return Path(os.environ.get("VIDEO_ENCODE_VIDEOS_DIR", "videos")).expanduser().resolve()


def default_upload_max_bytes() -> int:
    raw = os.environ.get("VIDEO_ENCODE_UPLOAD_MAX_BYTES", str(2 * 1024 * 1024 * 1024))
    try:
        return max(1, int(raw))
    except ValueError:
        return 2 * 1024 * 1024 * 1024


def _safe_filename(name: str | None) -> str:
    if not name:
        raise HTTPException(status_code=400, detail="Filename required")
    base = Path(name).name
    if not base or base in (".", "..") or "/" in base or "\\" in base:
        raise HTTPException(status_code=400, detail="Invalid filename")
    suf = Path(base).suffix.lower()
    if suf not in VIDEO_EXTENSIONS:
        raise HTTPException(
            status_code=400,
            detail=f"Unsupported extension {suf!r}; allowed: {', '.join(VIDEO_EXTENSIONS)}",
        )
    return base


class DeleteVideoBody(BaseModel):
    """JSON body for ``POST /api/videos/delete`` (same semantics as ``DELETE /api/videos/{filename}``)."""

    filename: str = Field(..., description="Basename only, e.g. clip.mp4")


def _delete_uploaded_video_impl(filename: str) -> dict:
    """Remove one file under ``VIDEO_ENCODE_VIDEOS_DIR`` and drop its analysis cache."""
    root = videos_dir()
    name = _safe_filename(filename)
    root_res = root.resolve()
    dest = (root / name).resolve()
    try:
        dest.relative_to(root_res)
    except ValueError:
        raise HTTPException(status_code=400, detail="Path escapes videos directory") from None
    if not dest.is_file():
        raise HTTPException(status_code=404, detail="File not found")
    forget_whole_video_analysis(dest)
    dest.unlink()
    logger.info("Deleted uploaded video %s", name)
    return {
        "ok": True,
        "deleted": name,
        "hint": "Call POST /reset on the environment (or reconnect) to refresh the video list.",
    }


def build_video_upload_router() -> APIRouter:
    router = APIRouter(tags=["videos"])

    @router.get("/api/videos")
    async def list_uploaded_videos() -> dict:
        """List video files in ``VIDEO_ENCODE_VIDEOS_DIR`` (non-recursive)."""
        root = videos_dir()
        if not root.is_dir():
            return {"videos_dir": str(root), "exists": False, "files": []}
        files = [p.name for p in list_video_files(root)]
        return {"videos_dir": str(root), "exists": True, "count": len(files), "files": files}

    @router.post("/api/videos/upload")
    async def upload_video(file: UploadFile = File(...)) -> dict:
        """
        Save an uploaded file into ``VIDEO_ENCODE_VIDEOS_DIR``.

        Call ``reset()`` on the environment afterward (or start a new session) so the
        new file is included in the episode video list.
        """
        name = _safe_filename(file.filename)
        root = videos_dir()
        root.mkdir(parents=True, exist_ok=True)

        dest = root / name
        if dest.is_file():
            forget_whole_video_analysis(dest)
        max_bytes = default_upload_max_bytes()
        written = 0

        try:
            with dest.open("wb") as out:
                while True:
                    chunk = await file.read(1024 * 1024)
                    if not chunk:
                        break
                    written += len(chunk)
                    if written > max_bytes:
                        dest.unlink(missing_ok=True)
                        raise HTTPException(
                            status_code=413,
                            detail=f"File exceeds VIDEO_ENCODE_UPLOAD_MAX_BYTES ({max_bytes})",
                        )
                    out.write(chunk)
        except HTTPException:
            raise
        except Exception as e:
            dest.unlink(missing_ok=True)
            raise HTTPException(status_code=500, detail=str(e)) from e

        raw_to = os.environ.get(
            "VIDEO_ENCODE_UPLOAD_ANALYSIS_TIMEOUT_SEC",
            str(_DEFAULT_UPLOAD_ANALYSIS_TIMEOUT_SEC),
        )
        try:
            analysis_timeout = float(raw_to)
        except ValueError:
            analysis_timeout = _DEFAULT_UPLOAD_ANALYSIS_TIMEOUT_SEC

        analysis_error: str | None = None
        try:
            analysis = await asyncio.wait_for(
                asyncio.to_thread(
                    partial(analyze_video, dest, complexity_timeout_sec=analysis_timeout),
                ),
                timeout=analysis_timeout + _UPLOAD_WAIT_HEADROOM_SEC,
            )
            store_whole_video_analysis(dest, analysis)
            logger.info("Stored in-memory video analysis for %s", name)
        except asyncio.TimeoutError:
            analysis_error = (
                f"analyze_video exceeded {analysis_timeout + _UPLOAD_WAIT_HEADROOM_SEC}s"
            )
            logger.warning("Upload analysis timeout for %s: %s", name, analysis_error)
        except Exception as e:
            analysis_error = str(e)
            logger.warning("Upload analysis failed for %s: %s", name, e, exc_info=True)

        out: dict[str, object] = {
            "ok": True,
            "path": str(dest.resolve()),
            "filename": name,
            "size_bytes": written,
            "hint": "Call POST /reset on the environment (or reconnect) to refresh the video list.",
        }
        if analysis_error is not None:
            out["analysis_error"] = analysis_error
        return out

    @router.post("/api/videos/delete")
    async def delete_uploaded_video_post(body: DeleteVideoBody) -> dict:
        """
        Same as ``DELETE /api/videos/{filename}``, but uses JSON so it works behind
        proxies that only allow GET/POST and for older images that lack the DELETE route.
        """
        return _delete_uploaded_video_impl(body.filename)

    @router.delete("/api/videos/{filename}")
    async def delete_uploaded_video(filename: str) -> dict:
        """
        Remove a video file under ``VIDEO_ENCODE_VIDEOS_DIR`` and its in-memory analysis cache.

        Filename must be a single basename (same rules as upload). Call ``reset`` afterward
        so the environment rescans the directory.
        """
        return _delete_uploaded_video_impl(filename)

    return router