belongsTo(User::class); } public function category() { return $this->belongsTo(Category::class); } public function comments() { return $this->hasMany(Comment::class); } public function faved() { return $this->belongsToMany(User::class, 'favorites', 'video_id', 'user_id'); } public static function getFirstId($related = null) { if ($related) { return $related->videos()->filtered()->orderBy('id', 'ASC')->first()->id; } return static::filtered()->orderBy('id', 'ASC')->first()->id; } public static function getLastId($related = null) { if ($related) { return $related->videos()->filtered()->orderBy('id', 'DESC')->first()->id; } return static::select('id')->filtered()->orderBy('id', 'DESC')->first()->id; } public function getNext($related = null) { if ($related) { return $related->videos()->filtered()->where('id', '>', $this->id)->orderBy('id', 'ASC')->first(); } else { return static::filtered()->where('id', '>', $this->id)->orderBy('id', 'ASC')->first(); } } public function getPrev($related = null) { if ($related) { return $related->videos()->filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first(); } else { return static::filtered()->where('id', '<', $this->id)->orderBy('id', 'DESC')->first(); } } public function scopeNewlyups($query) { return $query->where('created_at', '>=', Carbon::now()->subHours(12)); } public function scopeFiltered($query) { if(auth()->check()) { // TODO rename to filtered $filter = auth()->user()->categories; if(empty($filter)) return $query; return $query->withoutAnyTags($filter); } else { // TODO: filter if post has sfw & nsfw tags //return $query->withAllTags('sfw'); return $query->withoutAnyTags('nsfw'); } } public function checkFileEncoding() { $dat = $this->file; $in = public_path() . "/b"; $tmpdir = str_replace("public", "app/Http/Controllers/tmp", public_path()); for($i = 0; $i < 2; $i++) { $ret = shell_exec("ffmpeg -y -ss 0 -i {$in}/{$dat} -vframes 1 {$tmpdir}/test.png 2>&1"); if(strpos($ret, "nothing was encoded") !== false) { shell_exec("ffmpeg -i {$in}/{$dat} -map 0:0 -map 0:1 -c:v copy {$tmpdir}/{$dat}"); unlink($in . "/" . $dat); rename($tmpdir . "/" . $dat, $in . "/" . $dat); } else return true; } return false; } /** * Creates a .gif thumbnail to a given video file * * @param string $dat File of the video */ public function createThumbnail() { $dat = $this->file; $in = public_path() . "/b"; // webm-input $out = public_path() . "/thumbs"; //thumb-output $tmpdir = str_replace("public", "app/Http/Controllers/tmp", public_path()); $name = explode(".", $dat); array_pop($name); $name = join(".", $name); if(!file_exists("{$out}/{$name}.gif")) { $length = round(shell_exec("ffprobe -i {$in}/{$dat} -show_format -v quiet | sed -n 's/duration=//p'")); for ($i = 1; $i < 10; $i++) { $act = ($i * 10) * ($length / 100); $ffmpeg = shell_exec("ffmpeg -ss {$act} -i {$in}/{$dat} -vf \"scale='if(gt(a,4/3),206,-1)':'if(gt(a,4/3),-1,116)'\" -vframes 1 {$tmpdir}/{$name}_{$i}.png 2>&1"); } $tmp = shell_exec("convert -delay 27 -loop 0 {$tmpdir}/{$name}_*.png {$out}/{$name}.gif 2>&1"); if(@filesize("{$out}/{$name}.gif") < 2000) @unlink("{$out}/{$name}.gif"); array_map('unlink', glob("{$tmpdir}/{$name}*.png")); } } public function tesThumb() { set_time_limit(9899999999999999); $dat = $this->file; #$in = public_path() . "/b"; // webm-input $in = "/home/w0bm/w0bm/public/b"; $out = public_path() . "/thumbs/beta"; //thumb-output $tmpdir = str_replace("public", "app/Http/Controllers/tmp", public_path()); $name = explode(".", $dat); array_pop($name); $name = join(".", $name); #$ffmpegthumbnailer = round(shell_exec("ffmpegthumbnailer -i {$in}/{$dat} -a -o {$out}/{$name}_test.png")); //$ffmpeg = round(shell_exec("ffmpeg -i {$in}/{$dat} -vf select='eq(pict_type,PICT_TYPE_I)' -vf scale=128:128 {$out}/{$name}_ffmpeg.png")); $length = round(shell_exec("ffprobe -i {$in}/{$dat} -show_format -v quiet | sed -n 's/duration=//p'")); $half = $length / 20; $ffmpeg = exec("ffmpegthumbnailer -i {$in}/{$dat} -o {$out}/{$name}_test.png", $output, $return); if ($return != 0) { // error @unlink("{$out}/{$name}_test.png"); exec("ffmpeg -i {$in}/{$dat} -vf select='eq(pict_type,PICT_TYPE_I)' -vf scale=128:128:force_original_aspect_ratio=increase,crop=128:128 {$out}/{$name}.png"); }else{ // success @unlink("{$out}/{$name}_test.png"); exec("ffmpeg -i {$in}/{$dat} -vf select='eq(pict_type,PICT_TYPE_I)' -vf scale=128:128:force_original_aspect_ratio=increase,crop=128:128 -ss {$half} {$out}/{$name}.png"); } } public function blurryThumb() { set_time_limit(9899999999999999); $dat = $this->file; #$in = public_path() . "/b"; // webm-input $in = "/home/w0bm/w0bm/public/b"; $out = public_path() . "/thumbs/blurred"; //thumb-output $tmpdir = str_replace("public", "app/Http/Controllers/tmp", public_path()); $name = explode(".", $dat); array_pop($name); $name = join(".", $name); #$ffmpegthumbnailer = round(shell_exec("ffmpegthumbnailer -i {$in}/{$dat} -a -o {$out}/{$name}_test.png")); //$ffmpeg = round(shell_exec("ffmpeg -i {$in}/{$dat} -vf select='eq(pict_type,PICT_TYPE_I)' -vf scale=128:128 {$out}/{$name}_ffmpeg.png")); $length = round(shell_exec("ffprobe -i {$in}/{$dat} -show_format -v quiet | sed -n 's/duration=//p'")); $half = $length / 20; $ffmpeg = exec("ffmpegthumbnailer -i {$in}/{$dat} -o {$out}/{$name}_testblur.png", $output, $return); if ($return != 0) { // error @unlink("{$out}/{$name}_testblur.png"); exec("ffmpeg -i {$in}/{$dat} -vf select='eq(pict_type,PICT_TYPE_I)' -vf scale=128:128:force_original_aspect_ratio=increase,crop=128:128 {$out}/{$name}.png"); exec("convert {$out}/{$name}.png -blur 0x8 {$out}/{$name}_blurred.png"); @unlink("{$out}/{$name}.png"); }else{ // success @unlink("{$out}/{$name}_testblur.png"); exec("ffmpeg -i {$in}/{$dat} -vf select='eq(pict_type,PICT_TYPE_I)' -vf scale=128:128:force_original_aspect_ratio=increase,crop=128:128 -ss {$half} {$out}/{$name}.png"); exec("convert {$out}/{$name}.png -blur 0x8 {$out}/{$name}_blurred.png"); @unlink("{$out}/{$name}.png"); } } public function createThumbnailStatic() { $dat = $this->file; $in = public_path() . "/b"; // webm-input $out = public_path() . "/thumbs/testing"; //thumb-output $tmpdir = str_replace("public", "app/Http/Controllers/tmp", public_path()); $name = explode(".", $dat); array_pop($name); $name = join(".", $name); if(!file_exists("{$out}/{$name}.png")) { $length = round(shell_exec("ffprobe -i {$in}/{$dat} -show_format -v quiet | sed -n 's/duration=//p'")); for ($i = 1; $i < 10; $i++) { $act = ($i * 10) * ($length / 100); $ffmpeg = shell_exec("ffmpeg -ss {$act} -i {$in}/{$dat} -vf \"'select=eq(n\,0)'\" -vf scale=128:128 {$tmpdir}/{$name}_{$i}.png 2>&1"); } $tmp = shell_exec("convert -delay 27 -loop 0 {$tmpdir}/{$name}_*.png {$out}/{$name}.png 2>&1"); if(@filesize("{$out}/{$name}.png") < 2000) @unlink("{$out}/{$name}.png"); array_map('unlink', glob("{$tmpdir}/{$name}*.png")); } } public static function getRandom($related = null) { if ($related) { $id = $related->videos()->filtered()->countScoped()->count() - 1; if ($id < 0) { return redirect()->back()->with('error', 'no videos found'); } $id = mt_rand(0, $id); return $related->videos()->filtered()->skip($id); } $id = static::filtered()->countScoped()->count() - 1; if ($id < 0) { return redirect()->back()->with('error', 'no videos found'); } $id = mt_rand(0, $id); return static::filtered()->skip($id); } public function isSfw() { return $this->tags->contains(function ($key, $tag) { $tag->normalized === 'sfw'; }); } public function filesize() { return filesize(getcwd() . "/b/" . $this->file); } }