Adityadn commited on
Commit
d2cdcd3
·
verified ·
1 Parent(s): e1b08c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -6,8 +6,7 @@ from PIL import Image
6
  # Supported formats
7
  supported_formats = ['avi', 'mp4', 'mov', 'mkv', 'flv', 'wmv', 'webm', 'mpeg', 'mpg', '3gp']
8
  audio_formats = ['mp3', 'wav', 'aac', 'flac']
9
- gif_formats = ['gif']
10
- image_formats = list(Image.SAVE.keys()) # Supported image formats
11
 
12
  def get_video_duration(video_path):
13
  """Get video duration in seconds using ffmpeg."""
@@ -17,20 +16,24 @@ def get_video_duration(video_path):
17
  def convert_video(video, target_format, conversion_type, time_in_seconds=None):
18
  try:
19
  base_name = os.path.splitext(os.path.basename(video.name))[0]
 
 
 
 
20
 
21
  if conversion_type == 'Video to Video':
22
  output_file = f"flowly_ai_video_converter_{base_name}.{target_format.lower()}"
23
- ffmpeg.input(video.name).output(output_file).run()
24
  return output_file
25
 
26
  elif conversion_type == 'Video to Audio':
27
  audio_output_file = f"flowly_ai_video_to_audio_{base_name}.{target_format.lower()}"
28
- ffmpeg.input(video.name).output(audio_output_file).run()
29
  return audio_output_file
30
 
31
  elif conversion_type == 'Video to GIF':
32
  gif_output_file = f"flowly_ai_video_to_gif_{base_name}.gif"
33
- ffmpeg.input(video.name).output(gif_output_file, vf="fps=10,scale=320:-1:flags=lanczos").run()
34
  return gif_output_file
35
 
36
  elif conversion_type == 'Video to Image':
@@ -38,8 +41,9 @@ def convert_video(video, target_format, conversion_type, time_in_seconds=None):
38
  return "Please specify a valid time in seconds for image extraction."
39
 
40
  image_output_file = f"flowly_ai_video_to_image_{base_name}_{time_in_seconds}.png"
41
- ffmpeg.input(video.name, ss=time_in_seconds).output(image_output_file, vframes=1).run()
42
 
 
43
  img = Image.open(image_output_file)
44
  if target_format.lower() in image_formats:
45
  image_output_file = f"flowly_ai_video_to_image_{base_name}_{time_in_seconds}.{target_format.lower()}"
 
6
  # Supported formats
7
  supported_formats = ['avi', 'mp4', 'mov', 'mkv', 'flv', 'wmv', 'webm', 'mpeg', 'mpg', '3gp']
8
  audio_formats = ['mp3', 'wav', 'aac', 'flac']
9
+ image_formats = sorted(Image.SAVE.keys()) # Supported image formats
 
10
 
11
  def get_video_duration(video_path):
12
  """Get video duration in seconds using ffmpeg."""
 
16
  def convert_video(video, target_format, conversion_type, time_in_seconds=None):
17
  try:
18
  base_name = os.path.splitext(os.path.basename(video.name))[0]
19
+ video_path = f"./temp/{base_name}"
20
+
21
+ with open(video_path, "wb") as f:
22
+ f.write(video.getbuffer()) # Save the uploaded video to a local file
23
 
24
  if conversion_type == 'Video to Video':
25
  output_file = f"flowly_ai_video_converter_{base_name}.{target_format.lower()}"
26
+ ffmpeg.input(video_path).output(output_file).run() # Use ffmpeg.input properly
27
  return output_file
28
 
29
  elif conversion_type == 'Video to Audio':
30
  audio_output_file = f"flowly_ai_video_to_audio_{base_name}.{target_format.lower()}"
31
+ ffmpeg.input(video_path).output(audio_output_file).run() # Use ffmpeg.input properly
32
  return audio_output_file
33
 
34
  elif conversion_type == 'Video to GIF':
35
  gif_output_file = f"flowly_ai_video_to_gif_{base_name}.gif"
36
+ ffmpeg.input(video_path).output(gif_output_file, vf="fps=10,scale=320:-1:flags=lanczos").run() # Use ffmpeg.input properly
37
  return gif_output_file
38
 
39
  elif conversion_type == 'Video to Image':
 
41
  return "Please specify a valid time in seconds for image extraction."
42
 
43
  image_output_file = f"flowly_ai_video_to_image_{base_name}_{time_in_seconds}.png"
44
+ ffmpeg.input(video_path, ss=time_in_seconds).output(image_output_file, vframes=1).run() # Use ffmpeg.input properly
45
 
46
+ # Convert the image to the desired format using Pillow
47
  img = Image.open(image_output_file)
48
  if target_format.lower() in image_formats:
49
  image_output_file = f"flowly_ai_video_to_image_{base_name}_{time_in_seconds}.{target_format.lower()}"