Ahmadkhan12 commited on
Commit
d04ba1b
·
verified ·
1 Parent(s): 1d727d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -2,6 +2,11 @@ import os
2
  import subprocess
3
  import gradio as gr
4
 
 
 
 
 
 
5
  def split_video_into_chunks(video_path, chunk_duration=6, output_dir="video_chunks"):
6
  # Create output directory
7
  os.makedirs(output_dir, exist_ok=True)
@@ -27,8 +32,8 @@ def split_video_into_chunks(video_path, chunk_duration=6, output_dir="video_chun
27
  "ffmpeg", "-i", video_path,
28
  "-ss", str(start_time),
29
  "-t", str(chunk_duration),
30
- "-c:v", "libx264", # Re-encode video stream
31
- "-c:a", "aac", # Re-encode audio stream
32
  output_file
33
  ]
34
  subprocess.run(command, check=True)
@@ -37,24 +42,30 @@ def split_video_into_chunks(video_path, chunk_duration=6, output_dir="video_chun
37
 
38
  return chunk_files
39
 
40
- def process_video(video):
41
- # Split the video into 6-second chunks
42
- chunk_files = split_video_into_chunks(video)
 
 
 
43
 
44
  # Return the list of chunk files for download
45
  return chunk_files
46
 
47
  # Gradio Interface
48
- def gradio_interface(video):
49
- return process_video(video)
50
 
51
  # Define Gradio app
52
  iface = gr.Interface(
53
  fn=gradio_interface,
54
- inputs=gr.Video(label="Upload Video"),
 
 
 
55
  outputs=gr.Files(label="Download Chunks"),
56
- title="Video Splitter into 6-Second Chunks",
57
- description="Upload a video, and the app will split it into 6-second chunks for download."
58
  )
59
 
60
  # Launch the app
 
2
  import subprocess
3
  import gradio as gr
4
 
5
+ def check_video_size(video_path, max_size_mb=1000): # Allow up to 1 GB
6
+ size_mb = os.path.getsize(video_path) / (1024 * 1024)
7
+ if size_mb > max_size_mb:
8
+ raise gr.Error(f"Video is too large (max {max_size_mb} MB allowed).")
9
+
10
  def split_video_into_chunks(video_path, chunk_duration=6, output_dir="video_chunks"):
11
  # Create output directory
12
  os.makedirs(output_dir, exist_ok=True)
 
32
  "ffmpeg", "-i", video_path,
33
  "-ss", str(start_time),
34
  "-t", str(chunk_duration),
35
+ "-c:v", "libx264", "-preset", "fast", # Faster encoding
36
+ "-c:a", "aac",
37
  output_file
38
  ]
39
  subprocess.run(command, check=True)
 
42
 
43
  return chunk_files
44
 
45
+ def process_video(video, chunk_duration):
46
+ # Check video size (up to 1 GB allowed)
47
+ check_video_size(video, max_size_mb=1000)
48
+
49
+ # Split the video into chunks with the specified duration
50
+ chunk_files = split_video_into_chunks(video, chunk_duration=chunk_duration)
51
 
52
  # Return the list of chunk files for download
53
  return chunk_files
54
 
55
  # Gradio Interface
56
+ def gradio_interface(video, chunk_duration):
57
+ return process_video(video, chunk_duration)
58
 
59
  # Define Gradio app
60
  iface = gr.Interface(
61
  fn=gradio_interface,
62
+ inputs=[
63
+ gr.Video(label="Upload Video"),
64
+ gr.Slider(minimum=1, maximum=60, value=6, step=1, label="Chunk Duration (seconds)")
65
+ ],
66
  outputs=gr.Files(label="Download Chunks"),
67
+ title="Video Splitter with Custom Chunk Duration",
68
+ description="Upload a video (max 1 GB) and choose the chunk duration in seconds. The app will split the video into chunks for download."
69
  )
70
 
71
  # Launch the app