mgbam commited on
Commit
896af9f
·
verified ·
1 Parent(s): b4f2c32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -15
app.py CHANGED
@@ -5,34 +5,38 @@ import subprocess
5
  def main():
6
  st.title("Smart Edit Assistant")
7
 
 
8
  uploaded_file = st.file_uploader("Upload your video", type=["mp4", "mov", "mkv"])
9
 
10
  if uploaded_file:
 
11
  with open("temp_input.mp4", "wb") as f:
12
  f.write(uploaded_file.getbuffer())
13
 
 
14
  st.video("temp_input.mp4")
15
 
 
16
  if st.button("Process Video"):
17
- # 1. Extract audio using FFmpeg (example)
18
  with st.spinner("Extracting audio..."):
19
  audio_path = extract_audio_ffmpeg("temp_input.mp4", "temp_audio.wav")
20
 
21
- # 2. Transcribe audio (placeholder function; use openai-whisper or local)
22
  with st.spinner("Transcribing..."):
23
  transcript_text = transcribe_audio(audio_path)
24
  st.text_area("Transcript", transcript_text, height=200)
25
 
26
- # 3. Generate instructions (placeholder function; calls GPT or open-source LLM)
27
  with st.spinner("Generating edit instructions..."):
28
  edit_instructions = generate_edit_instructions(transcript_text)
29
  st.write("AI Edit Instructions:", edit_instructions)
30
 
31
- # 4. Apply Edits with FFmpeg
32
  with st.spinner("Applying edits..."):
33
  edited_video_path = apply_edits("temp_input.mp4", edit_instructions)
34
 
35
- # 5. Verify output file
36
  abs_edited_path = os.path.join(os.getcwd(), edited_video_path)
37
  if not os.path.exists(abs_edited_path):
38
  st.error(f"Edited video file not found at '{abs_edited_path}'. Check logs.")
@@ -40,12 +44,13 @@ def main():
40
 
41
  file_size = os.path.getsize(abs_edited_path)
42
  if file_size == 0:
43
- st.error(f"Edited video file is empty (0 bytes). Check ffmpeg or editing logic.")
44
  return
45
 
46
  st.success("Edit complete! Now previewing the edited video.")
47
- st.video(abs_edited_path) # pass the absolute path
48
 
 
49
  with open(abs_edited_path, "rb") as f_out:
50
  st.download_button(
51
  label="Download Edited Video",
@@ -54,14 +59,15 @@ def main():
54
  mime="video/mp4",
55
  )
56
 
 
57
  def extract_audio_ffmpeg(input_video, output_audio):
58
  """
59
- Calls ffmpeg to extract audio. Returns path if successful; raises if not.
60
  """
61
  cmd = [
62
  "ffmpeg", "-y",
63
  "-i", input_video,
64
- "-vn", # no video
65
  "-acodec", "pcm_s16le",
66
  "-ar", "16000",
67
  "-ac", "1",
@@ -72,31 +78,36 @@ def extract_audio_ffmpeg(input_video, output_audio):
72
  raise RuntimeError(f"ffmpeg error: {result.stderr.decode()}")
73
  return output_audio
74
 
 
75
  def transcribe_audio(audio_path):
76
  """
77
- Placeholder for your transcription logic (whisper / openai-whisper).
 
78
  """
79
  return "This is a mock transcript."
80
 
 
81
  def generate_edit_instructions(transcript_text):
82
  """
83
- Placeholder for GPT/LLM-based instructions.
84
- Return string or structured instructions (like JSON).
85
  """
86
  return "Keep everything; no major edits."
87
 
 
88
  def apply_edits(input_video, edit_instructions):
89
  """
90
- Demo function: We'll just copy input to output to show a valid flow.
91
- In practice, you'd parse instructions & cut the video with ffmpeg or moviepy.
 
92
  """
93
  output_video = "edited_video.mp4"
94
- # For demonstration, let's do a direct copy with ffmpeg:
95
  cmd = ["ffmpeg", "-y", "-i", input_video, "-c", "copy", output_video]
96
  result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
97
  if result.returncode != 0:
98
  raise RuntimeError(f"ffmpeg editing error: {result.stderr.decode()}")
99
  return output_video
100
 
 
101
  if __name__ == "__main__":
102
  main()
 
5
  def main():
6
  st.title("Smart Edit Assistant")
7
 
8
+ # 1. Upload a video
9
  uploaded_file = st.file_uploader("Upload your video", type=["mp4", "mov", "mkv"])
10
 
11
  if uploaded_file:
12
+ # Save the uploaded file to temp_input.mp4
13
  with open("temp_input.mp4", "wb") as f:
14
  f.write(uploaded_file.getbuffer())
15
 
16
+ # Display the original video
17
  st.video("temp_input.mp4")
18
 
19
+ # Button to process the video
20
  if st.button("Process Video"):
21
+ # Extract audio using FFmpeg
22
  with st.spinner("Extracting audio..."):
23
  audio_path = extract_audio_ffmpeg("temp_input.mp4", "temp_audio.wav")
24
 
25
+ # Transcribe audio (placeholder)
26
  with st.spinner("Transcribing..."):
27
  transcript_text = transcribe_audio(audio_path)
28
  st.text_area("Transcript", transcript_text, height=200)
29
 
30
+ # Generate editing instructions (placeholder)
31
  with st.spinner("Generating edit instructions..."):
32
  edit_instructions = generate_edit_instructions(transcript_text)
33
  st.write("AI Edit Instructions:", edit_instructions)
34
 
35
+ # Apply edits (placeholder) with FFmpeg
36
  with st.spinner("Applying edits..."):
37
  edited_video_path = apply_edits("temp_input.mp4", edit_instructions)
38
 
39
+ # Verify the edited video file
40
  abs_edited_path = os.path.join(os.getcwd(), edited_video_path)
41
  if not os.path.exists(abs_edited_path):
42
  st.error(f"Edited video file not found at '{abs_edited_path}'. Check logs.")
 
44
 
45
  file_size = os.path.getsize(abs_edited_path)
46
  if file_size == 0:
47
+ st.error("Edited video file is empty (0 bytes). Check ffmpeg or editing logic.")
48
  return
49
 
50
  st.success("Edit complete! Now previewing the edited video.")
51
+ st.video(abs_edited_path)
52
 
53
+ # Provide a download button for the edited file
54
  with open(abs_edited_path, "rb") as f_out:
55
  st.download_button(
56
  label="Download Edited Video",
 
59
  mime="video/mp4",
60
  )
61
 
62
+
63
  def extract_audio_ffmpeg(input_video, output_audio):
64
  """
65
+ Calls ffmpeg to extract audio. Returns the output_audio path if successful; raises if not.
66
  """
67
  cmd = [
68
  "ffmpeg", "-y",
69
  "-i", input_video,
70
+ "-vn", # Strip out the video
71
  "-acodec", "pcm_s16le",
72
  "-ar", "16000",
73
  "-ac", "1",
 
78
  raise RuntimeError(f"ffmpeg error: {result.stderr.decode()}")
79
  return output_audio
80
 
81
+
82
  def transcribe_audio(audio_path):
83
  """
84
+ Placeholder for your transcription logic (local Whisper, OpenAI Whisper API, etc.).
85
+ Here, we simply return a mock transcript for demonstration.
86
  """
87
  return "This is a mock transcript."
88
 
89
+
90
  def generate_edit_instructions(transcript_text):
91
  """
92
+ Placeholder for GPT or other LLM-based logic.
93
+ Return instructions (text, JSON, etc.) detailing how to edit the video.
94
  """
95
  return "Keep everything; no major edits."
96
 
97
+
98
  def apply_edits(input_video, edit_instructions):
99
  """
100
+ A simple placeholder function. In practice, you'd parse instructions
101
+ and run ffmpeg/moviepy to cut or reassemble the video.
102
+ Here, we just do a direct copy with ffmpeg to simulate a final step.
103
  """
104
  output_video = "edited_video.mp4"
 
105
  cmd = ["ffmpeg", "-y", "-i", input_video, "-c", "copy", output_video]
106
  result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
107
  if result.returncode != 0:
108
  raise RuntimeError(f"ffmpeg editing error: {result.stderr.decode()}")
109
  return output_video
110
 
111
+
112
  if __name__ == "__main__":
113
  main()