File size: 1,533 Bytes
4a9476d 6d20493 4a9476d 6d20493 4a9476d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import tempfile
import subprocess
import os
from moviepy.editor import VideoFileClip
import streamlit as st
import traceback
import shutil
# --------------------------
# Utility: Trim videos to 2 minutes
# --------------------------
def trim_video(video_path, max_duration=120):
"""Trims video to max_duration (in seconds) and extracts audio."""
try:
video = VideoFileClip(video_path)
duration = video.duration
video.close()
audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=".wav").name
command = [
"ffmpeg", "-i", video_path,
"-t", str(min(duration, max_duration)),
"-ar", "16000", "-ac", "1",
"-acodec", "pcm_s16le", "-y", audio_path
]
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
st.error("ffmpeg audio extraction failed.")
os.remove(audio_path) # Clean up failed temp file
st.code(result.stderr.decode())
return None
return audio_path
except Exception as e:
st.error(f"Error trimming video: {e}")
os.remove(audio_path)
st.code(traceback.format_exc())
return None
finally:
# Clean up input video if it was a temp file
if "tmp" in video_path and os.path.exists(video_path):
try:
os.remove(video_path)
except Exception:
pass # Avoid crashing on cleanup
|