Jamiiwej2903 commited on
Commit
4b71a9d
·
verified ·
1 Parent(s): f6a62bc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -31
main.py CHANGED
@@ -4,7 +4,9 @@ from fastapi.responses import StreamingResponse
4
  import io
5
  import requests
6
  from PIL import Image
7
- import subprocess
 
 
8
 
9
  app = FastAPI()
10
 
@@ -40,45 +42,32 @@ async def generate_video_api(
40
  # Add the generated image to frames
41
  frames.append(Image.open(io.BytesIO(response.content)))
42
 
43
- # Create a video from frames using PIL
44
- video_frames = []
45
- for frame in frames:
46
- img_byte_arr = io.BytesIO()
47
- frame.save(img_byte_arr, format='PNG')
48
- video_frames.append(img_byte_arr.getvalue())
 
 
49
 
50
- # Use FFmpeg to combine images into a video
51
- ffmpeg_cmd = [
52
- 'ffmpeg',
53
- '-f', 'image2pipe',
54
- '-framerate', str(fps),
55
- '-i', '-',
56
- '-c:v', 'libx264',
57
- '-pix_fmt', 'yuv420p',
58
- '-movflags', '+faststart',
59
- '-f', 'mp4',
60
- '-'
61
- ]
62
-
63
- process = subprocess.Popen(ffmpeg_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
64
-
65
- for frame in video_frames:
66
- process.stdin.write(frame)
67
-
68
- process.stdin.close()
69
- video_content, stderr = process.communicate()
70
-
71
- if process.returncode != 0:
72
- raise Exception(f"FFmpeg error: {stderr.decode()}")
73
 
74
  # Return the video as a streaming response
75
- return StreamingResponse(io.BytesIO(video_content), media_type="video/mp4")
76
 
77
  except requests.exceptions.HTTPError as http_err:
78
  # Handle HTTP errors
79
  error_detail = f"HTTP error occurred: {http_err}"
80
  raise HTTPException(status_code=500, detail=error_detail)
81
 
 
 
 
 
82
  except Exception as err:
83
  # Handle any other errors
84
  raise HTTPException(status_code=500, detail=f"An error occurred: {err}")
 
4
  import io
5
  import requests
6
  from PIL import Image
7
+ import ffmpeg
8
+ import tempfile
9
+ import os
10
 
11
  app = FastAPI()
12
 
 
42
  # Add the generated image to frames
43
  frames.append(Image.open(io.BytesIO(response.content)))
44
 
45
+ # Create a temporary directory
46
+ with tempfile.TemporaryDirectory() as tmpdir:
47
+ # Save frames as temporary files
48
+ frame_files = []
49
+ for i, frame in enumerate(frames):
50
+ frame_file = os.path.join(tmpdir, f"frame_{i:03d}.png")
51
+ frame.save(frame_file)
52
+ frame_files.append(frame_file)
53
 
54
+ # Use ffmpeg-python to combine images into a video
55
+ input_stream = ffmpeg.input(os.path.join(tmpdir, 'frame_%03d.png'), framerate=fps)
56
+ output_stream = ffmpeg.output(input_stream, 'pipe:', vcodec='libx264', pix_fmt='yuv420p', format='mp4')
57
+ out, err = ffmpeg.run(output_stream, capture_stdout=True, capture_stderr=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Return the video as a streaming response
60
+ return StreamingResponse(io.BytesIO(out), media_type="video/mp4")
61
 
62
  except requests.exceptions.HTTPError as http_err:
63
  # Handle HTTP errors
64
  error_detail = f"HTTP error occurred: {http_err}"
65
  raise HTTPException(status_code=500, detail=error_detail)
66
 
67
+ except ffmpeg.Error as e:
68
+ # Handle FFmpeg errors
69
+ raise HTTPException(status_code=500, detail=f"FFmpeg error occurred: {e.stderr.decode()}")
70
+
71
  except Exception as err:
72
  # Handle any other errors
73
  raise HTTPException(status_code=500, detail=f"An error occurred: {err}")