Adityadn commited on
Commit
c08ff30
·
verified ·
1 Parent(s): fc7fdb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -18
app.py CHANGED
@@ -6,6 +6,11 @@ from PIL import Image
6
  import re
7
  import tempfile
8
  import shutil
 
 
 
 
 
9
 
10
  st.set_page_config(layout="wide", page_title="Video Conversion Tool")
11
 
@@ -18,6 +23,7 @@ image_formats = sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB'
18
  'TGA', 'TIFF', 'WEBP', 'WMX', 'XBM']
19
 
20
  CACHE_DIR = tempfile.mkdtemp()
 
21
 
22
  def sanitize_filename(filename):
23
  """Sanitize filename by removing special characters and spaces."""
@@ -38,48 +44,50 @@ def get_video_duration(video_path):
38
 
39
  def convert_video(video, target_format, conversion_type, time_in_seconds=None):
40
  try:
41
- clean_cache() # Clean up cache before processing
 
42
 
43
- # Save the uploaded video to the temp directory
44
- sanitized_base_name = sanitize_filename(os.path.splitext(video.name)[0])
45
- video_path = os.path.join(CACHE_DIR, f"{sanitized_base_name}.mp4")
 
46
 
47
  with open(video_path, "wb") as f:
48
- f.write(video.getbuffer())
49
 
50
  if conversion_type == 'Video to Video':
51
- output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}")
52
- ffmpeg.input(video_path).output(output_file, overwrite_output=True).run(overwrite_output=True)
53
  return output_file
54
 
55
  elif conversion_type == 'Video to Audio':
56
- output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_to_audio_{sanitized_base_name}.{target_format.lower()}")
57
- ffmpeg.input(video_path).output(output_file, overwrite_output=True).run(overwrite_output=True)
58
- return output_file
59
 
60
  elif conversion_type == 'Video to GIF':
61
- output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_to_gif_{sanitized_base_name}.gif")
62
- ffmpeg.input(video_path).output(output_file, vf="fps=10,scale=320:-1:flags=lanczos", overwrite_output=True).run(overwrite_output=True)
63
- return output_file
64
 
65
  elif conversion_type == 'Video to Image':
66
  if time_in_seconds is None:
67
  return "Please specify a valid time in seconds for image extraction."
68
 
69
- image_output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.png")
70
- ffmpeg.input(video_path, ss=time_in_seconds).output(image_output_file, vframes=1, overwrite_output=True).run(overwrite_output=True)
71
 
72
  # Convert the image to the desired format using Pillow
73
  img = Image.open(image_output_file)
74
  if target_format.lower() in image_formats:
75
- image_output_file = os.path.join(CACHE_DIR, f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.{target_format.lower()}")
76
- img.save(image_output_file, target_format.upper())
77
 
78
  return image_output_file
79
 
80
  except Exception as e:
81
  return f"Error: {e}"
82
-
83
  def update_format_choices(conversion_type):
84
  """Update available format choices based on the conversion type."""
85
  if conversion_type == 'Video to Video':
 
6
  import re
7
  import tempfile
8
  import shutil
9
+ import threading
10
+
11
+ def delete_temp_dir(directory, delay=900): # 15 minutes = 900 seconds
12
+ timer = threading.Timer(delay, shutil.rmtree, [directory])
13
+ timer.start()
14
 
15
  st.set_page_config(layout="wide", page_title="Video Conversion Tool")
16
 
 
23
  'TGA', 'TIFF', 'WEBP', 'WMX', 'XBM']
24
 
25
  CACHE_DIR = tempfile.mkdtemp()
26
+ delete_temp_dir(CACHE_DIR, delay=900) # Clean up cache after 15 minutes
27
 
28
  def sanitize_filename(filename):
29
  """Sanitize filename by removing special characters and spaces."""
 
44
 
45
  def convert_video(video, target_format, conversion_type, time_in_seconds=None):
46
  try:
47
+ # Create a temporary directory for the uploaded file
48
+ temp_dir = tempfile.mkdtemp()
49
 
50
+ # Sanitize the filename and save the uploaded video to the temp directory
51
+ base_name = os.path.splitext(os.path.basename(video.name))[0]
52
+ sanitized_base_name = sanitize_filename(base_name)
53
+ video_path = os.path.join(temp_dir, f"{sanitized_base_name}.mp4") # Saving as mp4 by default for now
54
 
55
  with open(video_path, "wb") as f:
56
+ f.write(video.getbuffer()) # Save the uploaded video to a local file
57
 
58
  if conversion_type == 'Video to Video':
59
+ output_file = f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}"
60
+ ffmpeg.input(video_path).output(output_file).overwrite_output().run() # Add .overwrite_output()
61
  return output_file
62
 
63
  elif conversion_type == 'Video to Audio':
64
+ audio_output_file = f"flowly_ai_video_to_audio_{sanitized_base_name}.{target_format.lower()}"
65
+ ffmpeg.input(video_path).output(audio_output_file).overwrite_output().run() # Add .overwrite_output()
66
+ return audio_output_file
67
 
68
  elif conversion_type == 'Video to GIF':
69
+ gif_output_file = f"flowly_ai_video_to_gif_{sanitized_base_name}.gif"
70
+ ffmpeg.input(video_path).output(gif_output_file, vf="fps=10,scale=320:-1:flags=lanczos").overwrite_output().run() # Add .overwrite_output()
71
+ return gif_output_file
72
 
73
  elif conversion_type == 'Video to Image':
74
  if time_in_seconds is None:
75
  return "Please specify a valid time in seconds for image extraction."
76
 
77
+ image_output_file = f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.png"
78
+ ffmpeg.input(video_path, ss=time_in_seconds).output(image_output_file, vframes=1).overwrite_output().run() # Add .overwrite_output()
79
 
80
  # Convert the image to the desired format using Pillow
81
  img = Image.open(image_output_file)
82
  if target_format.lower() in image_formats:
83
+ image_output_file = f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.{target_format.lower()}"
84
+ img.save(image_output_file, target_format.upper()) # Save with the desired format
85
 
86
  return image_output_file
87
 
88
  except Exception as e:
89
  return f"Error: {e}"
90
+
91
  def update_format_choices(conversion_type):
92
  """Update available format choices based on the conversion type."""
93
  if conversion_type == 'Video to Video':