Saqib commited on
Commit
1ebc125
1 Parent(s): 13eb97b

Update modules/app.py

Browse files
Files changed (1) hide show
  1. modules/app.py +40 -30
modules/app.py CHANGED
@@ -72,48 +72,58 @@ async def download_file(url: str, suffix: str):
72
 
73
  @app.post("/add_audio_to_image")
74
  async def add_audio_to_image(input_data: AudioImageInput):
75
- # Download image and audio files
76
- temp_image_path = await download_file(str(input_data.image_url), ".png")
77
- temp_audio_path = await download_file(str(input_data.audio_url), ".mp3")
 
78
 
79
- # Generate a unique filename
80
- output_filename = f"{uuid.uuid4()}.mp4"
81
- output_path = os.path.join(OUTPUT_DIR, output_filename)
82
 
83
- # Use ffmpeg to combine image and audio into a video
84
- input_image = ffmpeg.input(temp_image_path, loop=1, t=5) # 5 seconds duration
85
- input_audio = ffmpeg.input(temp_audio_path)
86
- ffmpeg.concat(input_image, input_audio, v=1, a=1).output(output_path, vcodec='libx264', acodec='aac').run()
87
 
88
- # Clean up temporary files
89
- os.unlink(temp_image_path)
90
- os.unlink(temp_audio_path)
91
 
92
- # Return the URL path to the output file
93
- return f"/output/{output_filename}"
 
 
 
 
94
 
95
  @app.post("/concatenate_videos")
96
  async def concatenate_videos(input_data: VideosInput):
97
- temp_video_paths = []
 
98
 
99
- # Download videos to temporary files
100
- for video_url in input_data.video_urls:
101
- temp_video_paths.append(await download_file(str(video_url), ".mp4"))
102
 
103
- # Generate a unique filename
104
- output_filename = f"{uuid.uuid4()}.mp4"
105
- output_path = os.path.join(OUTPUT_DIR, output_filename)
106
 
107
- # Concatenate video clips using ffmpeg
108
- inputs = [ffmpeg.input(path) for path in temp_video_paths]
109
- ffmpeg.concat(*inputs, v=1, a=1).output(output_path, vcodec='libx264', acodec='aac').run()
110
 
111
- # Clean up temporary files
112
- for path in temp_video_paths:
113
- os.unlink(path)
114
 
115
- # Return the URL path to the output file
116
- return f"/output/{output_filename}"
 
 
 
 
117
 
118
  @app.get("/get_audio")
119
  async def get_audio(url: str):
 
72
 
73
  @app.post("/add_audio_to_image")
74
  async def add_audio_to_image(input_data: AudioImageInput):
75
+ try:
76
+ # Download image and audio files
77
+ temp_image_path = await download_file(str(input_data.image_url), ".png")
78
+ temp_audio_path = await download_file(str(input_data.audio_url), ".mp3")
79
 
80
+ # Generate a unique filename
81
+ output_filename = f"{uuid.uuid4()}.mp4"
82
+ output_path = os.path.join(OUTPUT_DIR, output_filename)
83
 
84
+ # Use ffmpeg to combine image and audio into a video
85
+ input_image = ffmpeg.input(temp_image_path, loop=1, t=5) # 5 seconds duration
86
+ input_audio = ffmpeg.input(temp_audio_path)
87
+ ffmpeg.concat(input_image, input_audio, v=1, a=1).output(output_path, vcodec='libx264', acodec='aac').run()
88
 
89
+ # Clean up temporary files
90
+ os.unlink(temp_image_path)
91
+ os.unlink(temp_audio_path)
92
 
93
+ # Return the URL path to the output file
94
+ return f"/output/{output_filename}"
95
+ except Exception as e:
96
+ print(f"An error occurred: {str(e)}")
97
+ print(traceback.format_exc())
98
+ raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
99
 
100
  @app.post("/concatenate_videos")
101
  async def concatenate_videos(input_data: VideosInput):
102
+ try:
103
+ temp_video_paths = []
104
 
105
+ # Download videos to temporary files
106
+ for video_url in input_data.video_urls:
107
+ temp_video_paths.append(await download_file(str(video_url), ".mp4"))
108
 
109
+ # Generate a unique filename
110
+ output_filename = f"{uuid.uuid4()}.mp4"
111
+ output_path = os.path.join(OUTPUT_DIR, output_filename)
112
 
113
+ # Concatenate video clips using ffmpeg
114
+ inputs = [ffmpeg.input(path) for path in temp_video_paths]
115
+ ffmpeg.concat(*inputs, v=1, a=1).output(output_path, vcodec='libx264', acodec='aac').run()
116
 
117
+ # Clean up temporary files
118
+ for path in temp_video_paths:
119
+ os.unlink(path)
120
 
121
+ # Return the URL path to the output file
122
+ return f"/output/{output_filename}"
123
+ except Exception as e:
124
+ print(f"An error occurred: {str(e)}")
125
+ print(traceback.format_exc())
126
+ raise HTTPException(status_code=500, detail=f"An unexpected error occurred: {str(e)}")
127
 
128
  @app.get("/get_audio")
129
  async def get_audio(url: str):