File size: 3,189 Bytes
08d80be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from io import IOBase
import os
import subprocess
from typing import Optional
from pathlib import Path
from rich.console import Console

console = Console()

class FailedProcess(Exception):
    pass

def ffmpeg_extract_frames(
        video_path: Path,
        frames_path: Path,
        # TODO: Enable these options
        # start_time: Optional[str] = None,
        # duration: Optional[float] = None,
        # end_time: Optional[str]  = None,
        fps: float = 1,
        qscale: int = 1,
        stream_file: Optional[IOBase] = None
        ) -> str:
    frame_destination = frames_path / "input"
    console.log(f"🎞️  Extracting Images from {video_path} to {frame_destination} (fps: {fps}, qscale: {qscale}")
    # Create the directory to store the frames
    frames_path.mkdir(parents=True, exist_ok=True)
    frame_destination.mkdir(parents=True, exist_ok=True)
    # Store the current working directory
    cwd = os.getcwd()
    # Change the current working directory to frame_destination
    os.chdir(frame_destination)
    
    # Construct the ffmpeg command as a list of strings
    cmd = [
        'ffmpeg', 
        '-i', str(video_path), 
        '-qscale:v', str(qscale),
        '-qmin', '1',
        '-vf', f"fps={fps}",
        '%04d.jpg'
    ]

    console.log(f"πŸ’» Executing command: {' '.join(cmd)}")
    
    _stdout = stream_file if stream_file else subprocess.PIPE
    with subprocess.Popen(cmd, stdout=_stdout, stderr=subprocess.STDOUT, text=True) as process:
        if process.stdout:
            for line in process.stdout:
                print(line)
                
    # Change the current working directory back to the original
    os.chdir(cwd)

    return_code = process.returncode
    
    if return_code == 0:
        console.log(f"βœ… Images Successfully Extracted! Path: {frames_path}")
    else:
        raise FailedProcess("Error extracting frames.")

    return frames_path

def ffmpeg_run(
        video_path: Path,
        output_path: Path,
        ffmpeg_command: str = "ffmpeg",
        # TODO: Enable these options
        # start_time: Optional[str] = None,
        # duration: Optional[float] = None,
        # end_time: Optional[str]  = None,
        fps: float = 1,
        qscale: int = 1,
        stream_file: Optional[IOBase] = None
        ) -> str:
    console.log("🌟 Starting the Frames Extraction...")
    frames_path = ffmpeg_extract_frames(
        video_path, 
        output_path,
        fps=fps, qscale=qscale, 
        stream_file=stream_file
    )
    console.log(f"πŸŽ‰ Frames Extraction Complete! Path: {frames_path}")
    return frames_path

if __name__ == "__main__":
    import tempfile
    with tempfile.NamedTemporaryFile(mode='w+t') as temp_file:
        print(f"Using temp file: {temp_file.name}")
        try:
            ffmpeg_run(
                Path("/home/europe/Desktop/gaussian-splatting-kit/test/test.mov"),
                Path("/home/europe/Desktop/gaussian-splatting-kit/test"),
                stream_file=temp_file
            )
        except FailedProcess:
            console.log("🚨 Error extracting frames.")
            temp_file.seek(0)
            print(temp_file.read())