File size: 2,026 Bytes
84b0762
fc6bc0e
84b0762
fc6bc0e
84b0762
fc6bc0e
84b0762
fc6bc0e
 
 
 
 
 
 
 
435f259
fc6bc0e
 
84b0762
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
import streamlit as st
import ffmpeg
import os
from transformers import pipeline
import tempfile

# Define transcription function
def extract_audio(video_path, output_audio_path):
    """Extract audio from a video file."""
    ffmpeg.input(video_path).output(output_audio_path, format="mp3", ac=1, ar="16000").run(overwrite_output=True)
    return output_audio_path

def transcribe_audio(audio_path):
    """Transcribe audio using OpenAI Whisper."""
    asr_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-base")
    transcription = asr_pipeline(audio_path, return_timestamps=True)
    return transcription["text"]

# Streamlit UI
st.title("Video-to-Text Transcription App")
st.write("Upload a video file to transcribe its audio content into text.")

# File upload
uploaded_file = st.file_uploader("Upload your video file (e.g., .mp4, .mov, etc.)", type=["mp4", "mov", "avi", "mkv"])

if uploaded_file is not None:
    with st.spinner("Processing video..."):
        # Save uploaded file to a temporary directory
        with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
            temp_video.write(uploaded_file.read())
            video_path = temp_video.name

        # Extract audio
        audio_path = os.path.join(tempfile.gettempdir(), "extracted_audio.mp3")
        extract_audio(video_path, audio_path)

        # Transcribe audio
        transcription = transcribe_audio(audio_path)

        # Display transcription
        st.subheader("Transcription")
        st.text_area("Transcribed Text", transcription, height=300)

        # Save transcription to file
        output_file = "transcription.txt"
        with open(output_file, "w") as f:
            f.write(transcription)

        # Download transcription
        with open(output_file, "rb") as file:
            st.download_button(
                label="Download Transcription",
                data=file,
                file_name="transcription.txt",
                mime="text/plain"
            )