Spaces:
Running
Running
| 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 |