Adityadn commited on
Commit
d81f996
·
verified ·
1 Parent(s): 3502cc3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -7
app.py CHANGED
@@ -2,11 +2,19 @@ import streamlit as st
2
  import ffmpeg
3
  import os
4
  from PIL import Image
 
 
5
 
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."""
@@ -15,24 +23,29 @@ def get_video_duration(video_path):
15
 
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
 
@@ -40,13 +53,13 @@ def convert_video(video, target_format, conversion_type, time_in_seconds=None):
40
  if time_in_seconds is None:
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()}"
50
  img.save(image_output_file, target_format.upper()) # Save with the desired format
51
 
52
  return image_output_file
 
2
  import ffmpeg
3
  import os
4
  from PIL import Image
5
+ import re
6
+ import tempfile
7
 
8
  # Supported formats
9
  supported_formats = ['avi', 'mp4', 'mov', 'mkv', 'flv', 'wmv', 'webm', 'mpeg', 'mpg', '3gp']
10
  audio_formats = ['mp3', 'wav', 'aac', 'flac']
11
+ gif_formats = ['gif']
12
+ image_formats = list(Image.SAVE.keys()) # Supported image formats
13
+
14
+ def sanitize_filename(filename):
15
+ """Sanitize filename by removing special characters and spaces."""
16
+ filename = re.sub(r'[^a-zA-Z0-9_.-]', '_', filename) # Replace invalid characters with '_'
17
+ return filename
18
 
19
  def get_video_duration(video_path):
20
  """Get video duration in seconds using ffmpeg."""
 
23
 
24
  def convert_video(video, target_format, conversion_type, time_in_seconds=None):
25
  try:
26
+ # Create a temporary directory for the uploaded file
27
+ temp_dir = tempfile.mkdtemp()
28
+
29
+ # Sanitize the filename and save the uploaded video to the temp directory
30
  base_name = os.path.splitext(os.path.basename(video.name))[0]
31
+ sanitized_base_name = sanitize_filename(base_name)
32
+ video_path = os.path.join(temp_dir, f"{sanitized_base_name}.mp4") # Saving as mp4 by default for now
33
 
34
  with open(video_path, "wb") as f:
35
  f.write(video.getbuffer()) # Save the uploaded video to a local file
36
 
37
  if conversion_type == 'Video to Video':
38
+ output_file = f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}"
39
  ffmpeg.input(video_path).output(output_file).run() # Use ffmpeg.input properly
40
  return output_file
41
 
42
  elif conversion_type == 'Video to Audio':
43
+ audio_output_file = f"flowly_ai_video_to_audio_{sanitized_base_name}.{target_format.lower()}"
44
  ffmpeg.input(video_path).output(audio_output_file).run() # Use ffmpeg.input properly
45
  return audio_output_file
46
 
47
  elif conversion_type == 'Video to GIF':
48
+ gif_output_file = f"flowly_ai_video_to_gif_{sanitized_base_name}.gif"
49
  ffmpeg.input(video_path).output(gif_output_file, vf="fps=10,scale=320:-1:flags=lanczos").run() # Use ffmpeg.input properly
50
  return gif_output_file
51
 
 
53
  if time_in_seconds is None:
54
  return "Please specify a valid time in seconds for image extraction."
55
 
56
+ image_output_file = f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.png"
57
  ffmpeg.input(video_path, ss=time_in_seconds).output(image_output_file, vframes=1).run() # Use ffmpeg.input properly
58
 
59
  # Convert the image to the desired format using Pillow
60
  img = Image.open(image_output_file)
61
  if target_format.lower() in image_formats:
62
+ image_output_file = f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.{target_format.lower()}"
63
  img.save(image_output_file, target_format.upper()) # Save with the desired format
64
 
65
  return image_output_file