smjain commited on
Commit
20747b7
1 Parent(s): b1a3bc7

Upload myinfer_latest.py

Browse files
Files changed (1) hide show
  1. myinfer_latest.py +21 -17
myinfer_latest.py CHANGED
@@ -32,9 +32,9 @@ from flask_dance.contrib.google import make_google_blueprint, google
32
  import io
33
  from space import ensure_model_in_weights_dir,upload_to_do
34
  import boto3
35
- from moviepy.editor import *
36
  import os
37
-
 
38
 
39
 
40
 
@@ -247,27 +247,31 @@ def get_status(audio_id):
247
 
248
 
249
  def merge_audio_image(mp3_path, image_path, output_dir, unique_id):
250
- # Load the image
251
- image_clip = ImageClip(image_path)
252
 
253
- # Load the audio
254
- audio_clip = AudioFileClip(mp3_path)
 
255
 
256
- # Set the duration of the image clip to match the audio duration
257
- image_clip = image_clip.set_duration(audio_clip.duration)
 
258
 
259
- # Resize the image clip to Instagram's square dimensions (1080x1080)
260
- #image_clip = image_clip.resize((1080, 1080))
261
- image_clip = image_clip.resize((1080, 1080))
262
 
263
- # Set the audio to the image clip
264
- final_clip = image_clip.set_audio(audio_clip)
 
265
 
266
- # Generate output file path
267
- output_path = os.path.join(output_dir, f"{unique_id}.mp4")
 
268
 
269
- # Write the output video file
270
- final_clip.write_videofile(output_path, codec='libx264', audio_codec='aac')
271
 
272
  return output_path
273
 
 
32
  import io
33
  from space import ensure_model_in_weights_dir,upload_to_do
34
  import boto3
 
35
  import os
36
+ import ffmpeg
37
+ import os
38
 
39
 
40
 
 
247
 
248
 
249
  def merge_audio_image(mp3_path, image_path, output_dir, unique_id):
250
+ # Generate output file path
251
+ output_path = os.path.join(output_dir, f"{unique_id}.mp4")
252
 
253
+ # Ensure the image file exists
254
+ if not os.path.isfile(image_path):
255
+ raise FileNotFoundError(f"Image file not found: {image_path}")
256
 
257
+ # Ensure the audio file exists
258
+ if not os.path.isfile(mp3_path):
259
+ raise FileNotFoundError(f"Audio file not found: {mp3_path}")
260
 
261
+ # Get the duration of the audio file
262
+ probe = ffmpeg.probe(mp3_path)
263
+ audio_duration = float(probe['format']['duration'])
264
 
265
+ # Create the ffmpeg command to combine image and audio into a video
266
+ input_image = ffmpeg.input(image_path, loop=1, t=audio_duration)
267
+ input_audio = ffmpeg.input(mp3_path)
268
 
269
+ # Apply scale and pad filters to the image
270
+ video_stream = input_image.filter('scale', size='1080x1080', force_original_aspect_ratio='decrease')\
271
+ .filter('pad', 1080, 1080, -1, -1)
272
 
273
+ # Combine image and audio into a video
274
+ ffmpeg.output(video_stream, input_audio, output_path, vcodec='libx264', acodec='aac', strict='experimental', pix_fmt='yuv420p').run()
275
 
276
  return output_path
277