167AliRaza commited on
Commit
1903c80
Β·
verified Β·
1 Parent(s): 392f7dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -31
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 (Ultra Fast Lossless Mode)")
9
 
10
  uploaded_file = st.file_uploader("Upload a video file", type=["mp4", "mov", "avi", "mkv"])
11
 
12
- speed = st.selectbox(
13
- "Select Playback Speed",
14
- options=[1.2, 1.5, 2.0],
15
- index=1
 
 
 
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
- if compress:
35
- # Re-encode with compression
36
- cmd = [
37
- "ffmpeg", "-i", input_path,
38
- "-filter_complex", f"[0:v]setpts={video_speed}*PTS[v];[0:a]atempo={audio_speed}[a]",
39
- "-map", "[v]", "-map", "[a]",
40
- "-vcodec", "libx264", "-crf", "28", "-preset", "fast",
41
- "-movflags", "+faststart",
42
- "-y", output_path
43
- ]
44
- else:
45
- # Lossless mode (copy video, re-encode only audio, make it browser-playable)
46
- cmd = [
47
- "ffmpeg", "-i", input_path,
48
- "-filter_complex", f"[0:v]setpts={video_speed}*PTS[v];[0:a]atempo={audio_speed}[a]",
49
- "-map", "[v]", "-map", "[a]",
50
- "-c:v", "copy", "-c:a", "aac", "-b:a", "192k",
51
- "-movflags", "+faststart", # ensures browser playback
52
- "-y", output_path
53
- ]
54
-
55
- subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
56
-
57
- st.success("βœ… Processing complete!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)