Browse Source

autoformat ffmpeg.php

pull/40/head
towards_a_new_leftypol 4 years ago
committed by towards-a-new-leftypol
parent
commit
ffdbaa1806
  1. 114
      inc/lib/webm/ffmpeg.php

114
inc/lib/webm/ffmpeg.php

@ -1,65 +1,65 @@
<?php <?php
/* /*
* ffmpeg.php * ffmpeg.php
* A barebones ffmpeg based webm implementation for vichan. * A barebones ffmpeg based webm implementation for vichan.
*/ */
function get_webm_info($filename) { function get_webm_info($filename) {
global $board, $config; global $board, $config;
$filename = escapeshellarg($filename); $filename = escapeshellarg($filename);
$ffprobe = $config['webm']['ffprobe_path']; $ffprobe = $config['webm']['ffprobe_path'];
$ffprobe_out = array(); $ffprobe_out = array();
$webminfo = array(); $webminfo = array();
exec("$ffprobe -v quiet -print_format json -show_format -show_streams $filename", $ffprobe_out); exec("$ffprobe -v quiet -print_format json -show_format -show_streams $filename", $ffprobe_out);
$ffprobe_out = json_decode(implode("\n", $ffprobe_out), 1); $ffprobe_out = json_decode(implode("\n", $ffprobe_out), 1);
$webminfo['error'] = is_valid_webm($ffprobe_out); $webminfo['error'] = is_valid_webm($ffprobe_out);
if(empty($webminfo['error'])) { if(empty($webminfo['error'])) {
$webminfo['width'] = $ffprobe_out['streams'][0]['width']; $webminfo['width'] = $ffprobe_out['streams'][0]['width'];
$webminfo['height'] = $ffprobe_out['streams'][0]['height']; $webminfo['height'] = $ffprobe_out['streams'][0]['height'];
$webminfo['duration'] = $ffprobe_out['format']['duration']; $webminfo['duration'] = $ffprobe_out['format']['duration'];
} }
return $webminfo; return $webminfo;
} }
function is_valid_webm($ffprobe_out) { function is_valid_webm($ffprobe_out) {
global $board, $config; global $board, $config;
if (empty($ffprobe_out)) if (empty($ffprobe_out))
return array('code' => 1, 'msg' => $config['error']['genwebmerror']); return array('code' => 1, 'msg' => $config['error']['genwebmerror']);
$extension = pathinfo($ffprobe_out['format']['filename'], PATHINFO_EXTENSION); $extension = pathinfo($ffprobe_out['format']['filename'], PATHINFO_EXTENSION);
if ($extension === 'webm') { if ($extension === 'webm' && !stristr($ffprobe_out['format']['format_name'], 'mp4')) {
if ($ffprobe_out['format']['format_name'] != 'matroska,webm') if ($ffprobe_out['format']['format_name'] != 'matroska,webm')
return array('code' => 2, 'msg' => $config['error']['invalidwebm']); return array('code' => 2, 'msg' => $config['error']['invalidwebm']."error 1");
} elseif ($extension === 'mp4') { } elseif ($extension === 'mp4') {
if ($ffprobe_out['streams'][0]['codec_name'] != 'h264' && $ffprobe_out['streams'][1]['codec_name'] != 'aac') if ($ffprobe_out['streams'][0]['codec_name'] != 'h264' && $ffprobe_out['streams'][1]['codec_name'] != 'aac')
return array('code' => 2, 'msg' => $config['error']['invalidwebm']); return array('code' => 2, 'msg' => $config['error']['invalidwebm']."error 2");
} else { } else {
return array('code' => 1, 'msg' => $config['error']['genwebmerror']); return array('code' => 1, 'msg' => $config['error']['genwebmerror']."error 3");
} }
if ((count($ffprobe_out['streams']) > 1) && (!$config['webm']['allow_audio'])) if ((count($ffprobe_out['streams']) > 1) && (!$config['webm']['allow_audio']))
return array('code' => 3, 'msg' => $config['error']['webmhasaudio']); return array('code' => 3, 'msg' => $config['error']['webmhasaudio']."error 4");
if (empty($ffprobe_out['streams'][0]['width']) || (empty($ffprobe_out['streams'][0]['height']))) if (empty($ffprobe_out['streams'][0]['width']) || (empty($ffprobe_out['streams'][0]['height'])))
return array('code' => 2, 'msg' => $config['error']['invalidwebm']); return array('code' => 2, 'msg' => $config['error']['invalidwebm']."error 5");
if ($ffprobe_out['format']['duration'] > $config['webm']['max_length']) if ($ffprobe_out['format']['duration'] > $config['webm']['max_length'])
return array('code' => 4, 'msg' => sprintf($config['error']['webmtoolong'], $config['webm']['max_length'])); return array('code' => 4, 'msg' => sprintf($config['error']['webmtoolong'], $config['webm']['max_length'])."error 6");
} }
function make_webm_thumbnail($filename, $thumbnail, $width, $height, $duration) { function make_webm_thumbnail($filename, $thumbnail, $width, $height, $duration) {
global $board, $config; global $board, $config;
$filename = escapeshellarg($filename); $filename = escapeshellarg($filename);
$thumbnailfc = escapeshellarg($thumbnail); // Should be safe by default but you $thumbnailfc = escapeshellarg($thumbnail); // Should be safe by default but you
// can never be too safe. // can never be too safe.
$width = escapeshellarg($width); $width = escapeshellarg($width);
$height = escapeshellarg($height); // Same as above. $height = escapeshellarg($height); // Same as above.
$ffmpeg = $config['webm']['ffmpeg_path']; $ffmpeg = $config['webm']['ffmpeg_path'];
$ret = 0; $ret = 0;
$ffmpeg_out = array(); $ffmpeg_out = array();
exec("$ffmpeg -strict -2 -ss " . floor($duration / 2) . " -i $filename -v quiet -an -vframes 1 -f mjpeg -vf scale=$width:$height $thumbnailfc 2>&1", $ffmpeg_out, $ret); exec("$ffmpeg -strict -2 -ss " . floor($duration / 2) . " -i $filename -v quiet -an -vframes 1 -f mjpeg -vf scale=$width:$height $thumbnailfc 2>&1", $ffmpeg_out, $ret);
// Work around for https://trac.ffmpeg.org/ticket/4362 // Work around for https://trac.ffmpeg.org/ticket/4362
if (filesize($thumbnail) === 0) { if (filesize($thumbnail) === 0) {
// try again with first frame // try again with first frame
exec("$ffmpeg -y -strict -2 -ss 0 -i $filename -v quiet -an -vframes 1 -f mjpeg -vf scale=$width:$height $thumbnailfc 2>&1", $ffmpeg_out, $ret); exec("$ffmpeg -y -strict -2 -ss 0 -i $filename -v quiet -an -vframes 1 -f mjpeg -vf scale=$width:$height $thumbnailfc 2>&1", $ffmpeg_out, $ret);
clearstatcache(); clearstatcache();
// failed if no thumbnail size even if ret code 0, ffmpeg is buggy // failed if no thumbnail size even if ret code 0, ffmpeg is buggy
if (filesize($thumbnail) === 0) { if (filesize($thumbnail) === 0) {
$ret = 1; $ret = 1;
} }
} }
return $ret; return $ret;
} }

Loading…
Cancel
Save