Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,21 +2,23 @@ import streamlit as st
|
|
| 2 |
import tempfile
|
| 3 |
import subprocess
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
st.set_page_config(page_title="Video Speed & Compression Tool", layout="centered")
|
| 7 |
|
| 8 |
-
st.title("π¬ Video Speed & Compression Tool (
|
| 9 |
|
| 10 |
uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "mov", "avi", "mkv"])
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
-
compress = st.toggle("Compress Video?")
|
| 19 |
-
|
| 20 |
if uploaded_file is not None:
|
| 21 |
st.video(uploaded_file)
|
| 22 |
|
|
@@ -31,30 +33,48 @@ if uploaded_file is not None:
|
|
| 31 |
video_speed = 1 / speed
|
| 32 |
audio_speed = speed
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Show processed video
|
| 60 |
st.video(output_path)
|
|
|
|
| 2 |
import tempfile
|
| 3 |
import subprocess
|
| 4 |
import os
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
st.set_page_config(page_title="Video Speed & Compression Tool", layout="centered")
|
| 8 |
|
| 9 |
+
st.title("π¬ Video Speed & Compression Tool (Custom Speed + Always Compress)")
|
| 10 |
|
| 11 |
uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "mov", "avi", "mkv"])
|
| 12 |
|
| 13 |
+
# Let user choose any custom speed
|
| 14 |
+
speed = st.number_input(
|
| 15 |
+
"Set Playback Speed (e.g., 1.2 for 20% faster, 0.5 for half speed)",
|
| 16 |
+
min_value=0.25,
|
| 17 |
+
max_value=4.0,
|
| 18 |
+
value=1.5,
|
| 19 |
+
step=0.05
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
| 22 |
if uploaded_file is not None:
|
| 23 |
st.video(uploaded_file)
|
| 24 |
|
|
|
|
| 33 |
video_speed = 1 / speed
|
| 34 |
audio_speed = speed
|
| 35 |
|
| 36 |
+
# Always compress
|
| 37 |
+
cmd = [
|
| 38 |
+
"ffmpeg", "-i", input_path,
|
| 39 |
+
"-filter_complex", f"[0:v]setpts={video_speed}*PTS[v];[0:a]atempo={audio_speed}[a]",
|
| 40 |
+
"-map", "[v]", "-map", "[a]",
|
| 41 |
+
"-vcodec", "libx264", "-crf", "28", "-preset", "fast",
|
| 42 |
+
"-movflags", "+faststart",
|
| 43 |
+
"-y", output_path
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
# Progress bar setup
|
| 47 |
+
progress_text = st.empty()
|
| 48 |
+
progress_bar = st.progress(0)
|
| 49 |
+
|
| 50 |
+
# Get video duration first
|
| 51 |
+
probe = subprocess.run(
|
| 52 |
+
["ffprobe", "-v", "error", "-show_entries", "format=duration",
|
| 53 |
+
"-of", "default=noprint_wrappers=1:nokey=1", input_path],
|
| 54 |
+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
| 55 |
+
)
|
| 56 |
+
try:
|
| 57 |
+
total_duration = float(probe.stdout.strip())
|
| 58 |
+
except:
|
| 59 |
+
total_duration = None
|
| 60 |
+
|
| 61 |
+
# Run ffmpeg with live output parsing
|
| 62 |
+
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1)
|
| 63 |
+
|
| 64 |
+
for line in process.stdout:
|
| 65 |
+
if "time=" in line and total_duration:
|
| 66 |
+
# Extract current time processed
|
| 67 |
+
match = re.search(r"time=(\d+:\d+:\d+\.\d+)", line)
|
| 68 |
+
if match:
|
| 69 |
+
h, m, s = match.group(1).split(":")
|
| 70 |
+
elapsed = int(h) * 3600 + int(m) * 60 + float(s)
|
| 71 |
+
percent = min(int((elapsed / total_duration) * 100), 100)
|
| 72 |
+
progress_bar.progress(percent)
|
| 73 |
+
progress_text.text(f"Processing... {percent}%")
|
| 74 |
+
|
| 75 |
+
process.wait()
|
| 76 |
+
progress_bar.progress(100)
|
| 77 |
+
progress_text.text("β
Processing complete!")
|
| 78 |
|
| 79 |
# Show processed video
|
| 80 |
st.video(output_path)
|