Jamiiwej2903 commited on
Commit
12de95f
1 Parent(s): 4b71a9d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +14 -3
main.py CHANGED
@@ -51,13 +51,24 @@ async def generate_video_api(
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
 
51
  frame.save(frame_file)
52
  frame_files.append(frame_file)
53
 
54
+ # Create a temporary file for the video
55
+ with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_video:
56
+ temp_video_path = temp_video.name
57
+
58
  # Use ffmpeg-python to combine images into a video
59
  input_stream = ffmpeg.input(os.path.join(tmpdir, 'frame_%03d.png'), framerate=fps)
60
+ output_stream = ffmpeg.output(input_stream, temp_video_path, vcodec='libx264', pix_fmt='yuv420p')
61
+ ffmpeg.run(output_stream, overwrite_output=True)
62
+
63
+ # Read the temporary video file
64
+ with open(temp_video_path, 'rb') as video_file:
65
+ video_content = video_file.read()
66
+
67
+ # Delete the temporary video file
68
+ os.unlink(temp_video_path)
69
 
70
  # Return the video as a streaming response
71
+ return StreamingResponse(io.BytesIO(video_content), media_type="video/mp4")
72
 
73
  except requests.exceptions.HTTPError as http_err:
74
  # Handle HTTP errors