Support common audio formats beyond MP3.
Accept WAV, OGG, FLAC, M4A, and related uploads, and read metadata from non-ID3 tag formats.
This commit is contained in:
@@ -3,9 +3,32 @@
|
|||||||
namespace App\Http\Requests;
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rules\File;
|
||||||
|
|
||||||
class StoreRecordingRequest extends FormRequest
|
class StoreRecordingRequest extends FormRequest
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Popular audio extensions Whisper-compatible providers typically accept.
|
||||||
|
*
|
||||||
|
* @var list<string>
|
||||||
|
*/
|
||||||
|
public const AUDIO_EXTENSIONS = [
|
||||||
|
'mp3',
|
||||||
|
'mpeg',
|
||||||
|
'mpga',
|
||||||
|
'wav',
|
||||||
|
'ogg',
|
||||||
|
'oga',
|
||||||
|
'flac',
|
||||||
|
'm4a',
|
||||||
|
'mp4',
|
||||||
|
'aac',
|
||||||
|
'webm',
|
||||||
|
'wma',
|
||||||
|
'aiff',
|
||||||
|
'aif',
|
||||||
|
];
|
||||||
|
|
||||||
public function authorize(): bool
|
public function authorize(): bool
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
@@ -17,7 +40,10 @@ class StoreRecordingRequest extends FormRequest
|
|||||||
public function rules(): array
|
public function rules(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'audio' => ['required', 'file', 'mimes:mp3,mpeg', 'max:102400'],
|
'audio' => [
|
||||||
|
'required',
|
||||||
|
File::types(self::AUDIO_EXTENSIONS)->max(102400),
|
||||||
|
],
|
||||||
'title' => ['nullable', 'string', 'max:255'],
|
'title' => ['nullable', 'string', 'max:255'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -28,8 +54,8 @@ class StoreRecordingRequest extends FormRequest
|
|||||||
public function messages(): array
|
public function messages(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'audio.required' => 'Please choose an MP3 file to upload.',
|
'audio.required' => 'Please choose an audio file to upload.',
|
||||||
'audio.mimes' => 'Only MP3 files are supported.',
|
'audio' => 'Unsupported audio type. Use MP3, WAV, OGG, FLAC, M4A, AAC, WebM, WMA, or AIFF.',
|
||||||
'audio.max' => 'The audio file may not be larger than 100 MB.',
|
'audio.max' => 'The audio file may not be larger than 100 MB.',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use getID3;
|
|||||||
class Mp3MetadataService
|
class Mp3MetadataService
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Extract ID3 and audio metadata from an MP3 file on disk.
|
* Extract tags and duration from a common audio file (MP3, WAV, OGG, FLAC, M4A, etc.).
|
||||||
*
|
*
|
||||||
* @return array{
|
* @return array{
|
||||||
* title: ?string,
|
* title: ?string,
|
||||||
@@ -23,17 +23,15 @@ class Mp3MetadataService
|
|||||||
$analyzer = new getID3;
|
$analyzer = new getID3;
|
||||||
$info = $analyzer->analyze($absolutePath);
|
$info = $analyzer->analyze($absolutePath);
|
||||||
|
|
||||||
$tags = [];
|
$tags = $this->preferredTags($info);
|
||||||
if (isset($info['tags']['id3v2'])) {
|
|
||||||
$tags = $info['tags']['id3v2'];
|
|
||||||
} elseif (isset($info['tags']['id3v1'])) {
|
|
||||||
$tags = $info['tags']['id3v1'];
|
|
||||||
}
|
|
||||||
|
|
||||||
$title = $this->firstTag($tags, 'title');
|
$title = $this->firstTag($tags, 'title');
|
||||||
$artist = $this->firstTag($tags, 'artist');
|
$artist = $this->firstTag($tags, 'artist');
|
||||||
$album = $this->firstTag($tags, 'album');
|
$album = $this->firstTag($tags, 'album');
|
||||||
$year = $this->firstTag($tags, 'year') ?? $this->firstTag($tags, 'recording_time');
|
$year = $this->firstTag($tags, 'year')
|
||||||
|
?? $this->firstTag($tags, 'date')
|
||||||
|
?? $this->firstTag($tags, 'recording_time')
|
||||||
|
?? $this->firstTag($tags, 'creation_date');
|
||||||
|
|
||||||
$duration = isset($info['playtime_seconds'])
|
$duration = isset($info['playtime_seconds'])
|
||||||
? (int) round((float) $info['playtime_seconds'])
|
? (int) round((float) $info['playtime_seconds'])
|
||||||
@@ -57,6 +55,38 @@ class Mp3MetadataService
|
|||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pick the richest tag set getID3 found for this format.
|
||||||
|
*
|
||||||
|
* @param array<string, mixed> $info
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
private function preferredTags(array $info): array
|
||||||
|
{
|
||||||
|
$priority = [
|
||||||
|
'id3v2',
|
||||||
|
'id3v1',
|
||||||
|
'vorbiscomment',
|
||||||
|
'quicktime',
|
||||||
|
'riff',
|
||||||
|
'asf',
|
||||||
|
'ape',
|
||||||
|
'matroska',
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($priority as $format) {
|
||||||
|
if (! empty($info['tags'][$format]) && is_array($info['tags'][$format])) {
|
||||||
|
return $info['tags'][$format];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! empty($info['comments']) && is_array($info['comments'])) {
|
||||||
|
return $info['comments'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array<string, mixed> $tags
|
* @param array<string, mixed> $tags
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
@section('content')
|
@section('content')
|
||||||
<div class="mb-8">
|
<div class="mb-8">
|
||||||
<h1 class="text-2xl font-semibold tracking-tight">Upload recording</h1>
|
<h1 class="text-2xl font-semibold tracking-tight">Upload recording</h1>
|
||||||
<p class="mt-1 text-sm text-stone-600">Upload an MP3 from your pocket recorder. ID3 metadata is extracted automatically.</p>
|
<p class="mt-1 text-sm text-stone-600">Upload audio from your pocket recorder. Embedded metadata is extracted when available.</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form
|
<form
|
||||||
@@ -17,15 +17,16 @@
|
|||||||
@csrf
|
@csrf
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label for="audio" class="block text-sm font-medium text-stone-700">MP3 file</label>
|
<label for="audio" class="block text-sm font-medium text-stone-700">Audio file</label>
|
||||||
<input
|
<input
|
||||||
id="audio"
|
id="audio"
|
||||||
type="file"
|
type="file"
|
||||||
name="audio"
|
name="audio"
|
||||||
accept=".mp3,audio/mpeg"
|
accept=".mp3,.wav,.ogg,.oga,.flac,.m4a,.mp4,.aac,.webm,.wma,.aiff,.aif,audio/*"
|
||||||
required
|
required
|
||||||
class="mt-2 block w-full text-sm text-stone-600 file:mr-4 file:rounded file:border-0 file:bg-teal-50 file:px-3 file:py-2 file:text-sm file:font-medium file:text-teal-800 hover:file:bg-teal-100"
|
class="mt-2 block w-full text-sm text-stone-600 file:mr-4 file:rounded file:border-0 file:bg-teal-50 file:px-3 file:py-2 file:text-sm file:font-medium file:text-teal-800 hover:file:bg-teal-100"
|
||||||
>
|
>
|
||||||
|
<p class="mt-2 text-xs text-stone-500">MP3, WAV, OGG, FLAC, M4A, AAC, WebM, WMA, or AIFF (max 100 MB).</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
@@ -35,7 +36,7 @@
|
|||||||
type="text"
|
type="text"
|
||||||
name="title"
|
name="title"
|
||||||
value="{{ old('title') }}"
|
value="{{ old('title') }}"
|
||||||
placeholder="Leave blank to use ID3 title or filename"
|
placeholder="Leave blank to use embedded title or filename"
|
||||||
class="mt-2 w-full rounded border border-stone-300 px-3 py-2 text-sm shadow-sm focus:border-teal-600 focus:outline-none focus:ring-1 focus:ring-teal-600"
|
class="mt-2 w-full rounded border border-stone-300 px-3 py-2 text-sm shadow-sm focus:border-teal-600 focus:outline-none focus:ring-1 focus:ring-teal-600"
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user