File size: 3,209 Bytes
52930d0
ce3a3a9
b2e37a3
ee8c3b8
 
7b1110c
5de25ba
ee8c3b8
 
b2e37a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee8c3b8
 
b2e37a3
 
7b1110c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2e37a3
 
 
ee8c3b8
 
b2e37a3
 
 
 
 
 
 
ee8c3b8
 
 
 
 
b2e37a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee8c3b8
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streamlit as st
from pytube import YouTube
from moviepy.editor import AudioFileClip
import speech_recognition as sr
from transformers import pipeline
from pydub import AudioSegment, silence
import os

def download_and_extract_audio(youtube_link):
    try:
        yt = YouTube(youtube_link)
        video = yt.streams.filter(only_audio=True).first()
        out_file = video.download(output_path=".")
        base, ext = os.path.splitext(out_file)
        new_file = base + '.mp3'
        os.rename(out_file, new_file)

        # Convert MP3 to WAV using moviepy
        audio_clip = AudioFileClip(new_file)
        wav_file = base + '.wav'
        audio_clip.write_audiofile(wav_file, codec='pcm_s16le')  # codec for WAV format
        audio_clip.close()
        return wav_file
    except Exception as e:
        st.error(f"Error downloading and extracting audio: {e}")
        return None

def transcribe_audio(audio_path):
    try:
        recognizer = sr.Recognizer()
        audio = AudioSegment.from_wav(audio_path)
        
        # Split the audio into non-silent chunks
        audio_chunks = silence.split_on_silence(audio, min_silence_len=1000, silence_thresh=-40)

        full_text = ""
        for i, chunk in enumerate(audio_chunks):
            chunk.export(f"/tmp/chunk{i}.wav", format="wav")
            with sr.AudioFile(f"/tmp/chunk{i}.wav") as source:
                audio_data = recognizer.record(source)
                try:
                    text = recognizer.recognize_google(audio_data)
                    full_text += " " + text
                except sr.UnknownValueError:
                    # Skip segments that the recognizer can't understand
                    continue

        return full_text.strip()
    except Exception as e:
        st.error(f"Error transcribing audio: {e}")
        return None

def summarize_text(text):
    try:
        summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
        summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
        return summary[0]['summary_text']
    except Exception as e:
        st.error(f"Error summarizing text: {e}")
        return None

def main():
    st.title("YouTube Video Summary")
    youtube_link = st.text_input("Enter YouTube Video Link:")
    if st.button("Summarize"):
        if youtube_link:
            with st.spinner('Downloading and extracting audio...'):
                audio_path = download_and_extract_audio(youtube_link)
                if audio_path is None:
                    return

            with st.spinner('Transcribing audio to text...'):
                transcription = transcribe_audio(audio_path)
                if transcription is None:
                    return

            with st.spinner('Summarizing transcription...'):
                summary = summarize_text(transcription)
                if summary is None:
                    return
            
            st.subheader("Transcription:")
            st.write(transcription)
            st.subheader("Summary:")
            st.write(summary)
        else:
            st.error("Please enter a valid YouTube video link.")

if __name__ == '__main__':
    main()