Upload 8 files
Browse files- requirements.txt +1 -3
- video_encoder.py +3 -44
requirements.txt
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
-
gradio==4.
|
| 2 |
-
gradio_client==0.16.2
|
| 3 |
-
huggingface_hub==0.23.0
|
| 4 |
pillow>=10.0.0
|
| 5 |
numpy
|
| 6 |
cloudinary
|
|
|
|
| 1 |
+
gradio==4.19.0
|
|
|
|
|
|
|
| 2 |
pillow>=10.0.0
|
| 3 |
numpy
|
| 4 |
cloudinary
|
video_encoder.py
CHANGED
|
@@ -63,54 +63,13 @@ def encode_frames_to_webm(frames: List[Image.Image], output_path: str, fps: int
|
|
| 63 |
|
| 64 |
def encode_frames_pipe(frames: List[Image.Image], output_path: str, fps: int = 24) -> str:
|
| 65 |
"""
|
| 66 |
-
Encode frames to WebM using
|
| 67 |
-
Pipes raw RGB data directly to FFmpeg.
|
| 68 |
"""
|
| 69 |
if not frames:
|
| 70 |
raise ValueError("No frames provided")
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
cmd = [
|
| 75 |
-
"ffmpeg", "-y",
|
| 76 |
-
"-f", "rawvideo",
|
| 77 |
-
"-vcodec", "rawvideo",
|
| 78 |
-
"-s", f"{width}x{height}",
|
| 79 |
-
"-pix_fmt", "rgb24",
|
| 80 |
-
"-r", str(fps),
|
| 81 |
-
"-i", "-", # Read from pipe
|
| 82 |
-
"-c:v", "libvpx",
|
| 83 |
-
"-b:v", "2M",
|
| 84 |
-
"-pix_fmt", "yuv420p",
|
| 85 |
-
"-deadline", "realtime",
|
| 86 |
-
"-cpu-used", "8",
|
| 87 |
-
output_path
|
| 88 |
-
]
|
| 89 |
-
|
| 90 |
-
process = subprocess.Popen(
|
| 91 |
-
cmd,
|
| 92 |
-
stdin=subprocess.PIPE,
|
| 93 |
-
stdout=subprocess.PIPE,
|
| 94 |
-
stderr=subprocess.PIPE
|
| 95 |
-
)
|
| 96 |
-
|
| 97 |
-
try:
|
| 98 |
-
for frame in frames:
|
| 99 |
-
# Convert to RGB and write raw bytes
|
| 100 |
-
rgb_frame = frame.convert('RGB')
|
| 101 |
-
process.stdin.write(rgb_frame.tobytes())
|
| 102 |
-
|
| 103 |
-
process.stdin.close()
|
| 104 |
-
stdout, stderr = process.communicate()
|
| 105 |
-
|
| 106 |
-
if process.returncode != 0:
|
| 107 |
-
raise RuntimeError(f"FFmpeg failed: {stderr.decode()}")
|
| 108 |
-
|
| 109 |
-
return output_path
|
| 110 |
-
|
| 111 |
-
except Exception as e:
|
| 112 |
-
process.kill()
|
| 113 |
-
raise e
|
| 114 |
|
| 115 |
|
| 116 |
# Test
|
|
|
|
| 63 |
|
| 64 |
def encode_frames_pipe(frames: List[Image.Image], output_path: str, fps: int = 24) -> str:
|
| 65 |
"""
|
| 66 |
+
Encode frames to WebM using temp files (more reliable than pipe).
|
|
|
|
| 67 |
"""
|
| 68 |
if not frames:
|
| 69 |
raise ValueError("No frames provided")
|
| 70 |
|
| 71 |
+
# Use the file-based approach which is more reliable
|
| 72 |
+
return encode_frames_to_webm(frames, output_path, fps)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
# Test
|