File size: 1,419 Bytes
5338741
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import subprocess
import ffmpeg

def convert_video_to_gif(video_path, fps=10, loop="0 (loop forever)"):
    """
    Convert video to GIF with specified FPS and loop settings.
    
    Args:
        video_path: Path to the video file
        fps: Frames per second for the output GIF
        loop: Loop setting as string (format: "N (description)")
    
    Returns:
        Path to the output GIF
    """
    # Extract just the loop number from the selection
    loop_value = loop.split(" ")[0]
    
    # Create output filename
    filename = os.path.basename(video_path)
    name, _ = os.path.splitext(filename)
    output_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "output")
    os.makedirs(output_dir, exist_ok=True)
    output_path = os.path.join(output_dir, f"{name}_fps{fps}_loop{loop_value}.gif")
    
    # Use ffmpeg to convert the video to GIF
    try:
        (
            ffmpeg
            .input(video_path)
            .filter('fps', fps=int(fps))
            .output(output_path, loop=int(loop_value))
            .overwrite_output()
            .run(capture_stdout=True, capture_stderr=True)
        )
        return output_path
    except subprocess.CalledProcessError as e:
        print(f"FFmpeg error: {e.stderr.decode() if hasattr(e, 'stderr') else str(e)}")
        return None
    except Exception as e:
        print(f"Error: {str(e)}")
        return None