Skip to content

Instantly share code, notes, and snippets.

@danielleevandenbosch
Last active May 30, 2024 13:29
Show Gist options
  • Save danielleevandenbosch/68b5e0daa37c63ba01a202c596bcb193 to your computer and use it in GitHub Desktop.
Save danielleevandenbosch/68b5e0daa37c63ba01a202c596bcb193 to your computer and use it in GitHub Desktop.
This pyton script allows you to create a text file containing the transcript

Installation Instructions for Transcription Script

Prerequisites

Before running the transcription script, ensure you have the following installed on your system:

  • Python 3.10 or later
  • pip (Python package installer)
  • ffmpeg

Step-by-Step Guide

1. Install Python and pip

Ensure Python 3.10 or later and pip are installed on your system. You can download Python from the official Python website.

To verify the installation, run:

python3 --version
pip3 --version
sudo apt update
sudo apt install python3
sudo apt install python3-pip
sudo apt install ffmpeg
pip3 install git+https://github.com/openai/whisper.git
pip3 install ffmpeg-python tqdm
add in a help section to the python script.
add in the ability to add arguments allowing the end user to specify the filename and output name
append the yyyymmdd-hhmmss to the output file.
Integrate this into PHP so the end user experiance is simply dragging a video file into a web page and the script will run on the webpage with progress.
import whisper
import ffmpeg
import os
import time
import threading
def extract_audio(video_file_path, audio_file_path):
ffmpeg.input(video_file_path).output(audio_file_path).run()
def transcribe_video(video_file_path):
audio_file_path = "audio.wav"
extract_audio(video_file_path, audio_file_path)
model = whisper.load_model("base")
# Extract the duration of the audio file
probe = ffmpeg.probe(audio_file_path)
duration = float(probe['format']['duration'])
# Define a function to periodically print progress
def print_progress():
start_time = time.time()
while not transcription_done:
elapsed_time = time.time() - start_time
progress = min(elapsed_time / duration, 1.0) * 100
print(f"Transcription progress: {progress:.2f}%")
time.sleep(5) # Update every 5 seconds
# Start a thread to print progress
transcription_done = False
progress_thread = threading.Thread(target=print_progress)
progress_thread.start()
# Start transcription
print("Starting transcription...")
result = model.transcribe(audio_file_path)
transcription_done = True
progress_thread.join()
print("Transcription completed.")
# Format the transcription with timestamps in hh:mm:ss format
def format_timestamp(seconds):
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
seconds = int(seconds % 60)
return f"{hours:02}:{minutes:02}:{seconds:02}"
formatted_transcript = ""
for segment in result['segments']:
start_time = format_timestamp(segment['start'])
end_time = format_timestamp(segment['end'])
text = segment['text'].strip()
formatted_transcript += f"[{start_time} - {end_time}] {text}\n\n"
return formatted_transcript
# Call the function for each video segment (if split) or single video file
video_files = ["video.mkv"] # List all segment files or use ["video.mkv"] if not split
full_transcription = ""
for i, video_file in enumerate(video_files):
transcription = transcribe_video(video_file)
full_transcription += transcription
with open(f"partial_transcript_{i}.txt", "w") as file:
file.write(transcription)
# Write the full transcription to a file
with open("transcript.txt", "w") as file:
file.write(full_transcription)
print("Transcription completed and saved to transcript.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment