Spaces:
Running

File size: 1,798 Bytes
b83d389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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.

"""Discover video file paths from a directory or a line-based list file."""

from __future__ import annotations

from pathlib import Path

VIDEO_EXTENSIONS = (".mp4", ".mkv", ".mov", ".webm")


def list_video_files(videos_dir: Path) -> list[Path]:
    """Sorted list of video files directly under ``videos_dir`` (non-recursive)."""
    if not videos_dir.is_dir():
        return []
    out: list[Path] = []
    for p in sorted(videos_dir.iterdir()):
        if p.is_file() and p.suffix.lower() in VIDEO_EXTENSIONS:
            out.append(p)
    return out


def load_video_paths_from_file(list_file: Path) -> list[Path]:
    """One absolute or relative path per line; ``#`` starts a comment; skips missing files."""
    if not list_file.is_file():
        return []
    base = list_file.parent
    out: list[Path] = []
    for line in list_file.read_text(encoding="utf-8").splitlines():
        s = line.strip()
        if not s or s.startswith("#"):
            continue
        p = Path(s)
        if not p.is_absolute():
            p = (base / p).resolve()
        if p.is_file():
            out.append(p)
    return out


def load_video_paths(
    *,
    videos_dir: Path | None = None,
    list_file: Path | None = None,
) -> list[Path]:
    """
    Resolve a list of video paths: prefer ``list_file`` if given and present,
    else scan ``videos_dir``.
    """
    if list_file is not None and list_file.is_file():
        return load_video_paths_from_file(list_file)
    if videos_dir is not None and videos_dir.is_dir():
        return list_video_files(videos_dir)
    return []