File size: 991 Bytes
b57b61b
 
 
 
 
 
 
 
7c40c83
b57b61b
 
7c40c83
b57b61b
 
 
 
 
7c40c83
b57b61b
 
 
 
 
 
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
import json
import ffmpeg
from transformers import Tool

class VideoFrameSampleTool(Tool):   
    name = "video_frame_sample_tool"
    description = """                                     
    This tool samples an image frame from an input video. 
    Inputs are input_path, output_path, and frame_number.
    Output is the output_path. 
    """                                 
    inputs = ["text", "text", "text"]   
    outputs = ["text"]
                                                                             
    def __call__(self, input_path: str, output_path: str, frame_number: int):
        out, _ = (                  
            ffmpeg.input(input_path)                           
            .filter("select", "gte(n,{})".format(str(frame_number)))    
            .output("pipe:", vframes=1, format="image2", vcodec="mjpeg")
            .run(capture_stdout=True)
        )
        img = Image.open(BytesIO(out))
        img.save(output_path)
        return output_path