photo/app/Console/Commands/VideoImport.php

199 lines
4.7 KiB
PHP
Raw Normal View History

2016-07-04 06:00:33 +00:00
<?php
namespace App\Console\Commands;
use Log;
use Illuminate\Console\Command;
use App\Model\Video;
use App\Model\Tag;
use App\Model\Person;
class VideoImport extends Command
{
use \App\Traits\Files;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'video:import
{--dir= : Directory to Parse}
{--file= : File to Import}
{--ignoredupe : Ignore duplicate files}
{--deletedupe : Delete duplicate files}
{--dumpid3 : Dump ID3 data}
{--people= : People to reference in video}
{--tags= : Add tag to video}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import videos into the database';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$files = $this->getFiles(['dir'=>$this->option('dir'),'file'=>$this->option('file')],'video');
if (! count($files))
exit;
// Show a progress bar
$bar = $this->output->createProgressBar(count($files));
$bar->setFormat("%current%/%max% [%bar%] %percent:3s%% (%memory%) (%remaining%) ");
$tags = NULL;
$t = $p = array();
// Tags
if ($this->option('tags'))
{
$tags = explode(',',$this->option('tags'));
$t = Tag::whereIn('tag',$tags)->pluck('id')->toArray();
if (! $t OR count($t) != count($tags))
{
$this->error(sprintf('Tag [%s] dont exist',join('|',$tags)));
abort(501);
}
}
// People
if ($this->option('people'))
{
$tags = explode(',',$this->option('people'));
$p = Person::whereIn('tag',$tags)->pluck('id')->toArray();
if (! $p OR count($p) != count($tags))
{
$this->error(sprintf('People [%s] dont exist',join('|',$tags)));
abort(501);
}
}
$c = 0;
foreach ($files as $file)
{
$bar->advance();
if (preg_match('/@__thumb/',$file) OR preg_match('/\/._/',$file))
{
$this->warn(sprintf('Ignoring file [%s]',$file));
continue;
}
if (! in_array(strtolower(pathinfo($file,PATHINFO_EXTENSION)),config('video.import.accepted')))
{
$this->warn(sprintf('Ignoring [%s]',$file));
continue;
}
if ($this->option('verbose'))
$this->info(sprintf('Processing file [%s]',$file));
$c++;
$vo = Video::where('filename',$file)->first();
if (is_null($vo))
{
$vo = new Video;
$vo->filename = $file;
}
if (! is_readable($vo->file_path()))
{
$this->warn(sprintf('Ignoring [%s], it is not readable',$vo->file_path()));
continue;
}
$vo->date_created = strtotime($vo->property('creationdate'));
$vo->signature = $vo->property('signature');
$vo->make = $vo->property('make');
$vo->model = $vo->property('model');
$vo->height = $vo->property('height');
$vo->width = $vo->property('width');
$vo->type = $vo->property('type');
$vo->length = $vo->property('length');
$vo->codec = $vo->property('codec');
$vo->audiochannels = $vo->property('audiochannels');
$vo->channelmode = $vo->property('channelmode');
$vo->samplerate = $vo->property('samplerate');
$vo->gps_lat = $vo->property('gps_lat');
$vo->gps_lon = $vo->property('gps_lon');
$vo->gps_altitude = $vo->property('gps_altitude');
// If this is a duplicate
$x = Video::whereIN('id',$vo->list_duplicate())->get();
if (count($x)) {
$skip = FALSE;
foreach ($x as $o) {
// We'll only ignore based on the same signature.
if ($vo->signature != $o->signature AND ! $vo->exists)
continue;
if ($this->option('ignoredupe'))
{
$skip = TRUE;
$this->warn(sprintf("Ignoring file [%s], it's the same as [%s] with id %s",$vo->file_path(),$o->filename,$o->id));
break;
}
elseif ($this->option('deletedupe') AND ($vo->filename != $o->filename))
{
$skip = TRUE;
$this->error(sprintf("Deleting file [%s], it's the same as [%s] with id %s and signature [%s]\n",$vo->file_path(),$o->filename,$o->id,$vo->signature));
unlink($vo->file_path());
}
}
if ($skip)
continue;
$vo->duplicate = '1';
$this->warn(sprintf('Video [%s] marked as a duplicate',$file));
}
if ($vo->exists)
$this->warn(sprintf('Video [%s] already in DB: %s',$file,$vo->id));
$vo->save();
if ($vo->wasRecentlyCreated)
$this->info(sprintf('Video [%s] stored in DB: %s',$file,$vo->id));
// Record our people and tags
if ($p)
$vo->People()->sync($p);
if ($t)
$vo->Tags()->sync($t);
if ($this->option('dumpid3'))
dd($vo->dump());
}
$bar->finish();
return $this->info(sprintf('Video processed: %s',$c));
}
}