Repository navigation
Expand file tree
/
Copy pathvideo_transcriber.py
More file actions
executable file
·862 lines (706 loc) · 32.6 KB
/
Copy pathvideo_transcriber.py
File metadata and controls
executable file
·862 lines (706 loc) · 32.6 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
#!/usr/bin/env python3
"""
Video Transcription Tool - Local Whisper Edition
Transcribes mp4 video files using local Whisper AI and creates Google Doc transcripts.
Uses zero-cost local processing with OpenAI's Whisper model.
Usage:
python video_transcriber.py /path/to/videos --model base
python video_transcriber.py /path/to/videos --model medium --recursive
"""
import os
import sys
import argparse
import pickle
from pathlib import Path
from datetime import datetime
from typing import List, Dict, Optional
import whisper
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from rich.console import Console
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn, TimeElapsedColumn
from rich.table import Table
from rich.panel import Panel
try:
from pyannote.audio import Pipeline
PYANNOTE_AVAILABLE = True
except ImportError:
PYANNOTE_AVAILABLE = False
console = Console()
class VideoTranscriber:
"""Transcribes videos using local Whisper and creates Google Docs"""
def __init__(self, model_size="base", credentials_path="token.pickle", hf_token=None):
"""
Initialize the transcriber
Args:
model_size: Whisper model size (tiny, base, small, medium, large)
credentials_path: Path to Google OAuth credentials
hf_token: Hugging Face token for speaker diarization (optional)
"""
self.model_size = model_size
self.model = None
self.diarization_pipeline = None
self.docs_service = None
self.drive_service = None
self.calendar_service = None
self.credentials_path = credentials_path
self.hf_token = hf_token or os.environ.get('HF_TOKEN')
def load_model(self):
"""Load the Whisper model and optionally the diarization pipeline"""
console.print(f"\n[cyan]Loading Whisper model ({self.model_size})...[/cyan]")
console.print("[yellow]Note: First run will download the model (~100-3000MB depending on size)[/yellow]")
self.model = whisper.load_model(self.model_size)
console.print("[green]✓ Whisper model loaded successfully![/green]")
# Load speaker diarization if HF token is available
if self.hf_token and PYANNOTE_AVAILABLE:
console.print("\n[cyan]Loading speaker diarization model...[/cyan]")
console.print("[yellow]Note: First run will download the diarization model (~300MB)[/yellow]")
try:
self.diarization_pipeline = Pipeline.from_pretrained(
"pyannote/speaker-diarization-3.1",
use_auth_token=self.hf_token
)
console.print("[green]✓ Speaker diarization enabled![/green]")
except Exception as e:
console.print(f"[yellow]Warning: Could not load diarization model: {e}[/yellow]")
console.print("[yellow]Falling back to pause-based speaker detection[/yellow]")
self.diarization_pipeline = None
elif not self.hf_token and PYANNOTE_AVAILABLE:
console.print("[yellow]ℹ Speaker diarization disabled (no HF_TOKEN)[/yellow]")
console.print("[yellow] Set HF_TOKEN environment variable or use --hf-token flag for better speaker identification[/yellow]")
console.print()
def init_google_services(self):
"""Initialize Google Docs and Drive APIs using existing credentials"""
# Try multiple locations for credentials
possible_paths = [
self.credentials_path,
os.path.join(os.path.dirname(__file__), 'token.pickle'),
os.path.expanduser('~/Development/Scripts/Blog idea generator/token.pickle'),
]
creds_path = None
for path in possible_paths:
if os.path.exists(path):
creds_path = path
console.print(f"[cyan]Using credentials from: {path}[/cyan]")
break
if not creds_path:
console.print(f"[red]Error: Could not find credentials file.[/red]")
console.print("[yellow]Tried locations:[/yellow]")
for path in possible_paths:
console.print(f" - {path}")
console.print("\n[yellow]Please run the Qwilo cli.py first to authenticate with Google,[/yellow]")
console.print("[yellow]or specify credentials path with --credentials flag.[/yellow]")
sys.exit(1)
with open(creds_path, 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
with open(creds_path, 'wb') as token:
pickle.dump(creds, token)
self.docs_service = build('docs', 'v1', credentials=creds)
self.drive_service = build('drive', 'v3', credentials=creds)
self.calendar_service = build('calendar', 'v3', credentials=creds)
console.print("[green]✓ Connected to Google services (Docs, Drive, Calendar)![/green]\n")
def parse_zoom_folder_datetime(self, video_path: str) -> datetime:
"""
Parse date/time from Zoom folder name
Format: "2025-07-02 13.00.28 Meeting Name/video.mp4"
Args:
video_path: Full path to video file
Returns:
datetime object or None if parsing fails
"""
try:
# Get parent folder name
folder_name = Path(video_path).parent.name
# Extract date/time part (first 19 characters: "2025-07-02 13.00.28")
datetime_str = folder_name[:19]
# Parse: YYYY-MM-DD HH.MM.SS
return datetime.strptime(datetime_str, '%Y-%m-%d %H.%M.%S')
except (ValueError, IndexError) as e:
console.print(f"[yellow]Could not parse date from folder: {folder_name}[/yellow]")
return None
def get_meeting_name_from_folder(self, video_path: str) -> str:
"""
Extract meeting name from Zoom folder, removing the date/time prefix
Format: "2025-07-02 13.00.28 Meeting Name" -> "Meeting Name"
Args:
video_path: Full path to video file
Returns:
Meeting name from folder, or filename if parsing fails
"""
try:
# Get parent folder name
folder_name = Path(video_path).parent.name
# Remove date/time prefix (first 20 characters: "2025-07-02 13.00.28 ")
if len(folder_name) > 20:
meeting_name = folder_name[20:].strip()
if meeting_name:
return meeting_name
# Fallback to folder name if no meeting name after prefix
return folder_name
except Exception as e:
# Ultimate fallback to video filename
return Path(video_path).stem
def find_calendar_event(self, meeting_datetime: datetime) -> Dict:
"""
Find calendar event matching the meeting time
Args:
meeting_datetime: DateTime of the Zoom recording
Returns:
Dict with 'title' and 'attendees' or None
"""
if not meeting_datetime:
return None
try:
# Search window: 30 minutes before and after
from datetime import timedelta
# Convert meeting_datetime (naive local) to UTC by using timestamp
meeting_utc = datetime.utcfromtimestamp(meeting_datetime.timestamp())
time_min = (meeting_utc - timedelta(minutes=30)).isoformat() + 'Z'
time_max = (meeting_utc + timedelta(minutes=30)).isoformat() + 'Z'
# Query calendar
events_result = self.calendar_service.events().list(
calendarId='primary',
timeMin=time_min,
timeMax=time_max,
maxResults=10,
singleEvents=True,
orderBy='startTime'
).execute()
events = events_result.get('items', [])
if not events:
return None
# Find closest event
closest_event = None
min_diff = float('inf')
for event in events:
event_start = event.get('start', {}).get('dateTime')
if not event_start:
continue
# Parse event time and convert to local time for comparison
event_time = datetime.fromisoformat(event_start.replace('Z', '+00:00'))
# Convert to local time by getting the UTC offset from the event
# and converting meeting_datetime (which is naive local) to UTC for comparison
event_time_utc = event_time.timestamp()
meeting_time_utc = meeting_datetime.timestamp()
diff = abs(event_time_utc - meeting_time_utc)
if diff < min_diff:
min_diff = diff
closest_event = event
if not closest_event:
return None
# Extract attendees
attendees = []
for attendee in closest_event.get('attendees', []):
email = attendee.get('email', '')
name = attendee.get('displayName') or email.split('@')[0]
# Skip resource calendars
if 'resource.calendar.google.com' not in email:
attendees.append(name)
# Include organizer
organizer = closest_event.get('organizer', {})
organizer_email = organizer.get('email', '')
organizer_name = organizer.get('displayName') or organizer_email.split('@')[0]
if organizer_name and organizer_name not in attendees:
attendees.insert(0, organizer_name)
return {
'title': closest_event.get('summary', 'Untitled Meeting'),
'attendees': attendees,
'event_time': closest_event.get('start', {}).get('dateTime')
}
except Exception as e:
console.print(f"[yellow]Calendar lookup failed: {e}[/yellow]")
return None
def find_videos(self, root_folder: str, recursive: bool = True, since_date: datetime = None, until_date: datetime = None, min_size_mb: float = 5.0) -> List[str]:
"""
Find all mp4 files in the folder
Args:
root_folder: Root directory to search
recursive: Search subdirectories recursively
since_date: Only include videos modified after this date (optional)
until_date: Only include videos modified before this date (optional)
min_size_mb: Minimum file size in MB (default: 5.0)
Returns:
List of video file paths
"""
pattern = "**/*.mp4" if recursive else "*.mp4"
videos = list(Path(root_folder).glob(pattern))
filtered_videos = []
for video in videos:
# Get file size in MB
size_mb = Path(video).stat().st_size / (1024 * 1024)
# Skip files under minimum size
if size_mb < min_size_mb:
continue
# Filter by modification date if specified
mod_time = datetime.fromtimestamp(Path(video).stat().st_mtime)
if since_date and mod_time < since_date:
continue
if until_date and mod_time > until_date:
continue
filtered_videos.append(str(video))
return filtered_videos
def transcribe_video(self, video_path: str) -> Dict:
"""
Transcribe a video file using Whisper
Args:
video_path: Path to the video file
Returns:
Dictionary with transcription results
"""
result = self.model.transcribe(
video_path,
verbose=False,
language="en" # Can be None for auto-detection
)
return result
def diarize_video(self, video_path: str) -> Optional[Dict]:
"""
Run speaker diarization on a video file
Args:
video_path: Path to the video file
Returns:
Dictionary mapping time ranges to speaker IDs, or None if diarization unavailable
"""
if not self.diarization_pipeline:
return None
try:
# Run diarization
diarization = self.diarization_pipeline(video_path)
# Convert to dict format: {(start, end): speaker_id}
speaker_segments = {}
for turn, _, speaker in diarization.itertracks(yield_label=True):
speaker_segments[(turn.start, turn.end)] = speaker
return speaker_segments
except Exception as e:
console.print(f"[yellow]Diarization failed: {e}[/yellow]")
return None
def format_transcript_google_meet_style(self, result: Dict, attendees: List[str] = None,
diarization: Optional[Dict] = None) -> str:
"""
Format transcript in Google Meet style (continuous dialogue)
Args:
result: Whisper transcription result
attendees: List of attendee names from calendar (optional)
diarization: Speaker diarization data (optional)
Returns:
Formatted transcript text in Google Meet style
"""
lines = []
# Group segments into speaker turns
segments = result.get('segments', [])
if not segments:
return "No speech detected."
# Use diarization if available
if diarization:
# Map diarization speaker IDs to attendee names
unique_speakers = sorted(set(diarization.values()))
speaker_map = {}
if attendees and len(attendees) >= len(unique_speakers):
# Map speakers to attendees (SPEAKER_00 -> first attendee, etc.)
for i, speaker_id in enumerate(unique_speakers):
speaker_map[speaker_id] = attendees[i] if i < len(attendees) else f"Speaker {i+1}"
else:
# Fallback to numbered speakers
for i, speaker_id in enumerate(unique_speakers):
speaker_map[speaker_id] = f"Speaker {i+1}"
# Assign each segment to a speaker based on diarization
for segment in segments:
text = segment['text'].strip()
segment_mid = (segment['start'] + segment['end']) / 2
# Find which diarization segment this belongs to
speaker_id = None
for (start, end), spk in diarization.items():
if start <= segment_mid <= end:
speaker_id = spk
break
if speaker_id:
speaker_name = speaker_map.get(speaker_id, "Unknown Speaker")
else:
speaker_name = "Unknown Speaker"
lines.append(f"{speaker_name}: {text}")
else:
# Fallback to pause-based heuristic
if attendees and len(attendees) >= 2:
speaker_names = attendees[:2]
else:
speaker_names = ["Speaker 1", "Speaker 2"]
current_speaker_idx = 0
last_end = 0
for i, segment in enumerate(segments):
text = segment['text'].strip()
# Detect speaker change based on pause (>2 seconds)
if i > 0:
pause_duration = segment['start'] - last_end
if pause_duration > 2.0:
current_speaker_idx = 1 if current_speaker_idx == 0 else 0
lines.append(f"{speaker_names[current_speaker_idx]}: {text}")
last_end = segment['end']
return "\n".join(lines)
def _format_timestamp(self, seconds: float) -> str:
"""Convert seconds to HH:MM:SS format (Google Meet style)"""
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
return f"{hours:02d}:{minutes:02d}:{secs:02d}"
def create_google_doc(self, title: str, transcript_result: Dict, video_path: str,
folder_id: str = None, calendar_event: Dict = None,
diarization: Optional[Dict] = None) -> str:
"""
Create a Google Doc with the transcript
Args:
title: Document title (fallback if no calendar event)
transcript_result: Whisper transcription result
video_path: Original video file path
folder_id: Optional Google Drive folder ID
calendar_event: Optional calendar event info with 'title' and 'attendees'
diarization: Optional speaker diarization data
Returns:
Document ID
"""
# Create the document
doc = self.docs_service.documents().create(body={
'title': title
}).execute()
doc_id = doc['documentId']
# Move to folder if specified
if folder_id:
try:
self.drive_service.files().update(
fileId=doc_id,
addParents=folder_id,
fields='id, parents'
).execute()
except HttpError as e:
console.print(f"[yellow]Warning: Could not move to folder: {e}[/yellow]")
# Format the content
video_name = Path(video_path).name
file_size_mb = Path(video_path).stat().st_size / (1024 * 1024)
# Get duration safely
segments = transcript_result.get('segments', [])
if segments:
duration_min = segments[-1].get('end', 0) / 60
else:
duration_min = 0
# Build document content - Google Meet style
# Format: Date, Title - Transcript, Starting timestamp, blank line, then dialogue
# Get video capture date (from Zoom folder name or file modification time)
video_date = self.parse_zoom_folder_datetime(video_path)
if not video_date:
# Fallback to file modification time if can't parse folder name
video_date = datetime.fromtimestamp(Path(video_path).stat().st_mtime)
video_date_str = video_date.strftime('%b %d, %Y') # e.g., "Oct 31, 2025"
# Use calendar event title if available, otherwise use folder name
if calendar_event and calendar_event.get('title'):
meeting_title = calendar_event['title']
else:
# Use folder name (with date/time removed) as fallback
meeting_title = self.get_meeting_name_from_folder(video_path)
doc_title = f"{meeting_title} - Transcript"
# Get attendee names from calendar event (for speaker identification)
attendees = calendar_event.get('attendees', []) if calendar_event else []
content = f"{video_date_str}\n"
content += f"{doc_title}\n"
# Add attendee list if available
if attendees:
attendee_list = ", ".join(attendees)
content += f"Attendees: {attendee_list}\n"
content += "00:00:00\n"
content += " \n" # Blank line (with space to preserve it)
content += self.format_transcript_google_meet_style(transcript_result, attendees, diarization)
# Insert content into document
requests = [{
'insertText': {
'location': {'index': 1},
'text': content
}
}]
self.docs_service.documents().batchUpdate(
documentId=doc_id,
body={'requests': requests}
).execute()
return doc_id
def process_videos(self, root_folder: str, recursive: bool = True,
folder_id: str = None, since_date: datetime = None, until_date: datetime = None) -> List[Dict]:
"""
Process all videos in the folder
Args:
root_folder: Root directory containing videos
recursive: Search subdirectories
folder_id: Optional Google Drive folder ID for output docs
since_date: Only process videos modified after this date (optional)
until_date: Only process videos modified before this date (optional)
Returns:
List of processing results
"""
# Find videos
console.print(f"[cyan]Scanning for videos in: {root_folder}[/cyan]")
if since_date:
console.print(f"[cyan]Filtering videos modified after: {since_date.strftime('%Y-%m-%d %H:%M:%S')}[/cyan]")
if until_date:
console.print(f"[cyan]Filtering videos modified before: {until_date.strftime('%Y-%m-%d %H:%M:%S')}[/cyan]")
videos = self.find_videos(root_folder, recursive, since_date, until_date)
if not videos:
console.print("[yellow]No mp4 files found![/yellow]")
return []
console.print(f"[green]Found {len(videos)} video(s)[/green]\n")
# Display video list
table = Table(title="Videos to Process")
table.add_column("No.", style="cyan", width=4)
table.add_column("Filename", style="magenta")
table.add_column("Size", style="yellow", width=10)
for idx, video_path in enumerate(videos, 1):
size_mb = Path(video_path).stat().st_size / (1024 * 1024)
table.add_row(str(idx), Path(video_path).name, f"{size_mb:.1f} MB")
console.print(table)
console.print()
# Process each video
results = []
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
TimeElapsedColumn(),
console=console
) as progress:
overall_task = progress.add_task(
f"[cyan]Processing {len(videos)} videos...",
total=len(videos)
)
for idx, video_path in enumerate(videos, 1):
video_name = Path(video_path).stem
try:
# Look up calendar event for this video
calendar_event = None
meeting_datetime = self.parse_zoom_folder_datetime(video_path)
if meeting_datetime:
calendar_event = self.find_calendar_event(meeting_datetime)
if calendar_event:
console.print(f"[green]✓ Found calendar event: {calendar_event['title']}[/green]")
else:
console.print(f"[yellow]No calendar event found for {meeting_datetime.strftime('%Y-%m-%d %H:%M')}[/yellow]")
# Update progress
progress.update(
overall_task,
description=f"[cyan]Transcribing ({idx}/{len(videos)}): {Path(video_path).name}"
)
# Transcribe
transcript_result = self.transcribe_video(video_path)
# Run speaker diarization if enabled
diarization = None
if self.diarization_pipeline:
progress.update(
overall_task,
description=f"[cyan]Diarizing ({idx}/{len(videos)}): {Path(video_path).name}"
)
diarization = self.diarize_video(video_path)
if diarization:
num_speakers = len(set(diarization.values()))
console.print(f"[green]✓ Identified {num_speakers} speaker(s)[/green]")
# Create Google Doc
progress.update(
overall_task,
description=f"[cyan]Creating Doc ({idx}/{len(videos)}): {video_name}"
)
# Document title - use calendar event title if available, otherwise folder name
if calendar_event and calendar_event.get('title'):
title = calendar_event['title']
else:
title = self.get_meeting_name_from_folder(video_path)
doc_id = self.create_google_doc(
title,
transcript_result,
video_path,
folder_id,
calendar_event,
diarization
)
doc_url = f"https://docs.google.com/document/d/{doc_id}"
# Get duration safely
segments = transcript_result.get('segments', [])
duration = segments[-1].get('end', 0) if segments else 0
results.append({
'video_path': video_path,
'video_name': video_name,
'doc_id': doc_id,
'doc_url': doc_url,
'status': 'success',
'duration': duration
})
console.print(f"[green]✓ {video_name} → {doc_url}[/green]")
except Exception as e:
console.print(f"[red]✗ Error processing {video_name}: {e}[/red]")
results.append({
'video_path': video_path,
'video_name': video_name,
'status': 'failed',
'error': str(e)
})
progress.update(overall_task, advance=1)
return results
def print_summary(self, results: List[Dict]):
"""Print processing summary"""
successful = [r for r in results if r['status'] == 'success']
failed = [r for r in results if r['status'] == 'failed']
total_duration = sum(r.get('duration', 0) for r in successful) / 60
summary = f"\n[bold cyan]Processing Complete![/bold cyan]\n\n"
summary += f"Total Videos: {len(results)}\n"
summary += f"[green]Successful: {len(successful)}[/green]\n"
summary += f"[red]Failed: {len(failed)}[/red]\n"
summary += f"Total Duration: {total_duration:.1f} minutes\n"
console.print(Panel(summary, title="Summary", border_style="cyan"))
if successful:
console.print("\n[bold green]Successfully Created Documents:[/bold green]")
for r in successful:
console.print(f" • {r['video_name']}: {r['doc_url']}")
if failed:
console.print("\n[bold red]Failed:[/bold red]")
for r in failed:
console.print(f" • {r['video_name']}: {r['error']}")
def main():
parser = argparse.ArgumentParser(
description="Transcribe videos to Google Docs using local Whisper AI",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Model Sizes:
tiny - Fastest, lowest accuracy (~1GB RAM, ~32x realtime)
base - Good balance (~1GB RAM, ~16x realtime) [DEFAULT]
small - Better accuracy (~2GB RAM, ~6x realtime)
medium - High accuracy (~5GB RAM, ~2x realtime)
large - Best accuracy (~10GB RAM, ~1x realtime)
Speaker Diarization (optional):
For accurate speaker identification, get a free Hugging Face token:
1. Create account at https://huggingface.co
2. Accept agreement at https://huggingface.co/pyannote/speaker-diarization-3.1
3. Get token from https://huggingface.co/settings/tokens
4. Use --hf-token flag or set HF_TOKEN environment variable
Examples:
python video_transcriber.py /path/to/videos
python video_transcriber.py /path/to/videos --model medium
python video_transcriber.py /path/to/videos --hf-token hf_xxxxx
python video_transcriber.py /path/to/videos --since 2024-12-01
python video_transcriber.py /path/to/videos --since "2024-12-01 14:30:00"
export HF_TOKEN=hf_xxxxx && python video_transcriber.py /path/to/videos
python video_transcriber.py /path/to/videos --no-recursive
python video_transcriber.py /path/to/videos --folder-id abc123xyz
"""
)
parser.add_argument(
'video_folder',
help='Path to folder containing mp4 videos'
)
parser.add_argument(
'--model',
choices=['tiny', 'base', 'small', 'medium', 'large'],
default='base',
help='Whisper model size (default: base)'
)
parser.add_argument(
'--no-recursive',
action='store_true',
help='Do not search subdirectories'
)
parser.add_argument(
'--folder-id',
default='155R7deqDMsu_YUwi8irlEgix2PBTcnbe',
help='Google Drive folder ID to save documents in (default: 155R7deqDMsu_YUwi8irlEgix2PBTcnbe)'
)
parser.add_argument(
'--credentials',
default='token.pickle',
help='Path to Google credentials file (default: token.pickle)'
)
parser.add_argument(
'--hf-token',
help='Hugging Face token for speaker diarization (can also use HF_TOKEN env variable)'
)
parser.add_argument(
'--since',
help='Only process videos modified after this date (format: YYYY-MM-DD or YYYY-MM-DD HH:MM:SS)'
)
parser.add_argument(
'--until',
help='Only process videos modified before this date (format: YYYY-MM-DD or YYYY-MM-DD HH:MM:SS)'
)
args = parser.parse_args()
# Parse since date if provided
since_date = None
if args.since:
try:
# Try parsing with time first
if ' ' in args.since:
since_date = datetime.strptime(args.since, '%Y-%m-%d %H:%M:%S')
else:
# Parse date only, set time to 00:00:00
since_date = datetime.strptime(args.since, '%Y-%m-%d')
console.print(f"[cyan]Will only process videos modified after: {since_date.strftime('%Y-%m-%d %H:%M:%S')}[/cyan]")
except ValueError:
console.print(f"[red]Error: Invalid date format for --since. Use YYYY-MM-DD or YYYY-MM-DD HH:MM:SS[/red]")
console.print(f"[red]Examples: 2024-12-01 or 2024-12-01 14:30:00[/red]")
sys.exit(1)
# Parse until date if provided
until_date = None
if args.until:
try:
# Try parsing with time first
if ' ' in args.until:
until_date = datetime.strptime(args.until, '%Y-%m-%d %H:%M:%S')
else:
# Parse date only, set time to 23:59:59 to include the entire day
until_date = datetime.strptime(args.until, '%Y-%m-%d')
until_date = until_date.replace(hour=23, minute=59, second=59)
console.print(f"[cyan]Will only process videos modified before: {until_date.strftime('%Y-%m-%d %H:%M:%S')}[/cyan]")
except ValueError:
console.print(f"[red]Error: Invalid date format for --until. Use YYYY-MM-DD or YYYY-MM-DD HH:MM:SS[/red]")
console.print(f"[red]Examples: 2024-12-31 or 2024-12-31 23:59:59[/red]")
sys.exit(1)
# Validate video folder
if not os.path.isdir(args.video_folder):
console.print(f"[red]Error: {args.video_folder} is not a valid directory[/red]")
sys.exit(1)
# Display banner
banner = """
╔══════════════════════════════════════════════════════════╗
║ ║
║ VIDEO TRANSCRIPTION TOOL ║
║ Powered by Local Whisper AI ║
║ ║
╚══════════════════════════════════════════════════════════╝
"""
console.print(banner, style="bold cyan")
# Initialize transcriber
transcriber = VideoTranscriber(
model_size=args.model,
credentials_path=args.credentials,
hf_token=args.hf_token
)
# Load model
transcriber.load_model()
# Initialize Google services
transcriber.init_google_services()
# Process videos
results = transcriber.process_videos(
args.video_folder,
recursive=not args.no_recursive,
folder_id=args.folder_id,
since_date=since_date,
until_date=until_date
)
# Print summary
transcriber.print_summary(results)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
console.print("\n\n[yellow]Interrupted by user[/yellow]")
sys.exit(0)
except Exception as e:
console.print(f"\n[red]Fatal error: {e}[/red]")
import traceback
console.print(traceback.format_exc())
sys.exit(1)