mgbam's picture
Update app.py
21479b0 verified
raw
history blame
3.81 kB
import streamlit as st
import os
import subprocess
def main():
st.title("Smart Edit Assistant")
uploaded_file = st.file_uploader("Upload your video", type=["mp4", "mov", "mkv"])
if uploaded_file:
with open("temp_input.mp4", "wb") as f:
f.write(uploaded_file.getbuffer())
st.video("temp_input.mp4")
if st.button("Process Video"):
# 1. Extract audio using FFmpeg (example)
with st.spinner("Extracting audio..."):
audio_path = extract_audio_ffmpeg("temp_input.mp4", "temp_audio.wav")
# 2. Transcribe audio (placeholder function; use openai-whisper or local)
with st.spinner("Transcribing..."):
transcript_text = transcribe_audio(audio_path)
st.text_area("Transcript", transcript_text, height=200)
# 3. Generate instructions (placeholder function; calls GPT or open-source LLM)
with st.spinner("Generating edit instructions..."):
edit_instructions = generate_edit_instructions(transcript_text)
st.write("AI Edit Instructions:", edit_instructions)
# 4. Apply Edits with FFmpeg
with st.spinner("Applying edits..."):
edited_video_path = apply_edits("temp_input.mp4", edit_instructions)
# 5. Verify output file
abs_edited_path = os.path.join(os.getcwd(), edited_video_path)
if not os.path.exists(abs_edited_path):
st.error(f"Edited video file not found at '{abs_edited_path}'. Check logs.")
return
file_size = os.path.getsize(abs_edited_path)
if file_size == 0:
st.error(f"Edited video file is empty (0 bytes). Check ffmpeg or editing logic.")
return
st.success("Edit complete! Now previewing the edited video.")
st.video(abs_edited_path) # pass the absolute path
with open(abs_edited_path, "rb") as f_out:
st.download_button(
label="Download Edited Video",
data=f_out,
file_name="edited_result.mp4",
mime="video/mp4",
)
def extract_audio_ffmpeg(input_video, output_audio):
"""
Calls ffmpeg to extract audio. Returns path if successful; raises if not.
"""
cmd = [
"ffmpeg", "-y",
"-i", input_video,
"-vn", # no video
"-acodec", "pcm_s16le",
"-ar", "16000",
"-ac", "1",
output_audio
]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
raise RuntimeError(f"ffmpeg error: {result.stderr.decode()}")
return output_audio
def transcribe_audio(audio_path):
"""
Placeholder for your transcription logic (whisper / openai-whisper).
"""
return "This is a mock transcript."
def generate_edit_instructions(transcript_text):
"""
Placeholder for GPT/LLM-based instructions.
Return string or structured instructions (like JSON).
"""
return "Keep everything; no major edits."
def apply_edits(input_video, edit_instructions):
"""
Demo function: We'll just copy input to output to show a valid flow.
In practice, you'd parse instructions & cut the video with ffmpeg or moviepy.
"""
output_video = "edited_video.mp4"
# For demonstration, let's do a direct copy with ffmpeg:
cmd = ["ffmpeg", "-y", "-i", input_video, "-c", "copy", output_video]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
raise RuntimeError(f"ffmpeg editing error: {result.stderr.decode()}")
return output_video
if __name__ == "__main__":
main()