Video.class.php
4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
class Lib_Video
{
private static $height = 0;
private static $width = 0;
/**
* 获取视频缩略图
*
* @param string $file
* @return string
*/
public static function getVideoThumb($file)
{
$realPath = '';
$path = pathinfo($file);
$thumbPic = sprintf('%s.jpg', $path['dirname'].'/'.md5(uniqid()));
$command = 'ffmpeg -y -i '.escapeshellcmd($file);
$command .= ' -f image2 -vframes 1 '.$thumbPic;
exec($command, $result , $retval);
if(empty($retval))
{
$size = getimagesize($thumbPic);
self::$height = $size[1];
self::$width = $size[0];
$realPath = Lib_Images::saveImage($thumbPic);
if(empty($realPath))
{
$realPath = '';
}
//上传图片
@unlink($thumbPic);
}
return $realPath;
}
/***
* 裁剪视频
*
* @param string $file
* @param int $top
* @param int $bottom
* @param int $left
* @param int $right
* @return boolean
*/
public static function cropVideo($file, $top = 0, $bottom = 0, $left = 0, $right = 0)
{
$oldSize = filesize($file);
$command = 'ffmpeg -y -i '.escapeshellcmd($file);
$command.= sprintf(" -croptop %d -cropbotom %d -cropleft %d -cropright %d %s",
$top, $top, $bottom, $left, $right, escapeshellcmd($file));
exec($command, $result , $retval);
$newSize = filesize($file);
if(empty($retval) && $newSize<$oldSize)
{
return true;
}
else
{
return false;
}
}
/**
* 静音
*
* @param string $file
* @return boolean
*/
public static function muteVideo($file)
{
$command = sprintf('ffmpeg -y -i %s -an %s', escapeshellcmd($file), escapeshellcmd($file));
exec($command, $result , $retval);
if(empty($retval))
{
return true;
}
else
{
return false;
}
}
/**
* 旋转视频90度
*
* @param string $file
* @return boolean
*/
public static function rotateVideo($file)
{
$path = pathinfo($file);
$tmpVideo = sprintf('%s.mp4', $path['dirname'].'/'.md5(uniqid()));
$command = 'mencoder -fps 30000/1001 -ofps 30000/1001 -ovc x264 -vf rotate=1 ';
$command .= ' -oac faac -srate 24000 -faacopts mpeg=4:object=2:raw:br=64 -of lavf ';
$command .= ' -lavfopts format=mp4 -x264encopts nocabac:threads=1024 ';
$command .= sprintf('%s -o %s ',escapeshellcmd($file), escapeshellcmd($tmpVideo));
file_put_contents('/tmp/video.txt', $command);
exec($command, $result , $retval);
if(empty($retval))
{
@rename($tmpVideo, $file);
@unlink($tmpVideo);
return true;
}
else
{
return false;
}
}
/**
* 保存视频
*
* @param string $file
* @return string {url:string, video_url:string}
*/
public static function saveVideo($file)
{
$path = Config_File_Video::$videoServer['video'].Config_File_Upload::$video['path'];
$tmpFile = current($file);
$videoKey = key($file);
//$cpFile = sprintf("%s/%s",dirname($tmpFile['tmp_name']), md5(uniqid()));
//@copy($tmpFile['tmp_name'], $cpFile);
$up = new Util_Upload_File($file, $path);
$up->setFormat(Config_File_Upload::$video['format']);
$up->setMaxFileSize(Config_File_Upload::$video['upload_max_size']);
$up->setSuffix('mp4');
$checkStatus = $up->check();
$video = '';
if($checkStatus['code'] == 0)
{
$checkStatus = $up->write ();
$video = $checkStatus ['result'] ['hit'][$videoKey]['relaPath'];
$pic = '';
$times = 10;
/*while(empty($pic) && $times > 0)
{
$pic = self::getVideoThumb($cpFile);
$times --;
}*/
// @unlink($cpFile);
return self::encode($video, $pic);
}
else
{
return '';
}
}
/**
* 获取视频地址
*
* @param string $video
* @return string
*/
public static function getVideoUrl($video)
{
return VIDEO_SITE_DOMAIN.$video;
}
/**
* 格式化视频数据
*
* @param string $video_url
* @param string $url
* @return string
*/
public static function encode($video_url, $url)
{
$data = array('video_url'=> $video_url, 'url'=> $url);
return Util_Json::encode($data);
}
/**
* 返回格式化视频数据 [{url:string, video_url:string}]
*
* @param string $data
* @return array
*/
public static function decode($data)
{
$data = Util_Json::decode($data, true);
if(isset($data['video_url']))
{
$data['video_url'] = self::getVideoUrl($data['video_url']);
}
else
{
$data = array('video_url'=>'', 'url'=> $data);
}
return $data;
}
}