|
import gradio as gr |
|
import pandas as pd |
|
import yt_dlp |
|
import os |
|
from transcriber import gen_csv() |
|
df = pd.read_csv("./final.csv") |
|
transcripts = df.to_dict(orient='records') |
|
|
|
|
|
def download_video(youtube_url): |
|
|
|
ydl_opts = { |
|
'format': 'mp4', |
|
'outtmpl': 'downloaded_video.%(ext)s', |
|
'quiet': True |
|
} |
|
|
|
|
|
with yt_dlp.YoutubeDL({'quiet': True}) as ydl: |
|
info_dict = ydl.extract_info(youtube_url, download=False) |
|
video_ext = info_dict.get('ext') |
|
video_path = f'downloaded_video.mp4' |
|
|
|
|
|
if not os.path.exists(video_path): |
|
|
|
with yt_dlp.YoutubeDL(ydl_opts) as ydl: |
|
ydl.download([youtube_url]) |
|
|
|
|
|
transcript_html = "" |
|
for t in transcripts: |
|
transcript_html += f'<div class="transcript-block"><a href="#" onclick="var video = document.getElementById(\'video-player\').querySelector(\'video\'); video.currentTime={t["start_time"]}; return false;">' \ |
|
f'[{t["start_time"]:.2f} - {t["end_time"]:.2f}]<br>{t["text"]}</a></div>' |
|
|
|
return video_path, transcript_html |
|
|
|
|
|
def search_transcript(keyword): |
|
search_results = "" |
|
for t in transcripts: |
|
if keyword.lower() in t['text'].lower(): |
|
search_results += f'<div class="transcript-block"><a href="#" onclick="var video = document.getElementById(\'video-player\').querySelector(\'video\'); video.currentTime={t["start_time"]}; return false;">' \ |
|
f'[{t["start_time"]:.2f} - {t["end_time"]:.2f}]<br>{t["text"]}</a></div>' |
|
return search_results |
|
|
|
|
|
css = """ |
|
.fixed-video { width: 480px !important; height: 270px !important; } |
|
.fixed-transcript { width: 480px !important; height: 270px !important; overflow-y: auto; } |
|
.transcript-block { margin: 10px 0; padding: 10px; border: 1px solid #ddd; border-radius: 5px; background-color: #f9f9f9; } |
|
.transcript-block a { text-decoration: none; color: #007bff; } |
|
.transcript-block a:hover { text-decoration: underline; } |
|
""" |
|
|
|
|
|
with gr.Blocks(css=css) as demo: |
|
gr.Markdown("# YouTube Video Player with Clickable Transcript") |
|
|
|
with gr.Row(): |
|
youtube_url = gr.Textbox(label="YouTube URL", placeholder="Enter YouTube video link here") |
|
download_button = gr.Button("Download and Display Transcript") |
|
|
|
with gr.Row(): |
|
video = gr.Video(label="Video Player", elem_id="video-player", elem_classes="fixed-video") |
|
transcript_display = gr.HTML(label="Transcript", elem_classes="fixed-transcript") |
|
|
|
with gr.Row(): |
|
search_box = gr.Textbox(label="Search Transcript", placeholder="Enter keyword to search") |
|
search_button = gr.Button("Search") |
|
search_results_display = gr.HTML(label="Search Results", elem_classes="fixed-transcript") |
|
|
|
|
|
def display_transcript(youtube_url): |
|
video_path, transcript_html = download_video(youtube_url) |
|
|
|
return video_path, transcript_html |
|
|
|
download_button.click(fn=display_transcript, inputs=youtube_url, outputs=[video, transcript_display]) |
|
|
|
|
|
search_button.click(fn=search_transcript, inputs=search_box, outputs=search_results_display) |
|
|
|
|
|
demo.launch() |