Adityadn commited on
Commit
6d31a4f
·
verified ·
1 Parent(s): a7cedc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -1
app.py CHANGED
@@ -10,6 +10,32 @@ st.set_page_config(layout="wide", page_title="Video Conversion Tool")
10
  # Supported formats
11
  supported_formats = sorted(['3GP', 'ASF', 'AVI', 'DIVX', 'FLV', 'M2TS', 'M4V', 'MKV', 'MOV', 'MP4', 'MPEG', 'MPG', 'MTS', 'TS', 'VOB', 'WEBM', 'WMV', 'XVID'])
12
  audio_formats = sorted(['MP3', 'WAV', 'AAC', 'FLAC', 'OGG', 'M4A', 'ALAC', 'WMA', 'AIFF', 'OPUS', 'APE', 'CAF', 'PCM', 'DTS', 'TTA', 'AMR', 'MID', 'SPX', 'WV', 'RA', 'TAK'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  video_path = os.path.join(temp_dir, f"{sanitized_base_name}.mp4") # Saving as mp4 by default for now
14
 
15
  with open(video_path, "wb") as f:
@@ -17,6 +43,46 @@ audio_formats = sorted(['MP3', 'WAV', 'AAC', 'FLAC', 'OGG', 'M4A', 'ALAC', 'WMA'
17
 
18
  if conversion_type == 'Video to Video':
19
  output_file = f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  return image_formats
21
  return []
22
 
@@ -80,4 +146,4 @@ def main():
80
  )
81
 
82
  if __name__ == "__main__":
83
- main()
 
10
  # Supported formats
11
  supported_formats = sorted(['3GP', 'ASF', 'AVI', 'DIVX', 'FLV', 'M2TS', 'M4V', 'MKV', 'MOV', 'MP4', 'MPEG', 'MPG', 'MTS', 'TS', 'VOB', 'WEBM', 'WMV', 'XVID'])
12
  audio_formats = sorted(['MP3', 'WAV', 'AAC', 'FLAC', 'OGG', 'M4A', 'ALAC', 'WMA', 'AIFF', 'OPUS', 'APE', 'CAF', 'PCM', 'DTS', 'TTA', 'AMR', 'MID', 'SPX', 'WV', 'RA', 'TAK'])
13
+ gif_formats = ['GIF']
14
+ image_formats = sorted(Image.SAVE.keys()) or ['BLP', 'BMP', 'BUFR', 'DDS', 'DIB', 'EPS',
15
+ 'GIF', 'GRIB', 'HDF5', 'ICNS', 'ICO', 'IM',
16
+ 'JPEG', 'JPEG2000', 'MPO', 'MSP', 'PALM', 'PCX',
17
+ 'PDF', 'PNG', 'PPM', 'SGI', 'SPIDER', 'TGA', 'TIFF',
18
+ 'WEBP', 'WMX', 'XBM']
19
+
20
+ def sanitize_filename(filename):
21
+ """Sanitize filename by removing special characters and spaces."""
22
+ filename = re.sub(r'[^a-zA-Z0-9_.-]', '_', filename) # Replace invalid characters with '_'
23
+ return filename
24
+
25
+ def get_video_duration(video_path):
26
+ """Get video duration in seconds using ffmpeg."""
27
+ probe = ffmpeg.probe(video_path, v='error', select_streams='v:0', show_entries='stream=duration')
28
+ duration_in_seconds = float(probe['streams'][0]['duration'])
29
+ return duration_in_seconds
30
+
31
+ def convert_video(video, target_format, conversion_type, time_in_seconds=None):
32
+ try:
33
+ # Create a temporary directory for the uploaded file
34
+ temp_dir = tempfile.mkdtemp()
35
+
36
+ # Sanitize the filename and save the uploaded video to the temp directory
37
+ base_name = os.path.splitext(os.path.basename(video.name))[0]
38
+ sanitized_base_name = sanitize_filename(base_name)
39
  video_path = os.path.join(temp_dir, f"{sanitized_base_name}.mp4") # Saving as mp4 by default for now
40
 
41
  with open(video_path, "wb") as f:
 
43
 
44
  if conversion_type == 'Video to Video':
45
  output_file = f"flowly_ai_video_converter_{sanitized_base_name}.{target_format.lower()}"
46
+ ffmpeg.input(video_path).output(output_file).run() # Use ffmpeg.input properly
47
+ return output_file
48
+
49
+ elif conversion_type == 'Video to Audio':
50
+ audio_output_file = f"flowly_ai_video_to_audio_{sanitized_base_name}.{target_format.lower()}"
51
+ ffmpeg.input(video_path).output(audio_output_file).run() # Use ffmpeg.input properly
52
+ return audio_output_file
53
+
54
+ elif conversion_type == 'Video to GIF':
55
+ gif_output_file = f"flowly_ai_video_to_gif_{sanitized_base_name}.gif"
56
+ ffmpeg.input(video_path).output(gif_output_file, vf="fps=10,scale=320:-1:flags=lanczos").run() # Use ffmpeg.input properly
57
+ return gif_output_file
58
+
59
+ elif conversion_type == 'Video to Image':
60
+ if time_in_seconds is None:
61
+ return "Please specify a valid time in seconds for image extraction."
62
+
63
+ image_output_file = f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.png"
64
+ ffmpeg.input(video_path, ss=time_in_seconds).output(image_output_file, vframes=1).run() # Use ffmpeg.input properly
65
+
66
+ # Convert the image to the desired format using Pillow
67
+ img = Image.open(image_output_file)
68
+ if target_format.lower() in image_formats:
69
+ image_output_file = f"flowly_ai_video_to_image_{sanitized_base_name}_{time_in_seconds}.{target_format.lower()}"
70
+ img.save(image_output_file, target_format.upper()) # Save with the desired format
71
+
72
+ return image_output_file
73
+
74
+ except Exception as e:
75
+ return f"Error: {e}"
76
+
77
+ def update_format_choices(conversion_type):
78
+ """Update available format choices based on the conversion type."""
79
+ if conversion_type == 'Video to Video':
80
+ return supported_formats
81
+ elif conversion_type == 'Video to Audio':
82
+ return audio_formats
83
+ elif conversion_type == 'Video to GIF':
84
+ return gif_formats
85
+ elif conversion_type == 'Video to Image':
86
  return image_formats
87
  return []
88
 
 
146
  )
147
 
148
  if __name__ == "__main__":
149
+ main()