-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyt2text.php
More file actions
58 lines (46 loc) · 1.7 KB
/
Copy pathyt2text.php
File metadata and controls
58 lines (46 loc) · 1.7 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
<?php
function yt2text($url,$model){
// --- CONFIG ---
$videoUrl = $url; // Replace with your YouTube URL
$tmpAudio = 'audio_tmp.m4a'; // Temporary file
$finalMp3 = 'audio_for_whisper.mp3'; // Output for Whisper
$whisperModel = 'large-v3-turbo'; // Whisper model
$language = 'de'; // Language code
// Step 0: Unload the LM Studio models before transcription to free gpu ram
exec("lms unload --all", $output, $returnCode);
if ($returnCode !== 0) {
echo "Failed to unload model: " . implode("\n", $output) . "\n";
}
// Step 1: Download best audio from YouTube
exec("yt-dlp -f bestaudio -o \"$tmpAudio\" \"$videoUrl\"");
// Step 2: Convert to low-bitrate mono MP3
exec("ffmpeg -y -i \"$tmpAudio\" -ab 32k -ac 1 \"$finalMp3\"");
// Step 3: Delete temp file
unlink($tmpAudio);
// Step 4: Transcribe with Whisper (only text output, no timestamps)
$whisperCmd = "whisper \"$finalMp3\" --model $whisperModel --language $language --output_format txt 2>&1";
exec($whisperCmd, $output, $returnCode);
$transcript = '';
if ($returnCode === 0) {
// Read the generated TXT file
$txtFile = pathinfo($finalMp3, PATHINFO_FILENAME) . '.txt';
if (file_exists($txtFile)) {
$transcript = file_get_contents($txtFile);
// Clean up files
unlink($finalMp3);
unlink($txtFile);
} else {
$transcript = "Error: Transcript file not found";
}
} else {
$transcript = "Whisper Error (Code $returnCode):\n" . implode("\n", $output);
}
// Step 5: Load the LM Studio model back after transcription
exec("lms load " . $model, $output, $returnCode);
if ($returnCode !== 0) {
echo "Failed to load model: " . implode("\n", $output) . "\n";
}
echo "\nProcess completed!\n";
return $transcript;
}
?>