Major refactor of video processing.

This commit is contained in:
Deon George 2024-09-16 23:17:51 +10:00
parent f9cdc8f9d2
commit a5b5256793
9 changed files with 230 additions and 259 deletions

View File

@ -65,7 +65,7 @@ class CatalogScan implements ShouldQueue, ShouldBeUnique
$x = $this->o->myduplicates()->get();
if (count($x)) {
foreach ($x as $this->oo) {
// And that photo is not marked as a duplicate
// And that catalog item is not marked as a duplicate
if (! $this->oo->duplicate) {
$this->o->duplicate = TRUE;
Log::alert(sprintf('Image [%s] marked as a duplicate',$this->o->filename));

View File

@ -64,6 +64,11 @@ class QuickTime extends Base {
return $atom->{$key};
case 'make':
case 'model':
case 'software':
return $this->find_atoms(moov\meta::class,1)->{$key};
case 'time_scale':
$atom = $this->find_atoms(moov\mvhd::class,1);

View File

@ -40,6 +40,11 @@ class meta extends SubAtom
case 'gps_lon':
return Arr::get($this->gps,'lon');
case 'make':
case 'model':
case 'software':
return Arr::get($this->cache,'mdta.com.apple.quicktime.'.$key);
default:
return parent::__get($key);
}

View File

@ -6,17 +6,6 @@ namespace App\Media;
* This represents a media type that we dont know how to parse
*/
class Unknown extends Base {
private const LOGKEY = 'MF?';
private string $type;
public function __construct(string $filename,string $type)
{
parent::__construct($filename);
$this->type = $type;
}
public function __get(string $key): mixed
{
return NULL;

View File

@ -2,190 +2,119 @@
namespace App\Models;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use App\Jobs\CatalogMove;
use App\Media\{Base,Factory};
class Video extends Abstracted\Catalog
{
// ID3 Object
private $_o = NULL;
public const config = 'video';
public function getIDLinkAttribute()
protected $casts = [
'created'=>'datetime:Y-m-d H:i:s',
];
// Media Object
private ?Base $_o;
protected array $init = [
'creation_date',
'gps',
'heightwidth',
'signature',
'software',
];
public static function boot()
{
return $this->HTMLLinkAttribute($this->id,'v/info');
parent::boot();
// Any video saved, queue it to be moved.
self::saved(function($item) {
if ($item->scanned && (! $item->duplicate) && (! $item->remove) && ($x=$item->shouldMove()) === TRUE) {
Log::info(sprintf('%s: Need to Move [%s]',__METHOD__,$item->id.'|'.serialize($x)));
CatalogMove::dispatch($item)
->onQueue('move');
}
});
}
public function getHtmlImageURL()
public function __get($key): mixed
{
return sprintf('<video width="320" height="240" src="%s" controls></video>',url('/v/view/'.$this->id));
}
if ($key === 'o') {
if (isset($this->_o))
return $this->_o;
/**
* Return an GetID3 object or attribute
*
*/
protected function o($attr=NULL)
{
if (! $this->filename OR ! file_exists($this->file_path()) OR ! is_readable($this->file_path()))
return FALSE;
if ((! file_exists($this->file_name(FALSE))) || (! is_readable($this->file_name(FALSE))))
return $this->_o = NULL;
if (is_null($this->_o))
{
/*
* @todo There is a bug with getID3, where the second iteration yields different results
* this is problematic when video:scanall is run from a queue and there is more than 1 job.
* It also appears that only 1.9.18 gets date/gps data, a later version doesnt get gps data.
*/
$this->_o = new \getID3;
$this->_o->analyze($this->file_path());
$this->_o->getHashdata('sha1');
if (! isset($this->_o)) {
$this->_o = Factory::create($this->file_name(FALSE));
return $this->_o;
}
}
return is_null($attr) ? $this->_o : (array_key_exists($attr,$this->_o->info) ? $this->_o->info[$attr] : NULL);
return parent::__get($key);
}
public function property(string $property)
/* METHODS */
public function custom_init()
{
if (! $this->o())
return NULL;
$this->audio_channels = $this->getObjectOriginal('audio_channels');
$this->audio_codec = $this->getObjectOriginal('audio_codec');
$this->audio_samplerate = $this->getObjectOriginal('audio_samplerate');
$this->gps_altitude = $this->getObjectOriginal('gps_altitude');
$this->length = round($this->getObjectOriginal('length'),2);
$this->type = $this->getObjectOriginal('type');
$this->video_codec = $this->getObjectOriginal('video_codec');
$this->video_framerate = $this->getObjectOriginal('video_framerate');
/*
$x = new \getID3;
$x->analyze($this->file_name(FALSE));
dump(['id3'=>$x]);
*/
}
public function getObjectOriginal(string $property): mixed
{
switch ($property) {
case 'creationdate':
// Try creation_Data
$x = Arr::get($this->_o->info,'quicktime.comments.creation_date.0');
case 'file_signature':
return md5_file($this->file_name(FALSE));
// Try creation_Data
if (is_null($x))
$x = Arr::get($this->_o->info,'quicktime.comments.creationdate.0');
case 'length':
return $this->o?->duration;
return $x;
case 'make': return Arr::get($this->_o->info,'quicktime.comments.make.0');
case 'model': return Arr::get($this->_o->info,'quicktime.comments.model.0');
case 'software': return Arr::get($this->_o->info,'quicktime.comments.software.0');
case 'signature': return Arr::get($this->_o->info,'sha1_data');
#case 'height': return $this->subatomsearch('quicktime.moov.subatoms',['trak','tkhd'],'height'); break;
#case 'width': return $this->subatomsearch('quicktime.moov.subatoms',['trak','tkhd'],'width'); break;
case 'height': return Arr::get($this->_o->info,'video.resolution_y');
case 'width': return Arr::get($this->_o->info,'video.resolution_x');
case 'length': return Arr::get($this->_o->info,'playtime_seconds');
case 'type': return Arr::get($this->_o->info,'video.dataformat');
case 'codec': return Arr::get($this->_o->info,'audio.codec');
case 'audiochannels': return Arr::get($this->_o->info,'audio.channels');
case 'samplerate': return Arr::get($this->_o->info,'audio.sample_rate');
case 'channelmode': return Arr::get($this->_o->info,'audio.channelmode');
case 'gps_lat': return Arr::get($this->_o->info,'quicktime.comments.gps_latitude.0');
case 'gps_lon': return Arr::get($this->_o->info,'quicktime.comments.gps_longitude.0');
case 'gps_altitude': return Arr::get($this->_o->info,'quicktime.comments.gps_altitude.0');
case 'identifier':
if ($x=Arr::get($this->_o->info,'quicktime.comments')) {
return Arr::get(Arr::flatten(Arr::only($x,'content.identifier')),0);
}
break;
case 'audio_channels':
case 'audio_codec':
case 'audio_samplerate':
case 'creation_date':
case 'gps_altitude':
case 'gps_lat':
case 'gps_lon':
case 'height':
case 'make':
case 'model':
case 'signature':
case 'software':
case 'type':
case 'width':
case 'video_codec':
case 'video_framerate':
return $this->o?->{$property};
default:
return NULL;
throw new \Exception('To implement: '.$property);
}
}
public function properties()
{
return $this->o() ? $this->_o->info : [];
}
/**
* Navigate through ID3 data to find the value.
* Return the extension of the video
*/
private function subatomsearch($atom,array $paths,$key,array $data=[]) {
if (! $data AND is_null($data = Arr::get($this->_o->info,$atom))) {
// Didnt find it.
return NULL;
}
foreach ($paths as $path) {
$found = FALSE;
foreach ($data as $array) {
if ($array['name'] === $path) {
$found = TRUE;
if ($path != last($paths))
$data = $array['subatoms'];
else
$data = $array;
break;
}
}
if (! $found)
break;
}
return isset($data[$key]) ? $data[$key] : NULL;
}
public function setDateCreated()
{
$this->created = $this->property('creationdate') ? strtotime($this->property('creationdate')) : NULL;
}
public function setLocation()
{
$this->gps_lat = $this->property('gps_lat');
$this->gps_lon = $this->property('gps_lon');
$this->gps_altitude = $this->property('gps_altitude');
}
public function setMakeModel()
{
$ma = NULL;
if ($this->property('make'))
$ma = Make::firstOrCreate([
'name'=>$this->property('make'),
]);
$mo = Model::firstOrCreate([
'name'=>$this->property('model') ?: NULL,
'make_id'=>$ma ? $ma->id : NULL,
]);
$so = Software::firstOrCreate([
'name'=>$this->property('software') ?: NULL,
'model_id'=>$mo->id,
]);
$this->software_id = $so->id;
$this->type = $this->property('type');
$this->length = round($this->property('length'),2);
$this->codec = $this->property('codec');
$this->audiochannels = $this->property('audiochannels');
$this->channelmode = $this->property('channelmode');
$this->samplerate = $this->property('samplerate');
}
public function setSignature()
{
parent::setSignature();
$this->identifier = $this->property('identifier');
}
public function setSubSecTime()
{
// NOOP
}
public function setThumbnail()
{
// NOOP
}
/**
* Return the extension of the image
*/
public function type($mime=FALSE)
public function type($mime=FALSE): string
{
return strtolower($mime ? File::mime_by_ext(pathinfo($this->filename,PATHINFO_EXTENSION)) : pathinfo($this->filename,PATHINFO_EXTENSION));
}

View File

@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement('ALTER TABLE videos RENAME COLUMN codec TO audio_codec');
DB::statement('ALTER TABLE videos RENAME COLUMN samplerate TO audio_samplerate');
DB::statement('ALTER TABLE videos RENAME COLUMN audiochannels TO audio_channels');
Schema::table('videos', function (Blueprint $table) {
$table->string('video_codec')->nullable();
$table->float('video_framerate')->nullable();
$table->dropForeign(['model_id']);
$table->dropColumn(['identifier','channelmode','model_id']);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
DB::statement('ALTER TABLE videos RENAME COLUMN audio_codec TO codec');
DB::statement('ALTER TABLE videos RENAME COLUMN audio_samplerate TO samplerate');
DB::statement('ALTER TABLE videos RENAME COLUMN audio_channels TO audiochannels');
Schema::table('videos', function (Blueprint $table) {
$table->dropColumn(['video_codec','video_framerate']);
$table->string('identifier')->nullable();
$table->string('channelmode',16)->nullable();
$table->bigInteger('model_id')->unsigned()->nullable();
$table->foreign('model_id')->references('id')->on('models');
});
}
};

View File

@ -0,0 +1,13 @@
<ul class="pagination">
<li class="page-item @disabled(! $x=$o->previous())">
<a class="page-link" href="{{ $x ? url($pre.'/info',$x->id) : '#' }}">&lt;&lt;</a>
</li>
<li class="page-item active">
<span class="page-link">{{ $o->id }}</span>
</li>
<li class="page-item @disabled(! $x=$o->next())">
<a class="page-link" href="{{ $x ? url($pre.'/info',$x->id) : '#' }}">&gt;&gt;</a>
</li>
</ul>

View File

@ -0,0 +1,3 @@
<a href="{{ url('v/view',$id) }}" target="{{ $id }}">
<video class="pb-3 w-100" src="{{ url('v/view/'.$id) }}" controls></video>
</a>

View File

@ -1,115 +1,96 @@
@extends('adminlte::layouts.app')
@section('htmlheader_title')
Video - {{ $o->id }}
Video - {{ $vo->id }}
@endsection
@section('contentheader_title')
Video #{{ $o->id }}
Video #{{ $vo->id }}
@endsection
@section('contentheader_description')
@if(! $o->scanned)<button class="btn btn-sm btn-info">TO SCAN</button>@endif
@if($o->duplicate)<button class="btn btn-sm btn-warning">DUPLICATE</button>@endif
@if($o->ignore_duplicate)<button class="btn btn-sm btn-secondary">DUPLICATE IGNORE</button>@endif
@if($o->remove)<button class="btn btn-sm btn-danger">PENDING DELETE</button>@endif
@endsection
@section('page_title')
#{{ $o->id }}
#{{ $vo->id }}
@endsection
@section('main-content')
<div class="row">
<div class="col-4">
<a href="{{ url('v/view',$o->id) }}">{!! $o->getHtmlImageURL() !!}</a>
<x-video.thumbnail :id="$vo->id"/>
<span class="pagination justify-content-center">
<nav>
<ul class="pagination">
<li class="page-item @if(! $x=$o->previous())disabled @endif" aria-disabled="@if(! $x)true @else false @endif" aria-label="&laquo; Previous">
<a class="page-link" href="{{ $x ? url('v/info',$x->id) : '#' }}">&lt;&lt;</a>
</li>
<li class="page-item active" aria-current="page"><span class="page-link">{{ $o->id }}</span></li>
<li class="page-item @if(! $x=$o->next())disabled @endif" aria-disabled="@if(! $x)true @else false @endif" aria-label="&laquo; Previous">
<a class="page-link" href="{{ $x ? url('v/info',$x->id) : '#' }}">&gt;&gt;</a>
</li>
</ul>
</nav>
</span>
<div class="pagination justify-content-center">
<x-pagination.nav :o="$vo" pre="v"/>
</div>
</div>
<div class="col-8">
<div class="dl-horizontal">
<dt>Signature</dt><dd>{{ $o->signature(TRUE) }}</dd>
<dt>Filename</dt><dd>{{ $o->filename }}<dd>
<div class="card">
<div class="card-body">
<div class="float-right">
@if(! $vo->scanned)<button class="btn btn-sm btn-info">TO SCAN</button>@endif
@if($vo->duplicate)<button class="btn btn-sm btn-warning">DUPLICATE</button>@endif
@if($vo->ignore_duplicate)<button class="btn btn-sm btn-secondary">DUPLICATE IGNORE</button>@endif
@if($vo->remove)<button class="btn btn-sm btn-danger">PENDING DELETE</button>@endif
</div>
<div class="dl-horizontal">
<dt>Signature</dt><dd>{{ $vo->signature(TRUE) }}</dd>
<dt>Filename</dt><dd>{{ $vo->filename }}<dd>
@if ($o->shouldMove())
<dt>NEW Filename</dt><dd>{{ $o->file_name() }}<dd>
@endif
@if ($vo->shouldMove())
<dt>NEW Filename</dt><dd>{{ $vo->file_name() }}<dd>
@endif
<dt>Size</dt><dd>{{ number_format($vo->file_size) }}<dd>
<dt>Date Taken</dt><dd>{{ $vo->created?->format('Y-m-d H:i:s') }}<dd>
@if($vo->scanned)
<dt>Dimensions</dt><dd>{{ $vo->dimensions }}<dd>
<dt>Length</dt><dd>{{ $vo->length }}<dd>
<dt>Type</dt><dd>{{ $vo->type }}<dd>
<hr>
<dt>Audio Codec</dt><dd>{{ $vo->audio_codec }}<dd>
<dt>Audio Channels</dt><dd>{{ $vo->audio_channels }}<dd>
<dt>Sample Rate</dt><dd>{{ $vo->audio_samplerate }}<dd>
<hr>
<dt>Video Codec</dt><dd>{{ $vo->video_codec }}<dd>
<dt>Video Framerate</dt><dd>{{ $vo->video_framerate }}<dd>
<dt>Camera</dt><dd>{{ $vo->device }}<dd>
<hr>
<dt>Location</dt>
<dd>
@if(! $vo->gps)
UNKNOWN
@else
<x-map :o="$vo"/>
@endif
</dd>
@if(($x=$vo->myduplicates()->get())->count())
<dt>Duplicates</dt>
<dd>
@foreach($x as $oo)
@if(! $loop->first)| @endif
{!! $oo->id_link !!}
@endforeach
</dd>
@endif
@endif
</div>
@if ($vo->remove)
<form action="{{ url('v/undelete',$vo->id) }}" method="POST">
<button class="btn btn-sm btn-primary">Undelete</button>
<dt>Size</dt><dd>{{ $o->file_size() }}<dd>
<dt>Dimensions</dt><dd>{{ $o->dimensions }}<dd>
<dt>Length</dt><dd>{{ $o->length }}<dd>
<dt>Type</dt><dd>{{ $o->type }}<dd>
<dt>Codec</dt><dd>{{ $o->codec }}<dd>
<dt>Audio Channels</dt><dd>{{ $o->audiochannels }}<dd>
<dt>Channels Mode</dt><dd>{{ $o->channelmode }}<dd>
<dt>Sample Rate</dt><dd>{{ $o->samplerate }}<dd>
<hr>
<dt>Date Taken</dt><dd>{{ $o->date_taken() }}<dd>
<dt>Camera</dt><dd>{{ $o->device() }}<dd>
<dt>Identifier</dt><dd>{{ $o->identifier }}<dd>
<hr>
<dt>Location</dt>
<dd>
@if($o->gps() == 'UNKNOWN')
UNKNOWN
@else
<div id="map" style="width: 400px; height: 300px"></div>
<form action="{{ url('v/delete',$vo->id) }}" method="POST">
<button class="btn btn-sm btn-danger">Delete</button>
@endif
</dd>
@if(($x=$o->myduplicates()->get())->count())
<dt>Duplicates</dt>
<dd>
@foreach($x as $oo)
@if(! $loop->first)| @endif
{!! $oo->id_link !!}
@endforeach
</dd>
@endif
@csrf
</form>
</div>
</div>
@if ($o->remove)
<form action="{{ url('v/undelete',$o->id) }}" method="POST">
<button class="btn btn-primary">Undelete</button>
@else
<form action="{{ url('v/delete',$o->id) }}" method="POST">
<button class="btn btn-danger">Delete</button>
@endif
{{ csrf_field() }}
</form>
</div>
</div>
@endsection
@section('page-scripts')
@if($o->gps() !== 'UNKNOWN')
@js('//maps.google.com/maps/api/js?sensor=false')
<script type="text/javascript">
var myLatLng = {lat: {{ $o->gps_lat }}, lng: {{ $o->gps_lon }}};
var map = new google.maps.Map(document.getElementById("map"), {
zoom: 16,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
map: map,
position: myLatLng,
});
</script>
@endif
@append