Add first edition of AndyTranscribe.

This commit is contained in:
ben
2026-08-12 13:59:33 +02:00
parent 9c8461405f
commit 771658040b
100 changed files with 18039 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
<?php
namespace App\Services;
use Carbon\Carbon;
use getID3;
class Mp3MetadataService
{
/**
* Extract ID3 and audio metadata from an MP3 file on disk.
*
* @return array{
* title: ?string,
* artist: ?string,
* album: ?string,
* duration_seconds: ?int,
* recorded_at: ?string
* }
*/
public function extract(string $absolutePath): array
{
$analyzer = new getID3;
$info = $analyzer->analyze($absolutePath);
$tags = [];
if (isset($info['tags']['id3v2'])) {
$tags = $info['tags']['id3v2'];
} elseif (isset($info['tags']['id3v1'])) {
$tags = $info['tags']['id3v1'];
}
$title = $this->firstTag($tags, 'title');
$artist = $this->firstTag($tags, 'artist');
$album = $this->firstTag($tags, 'album');
$year = $this->firstTag($tags, 'year') ?? $this->firstTag($tags, 'recording_time');
$duration = isset($info['playtime_seconds'])
? (int) round((float) $info['playtime_seconds'])
: null;
$recordedAt = null;
if (filled($year) && preg_match('/^\d{4}/', $year)) {
try {
$recordedAt = Carbon::parse($year)->toDateTimeString();
} catch (\Throwable) {
$recordedAt = null;
}
}
return [
'title' => $title,
'artist' => $artist,
'album' => $album,
'duration_seconds' => $duration,
'recorded_at' => $recordedAt,
];
}
/**
* @param array<string, mixed> $tags
*/
private function firstTag(array $tags, string $key): ?string
{
if (! isset($tags[$key])) {
return null;
}
$value = $tags[$key];
if (is_array($value)) {
$value = $value[0] ?? null;
}
return filled($value) ? (string) $value : null;
}
}
+102
View File
@@ -0,0 +1,102 @@
<?php
namespace App\Services;
use App\Models\Recording;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Laravel\Ai\Transcription;
use RuntimeException;
class TranscriptionService
{
/**
* Run transcription for a recording using the selected driver.
*/
public function transcribe(Recording $recording): string
{
return match ($recording->transcription_driver) {
'cloud' => $this->viaCloud($recording),
'local' => $this->viaLocal($recording),
'ollama' => $this->viaRemoteCompatible($recording),
default => throw new RuntimeException('Unknown transcription driver: '.$recording->transcription_driver),
};
}
private function viaCloud(Recording $recording): string
{
$transcript = Transcription::fromStorage($recording->file_path)
->timeout((int) config('ai.transcription_timeout', 600))
->generate('openai', 'whisper-1');
return (string) $transcript;
}
private function viaLocal(Recording $recording): string
{
$model = config('ai.local_whisper_model', 'Systran/faster-whisper-base');
$transcript = Transcription::fromStorage($recording->file_path)
->timeout((int) config('ai.transcription_timeout', 600))
->generate('local-whisper', $model);
return (string) $transcript;
}
/**
* Call an OpenAI-compatible /v1/audio/transcriptions endpoint at a user-supplied host URL.
*/
private function viaRemoteCompatible(Recording $recording): string
{
if (! filled($recording->ollama_url)) {
throw new RuntimeException('Ollama host URL is required for remote transcription.');
}
$base = $this->normalizeBaseUrl($recording->ollama_url);
$model = config('ai.remote_whisper_model', config('ai.local_whisper_model', 'Systran/faster-whisper-base'));
$path = $recording->absolutePath();
if (! is_readable($path)) {
throw new RuntimeException('Recording audio file is not readable.');
}
$response = Http::timeout((int) config('ai.transcription_timeout', 600))
->attach(
'file',
fopen($path, 'r'),
$recording->original_filename ?: basename($path),
)
->post($base.'/audio/transcriptions', [
'model' => $model,
'response_format' => 'json',
]);
if (! $response->successful()) {
throw new RuntimeException(
'Remote transcription failed (HTTP '.$response->status().'): '.$response->body()
);
}
$text = $response->json('text');
if (! is_string($text) || $text === '') {
throw new RuntimeException('Remote transcription returned an empty transcript. Ensure the host exposes OpenAI-compatible /v1/audio/transcriptions.');
}
return $text;
}
/**
* Normalize a user URL to an OpenAI-style base ending in /v1.
*/
private function normalizeBaseUrl(string $url): string
{
$url = rtrim(trim($url), '/');
if (Str::endsWith($url, '/v1')) {
return $url;
}
return $url.'/v1';
}
}