|
1
|
|
|
<?php |
|
2
|
|
|
class Nip_File_Video extends Nip_File_Handler { |
|
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
static public $extensions = array('avi', 'mp4', 'mpg', 'mpeg', 'mkv', 'm4v'); |
|
5
|
|
|
protected $_ffmpeg_video; |
|
6
|
|
|
|
|
7
|
|
|
public function convert($params = array(), $removeOriginal = true) { |
|
8
|
|
|
if (!$params['f'] || $params['f'] == $this->extension) { |
|
9
|
|
|
return; |
|
10
|
|
|
} |
|
11
|
|
|
|
|
12
|
|
|
$command = []; |
|
13
|
|
|
$command[] = '/usr/bin/ffmpeg'; |
|
14
|
|
|
$command[] = "-i ".escapeshellarg($this->path); |
|
15
|
|
|
foreach ($params as $key => $value) { |
|
16
|
|
|
$command[] = "-$key ".escapeshellarg($value); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
$path = dirname($this->path) .DIRECTORY_SEPARATOR. pathinfo($this->path, PATHINFO_FILENAME) . '.' . strtolower($params['f']); |
|
20
|
|
|
$command[] = $path; |
|
21
|
|
|
|
|
22
|
|
|
$command = implode(" ", $command) . " && chmod 777 $path".($removeOriginal ? " && rm $this->path" : ""); |
|
23
|
|
|
|
|
24
|
|
|
$process = new Process($command); |
|
25
|
|
|
$process->start(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function saveRandomFrame($dir, $width = false, $height = false) { |
|
29
|
|
|
/* @var $frame ffmpeg_frame */ |
|
30
|
|
|
$frame = $this->getRandomFrame(); |
|
31
|
|
|
$image = new Image_VideoFrame(); |
|
32
|
|
|
$image->setFFmpegFrame($frame); |
|
33
|
|
|
|
|
34
|
|
|
if (!$width) { |
|
35
|
|
|
$width = $frame->getWidth(); |
|
36
|
|
|
} |
|
37
|
|
|
if (!$height) { |
|
38
|
|
|
$height = $frame->getHeight(); |
|
39
|
|
|
} |
|
40
|
|
|
$image->cropToCenter($width, $height); |
|
41
|
|
|
$image->unsharpMask(); |
|
42
|
|
|
|
|
43
|
|
|
$filename = explode(".", basename($this->path)); |
|
44
|
|
|
array_pop($filename); |
|
45
|
|
|
$filename[] = "jpg"; |
|
46
|
|
|
$filename = implode(".", $filename); |
|
47
|
|
|
$image->path = $dir . '/' . $filename; |
|
48
|
|
|
$image->save(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getRandomFrame() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->getFFmpegVideo()->getFrame(rand(1, $this->getFrameCount())); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
protected function getFFmpegVideo() |
|
57
|
|
|
{ |
|
58
|
|
|
if (!$this->_ffmpeg_video) { |
|
59
|
|
|
$this->_ffmpeg_video = new ffmpeg_movie($this->path); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $this->_ffmpeg_video; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getFrameCount() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->getFFmpegVideo()->getFrameCount(); |
|
68
|
|
|
} |
|
69
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.