File size: 1,296 Bytes
bd65e34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import subprocess
from pathlib import Path


def download_twitch_stream(TWITCH_ID: str, end_time: str | None = None):
    out_path = Path(f"downloaded/{TWITCH_ID}.mp4")
    out_path.parent.mkdir(exist_ok=True, parents=True)
    if out_path.exists():
        print(f"Already downloaded {TWITCH_ID}")
        return

    end_time = ["-e", end_time] if end_time is not None else []
    subprocess.Popen(
        [
            "twitch-dl",
            "download",
            TWITCH_ID,
            "-q",
            "720p60",
            *end_time,
            "--output",
            str(out_path),
        ],
    ).communicate()
    return True


def vid_to_frames(TWITCH_ID: str, use_cuda: bool = True, frames: int = 3):
    in_path = Path(f"downloaded/{TWITCH_ID}.mp4")
    out_path = Path(f"converted/{TWITCH_ID}")
    if out_path.exists():
        print(f"Already converted {TWITCH_ID} to frames")
        return
    out_path.mkdir(parents=True, exist_ok=True)

    use_cuda = ["-hwaccel", "cuda"] if use_cuda else []
    subprocess.Popen(
        [
            "ffmpeg",
            *use_cuda,
            "-i",
            str(in_path),
            "-vf",
            f"fps={frames}",
            "-q:v",
            "25",
            f"{out_path}/img%d.jpg",
        ],
    ).communicate()