naohiro701 commited on
Commit
7d582a8
·
verified ·
1 Parent(s): e769ad2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import ffmpeg
3
+ import math
4
+ import os
5
+ from pathlib import Path
6
+
7
+ def split_video(input_file_path, output_dir, duration=20):
8
+ """
9
+ Splits the input video file into segments of specified duration.
10
+
11
+ Parameters:
12
+ - input_file_path (str): Path to the input video file.
13
+ - output_dir (str): Directory to save output segments.
14
+ - duration (int): Duration of each segment in seconds.
15
+
16
+ Returns:
17
+ - segment_files (list): List of file paths for each video segment.
18
+ """
19
+ probe = ffmpeg.probe(input_file_path)
20
+ total_duration = float(probe['format']['duration'])
21
+ num_segments = math.ceil(total_duration / duration)
22
+ segment_files = []
23
+
24
+ for i in range(num_segments):
25
+ start_time = i * duration
26
+ output_file = os.path.join(output_dir, f"segment_{i + 1}.mp4")
27
+ ffmpeg.input(input_file_path, ss=start_time, t=duration).output(output_file).run()
28
+ segment_files.append(output_file)
29
+
30
+ return segment_files
31
+
32
+ # Streamlit UI
33
+ st.title("Video Splitter")
34
+ st.write("Upload a video file, specify the segment duration, and download the split video segments.")
35
+
36
+ # File uploader
37
+ uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "mov", "avi"])
38
+
39
+ # Duration input
40
+ duration = st.number_input("Segment duration (seconds)", min_value=1, max_value=300, value=20)
41
+
42
+ if uploaded_file is not None:
43
+ # Save the uploaded file to a temporary path
44
+ input_file_path = Path("uploaded_video.mp4")
45
+ with open(input_file_path, "wb") as f:
46
+ f.write(uploaded_file.getbuffer())
47
+
48
+ # Output directory for segments
49
+ output_dir = Path("segments")
50
+ output_dir.mkdir(exist_ok=True)
51
+
52
+ if st.button("Split Video"):
53
+ # Split video into segments
54
+ segment_files = split_video(str(input_file_path), str(output_dir), duration)
55
+
56
+ st.success(f"Video split into {len(segment_files)} segments.")
57
+
58
+ # Provide download links for each segment
59
+ for segment in segment_files:
60
+ with open(segment, "rb") as file:
61
+ btn = st.download_button(
62
+ label=f"Download {Path(segment).name}",
63
+ data=file,
64
+ file_name=Path(segment).name
65
+ )