2021-12-14 17:41:10 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
2022-02-19 17:06:11 +01:00
|
|
|
* @copyright 2021 Ad Aures
|
2021-12-14 17:41:10 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2023-03-16 14:00:05 +01:00
|
|
|
namespace Modules\Media\Entities;
|
2021-12-14 17:41:10 +01:00
|
|
|
|
|
|
|
use CodeIgniter\Files\File;
|
|
|
|
use JamesHeinrich\GetID3\GetID3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property float $duration
|
|
|
|
* @property int $header_size
|
|
|
|
*/
|
2021-12-17 18:14:37 +01:00
|
|
|
class Audio extends BaseMedia
|
2021-12-14 17:41:10 +01:00
|
|
|
{
|
|
|
|
protected string $type = 'audio';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string, mixed>|null $data
|
|
|
|
*/
|
|
|
|
public function __construct(array $data = null)
|
|
|
|
{
|
|
|
|
parent::__construct($data);
|
|
|
|
|
|
|
|
if ($this->file_metadata) {
|
|
|
|
$this->duration = (float) $this->file_metadata['playtime_seconds'];
|
|
|
|
$this->header_size = (int) $this->file_metadata['avdataoffset'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setFile(File $file): self
|
|
|
|
{
|
|
|
|
parent::setFile($file);
|
|
|
|
|
|
|
|
$getID3 = new GetID3();
|
2023-03-16 14:00:05 +01:00
|
|
|
$audioMetadata = $getID3->analyze($file->getRealPath());
|
2021-12-14 17:41:10 +01:00
|
|
|
|
2021-12-20 18:12:12 +01:00
|
|
|
$this->attributes['file_mimetype'] = $audioMetadata['mime_type'];
|
2021-12-14 17:41:10 +01:00
|
|
|
$this->attributes['file_size'] = $audioMetadata['filesize'];
|
2021-12-21 14:42:31 +01:00
|
|
|
$this->attributes['description'] = @$audioMetadata['id3v2']['comments']['comment'][0];
|
2023-06-21 18:17:11 +02:00
|
|
|
$this->attributes['file_metadata'] = json_encode([
|
|
|
|
'playtime_seconds' => $audioMetadata['playtime_seconds'],
|
|
|
|
'avdataoffset' => $audioMetadata['avdataoffset'],
|
|
|
|
], JSON_INVALID_UTF8_SUBSTITUTE);
|
2021-12-14 17:41:10 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|