php实现ffmpeg处理视频的实践

admin3年前PHP教程23

最近有一个项目需要使用ffmpeg处理视频,这里我写了一个demo,方便我们来实现视频操作

ffmpeg操作demo?

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
 
namespace common\helpers;
 
use common\models\Config;
use common\models\VideoApiLog;
use Yii;
use yii\helpers\ArrayHelper;
use common\helpers\Universal;
use yii\helpers\FileHelper;
use yii\httpclient\Client;
use yii\web\ServerErrorHttpException;
 
/**
 * ffmpeg视频处理
 *
 * @author wangjian
 * @since 0.1
 */
class FfmpegVideo
{
 
    public $ffmpeg = 'ffmpeg';
 
 
    public function __construct($ffmpeg = null)
    {
        if ($ffmpeg) {
            $this->ffmpeg = $ffmpeg;
        }
    }
 
 
    /**
     * 添加视频文字滚动
     * @param $source string 视频
     * @param $saveFile string 保存文件
     * @param $text string 水印文字
     * @param array $options 水印样式
     * @param int $step 每秒步长
     * @param int $star 出现时间
     */
    public function titleMod($source, $saveFile, $text, $options = [], $step = 20, $star = 0)
    {
        $command = $this->ffmpeg .' -y -i '. $source .' -async 1 -metadata:s:v:0 start_time=0 -vf ';
 
        $fonts = Yii::getAlias('@webroot') . "/fonts/simsun.ttc";
        $fonts = str_replace('\\', '/', $fonts);
        $fonts = str_replace(':', '\\:', $fonts);
        $command .= '"drawtext=fontfile=\''. $fonts .'\': text=\''. $text .'\'';
 
        foreach ($options as $key => $value) {
            $command .= ':' . $key . '=' . $value;
        }
 
        $command .= ':x=\'if(gte(t,'. $star .'),((t-'. $star .') * '. $step .'),NAN)\'';
 
        $command .= '" ';
        $command .= $saveFile;
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
 
    /**
     * 图片水印
     * @param $source string 视频
     * @param $saveFile string 保存文件
     * @param $waterImage string 水印图片
     * @param $left integer 水印水平位置
     * @param $top integer 水印垂直位置
     * @param null $star 水印开始时间
     * @param null $duration 水印时长
     */
    public function imageWater($source, $saveFile, $waterImage, $left, $top, $star = null, $duration = null)
    {
        $waterImage = str_replace('\\', '/', $waterImage);
        $waterImage = str_replace(':', '\\:', $waterImage);
        $command = $this->ffmpeg . ' -y -i '. $source .' -vf "movie=\''. $waterImage .'\'[watermark];';
        $command .= '[in][watermark] overlay='. $left .':'. $top;
        if ($star) {
            $end = ($duration) ? $star + $duration : $star;
            $command .= ':enable=\'between(t,'. $star .','. $end .')\'';
        }
        $command .= '[out] " ' . $saveFile;
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
 
    /**
     * 给视频添加文字水印
     * @param $source string 视频
     * @param $saveFile string 保存文件
     * @param $text string 水印文字
     * @param array $options 水印样式
     * @param null $star 水印开始时间
     * @param null $duration 水印时长
     */
    public function titleWater($source, $saveFile, $text, $options = [], $star = null, $duration = null)
    {
        $command = $this->ffmpeg .' -y -i '. $source .' -vf ';
        $fonts = Yii::getAlias('@webroot') . "/fonts/STZHONGS.TTF";
        $fonts = str_replace('\\', '/', $fonts);
        $fonts = str_replace(':', '\\:', $fonts);
        $command .= '"drawtext=fontfile=\''. $fonts .'\': text=\''. $text .'\'';
 
        foreach ($options as $key => $value) {
            $command .= ':' . $key . '=' . $value;
        }
        if ($star) {
            $end = ($duration) ? $star + $duration : $star;
            $command .= ':enable=\'between(t,'. $star .','. $end .')\'';
        }
        $command .= '" ';
        $command .= $saveFile;
 
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
    /**
     * 将音频合并到视频中
     * @param $videoFile string 视频文件
     * @param $audioFile string 音频文件
     * @param $saveFile string 保存文件
     * @param $delay integer 声音插入延时秒数
     */
    public function mergeVideoAudio($videoFile, $audioFile, $saveFile, $delay = null)
    {
        $delayTime = 0;
        if ($delay) {
            $delayTime = $delay * 1000;
        }
        $command $this->ffmpeg . ' -y -i '. $audioFile .' -i '. $videoFile .' -c:v copy -c:a aac -strict experimental -filter_complex "[0]adelay='. $delayTime .'|'. $delayTime .'[del1],[1][del1]amix" ' . $saveFile;
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
 
    /**
     * 静音
     */
    public function audioMute($source, $saveFile)
    {
        $command $this->ffmpeg . ' -y -i '. $source .' -filter:a "volume=0" ' . $saveFile;
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
 
    /**
     * 提取视频的音频
     * @param $source string 需要提取声音的视频
     * @param $saveFile string 提取声音后保存的音频
     * @return bool
     */
    public function collectAudio($source, $saveFile)
    {
        $command $this->ffmpeg . ' -y -i '. $source .' -vn -acodec copy ' . $saveFile;
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
    /**
     * 去除视频声音
     * @param $source string 需要去除声音的视频
     * @param $saveFile string 去除声音后保存的视频
     */
    public function removeAudio($source, $saveFile)
    {
        $command $this->ffmpeg . ' -y -i '. $source .' -an ' . $saveFile;
 
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
    /**
     * 视频拼接
     * @param $sources array 需要拼接的视频/音频
     * @param $saveFile string 拼接后的视频/音频
     */
    public function spliceVideo($sources, $saveFile)
    {
        $commands = [];
        $temporaryFile = [];
        $basePath = sys_get_temp_dir();
        $index = 0;
        foreach ($sources as $i => $source) {
            $file = $basePath . '/' . $i . '.ts';
            $commands[$index] = $this->ffmpeg . ' -y -i '. $source .' -vcodec copy -acodec copy -vbsf h264_mp4toannexb ' . $file;
            $temporaryFile[] = $file;
            $index++;
        }
        $commands[$index] = $this->ffmpeg . ' -y -i "concat:'. implode('|', $temporaryFile) .'"  -acodec copy -vcodec copy -absf aac_adtstoasc ' . $saveFile;
        foreach ($commands as $command) {
            exec($command, $output, $result_code);
        }
 
        foreach ($temporaryFile as $file) {
            @unlink($file);
        }
        return true;
 
    }
 
 
    /**
     * 视频剪切
     * @param $source string 需要剪切视频/音频
     * @param $saveFile string 剪切后保存视频/音频
     * @param $star string 剪切开始时间
     * @param null $duration string 剪切时长
     */
    public function clipVideo($source, $saveFile, $star, $duration = null)
    {
        $command = $this->ffmpeg . ' -y -ss '. $star;
        if ($duration) {
            $command .= ' -t '. $duration;
        }
        $command .= ' -i '. $source .' -acodec copy ' . $saveFile;
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
    const ROTATE_90 = 'transpose=1';
    const ROTATE_180 = 'hflip,vflip';
    const ROTATE_270 = 'transpose=2';
 
    /**
     * 视频旋转
     * @param $source string 需要旋转的视频
     * @param $saveFile string 旋转后视频
     * @param $rotate string 旋转角度
     */
    public function transposeVideo($source, $saveFile, $rotate)
    {
 
        $command = $this->ffmpeg . ' -y -i ' . $source . ' -vf ""transpose=1"" ' . $saveFile;
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
    /**
     * 视频转码
     * @param $source string 需要转码的视频/音频
     * @param $saveFile string 转码后的视频/音频
     */
    public function acodecVideo($source, $saveFile)
    {
        $command = $this->ffmpeg . ' -y -i '. $source .' -acodec copy -vcodec copy -f mp4 ' . $saveFile;
        exec($command, $output, $result_code);
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
    /**
     * 视频拼接
     * @param $sources array 需要拼接的视频/音频
     * @param $saveFile string 拼接后的视频/音频
     */
    public function concatVideo($sources, $saveFile)
    {
        $file = $this->createTemporaryFile();
        $fileStream = @fopen($file, 'w');
        if($fileStream === false) {
            throw new ServerErrorHttpException('Cannot open the temporary file.');
        }
 
        $count_videos = 0;
        if(is_array($sources) && (count($sources) > 0)) {
            foreach ($sources as $videoPath) {
                $line = "";
                if($count_videos != 0)
                    $line .= "\n";
                $line .= "file '". str_replace('\\','/',$videoPath) ."'";
 
                fwrite($fileStream, $line);
                $count_videos++;
            }
        }
        else {
            throw new ServerErrorHttpException('The list of videos is not a valid array.');
        }
 
        $command = $this->ffmpeg .' -y -f concat -safe 0 -i '. $file . ' -c copy ' . $saveFile;
        exec($command, $output, $result_code);
        fclose($fileStream);
        @unlink($file);//删除文件
        if ($result_code == 0) {
            return true;
        }
        return  false;
    }
 
    /**
     * 创建一个临时文件
     */
    public function createTemporaryFile()
    {
        $basePath = sys_get_temp_dir();
        if (false === $file = @tempnam($basePath, null)) {
            throw new ServerErrorHttpException('Unable to generate a temporary filename');
        }
        return $file;
    }
 
    /**
     * 获取视频信息
     * @param $source string 需要获取时长的资源
     */
    public function getAttributes($source)
    {
        ob_start();
        $command = $this->ffmpeg . ' -i "'. $source .'" 2>&1';
        passthru($command);
        $getContent = ob_get_contents();
        ob_end_clean();
        $duration = 0;
        $widht = 0;
        $height = 0;
        if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $getContent, $match)) {
            $matchs = explode(':', $match[1]);
            $duration = $matchs[0] * 3600 + $matchs[1] * 60 + $matchs[2]; //转换播放时间为秒数
        }
 
        if (preg_match("/Video: (.*?), (.*?), (.*?)[,\s]/", $getContent, $match)) {
            $matchs = explode('x', $match[3]);
            $widht = $matchs[0];
            $height = $matchs[1];
        }
 
        return [
            'duration' => intval($duration),
            'widht' => intval($widht),
            'height' => intval($height),
        ];
    }
}






使用简单示例

这里注意如果无法执行ffmpeg,实例化时需要传入ffmpeg的安装地址,例如linux下ffmpeg安装地址为/usr/local/ffmepg,那么实例化时需要传入/usr/local/ffmpeg/bin/ffmpeg

1:给视频添加文字

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ffmpeg = new FfmpegVideo();
$ffmpeg ->titleWater(
    'XXX',//原视频
    'XXX',//处理后保存视频
    'XXX',//文字
    [
        'x' => 30,//水平距离
        'y' => 30,//垂直距离
        'fontsize' => 20,//文字大小
        'fontcolor' => 'red',//文字颜色
        'shadowy' => 2,//文字阴影
    ],
    200,//每秒移动步长
    2//文字出现时间(秒)
);






2:将视频设为静音

?

1
2
3
4
5
$ffmpeg = new FfmpegVideo();
$ffmpeg->audioMute(
    'XXX',//原视频
    'XXX',//处理后保存视频
);






3:视频裁剪

?

1
2
3
4
5
6
7
$ffmpeg = new FfmpegVideo();
$ffmpeg->clipVideo(
    'XXX',//原视频
    'XXX',//处理后保存视频
    0,//裁剪开始时间
    10//裁剪时长
);






4:视频拼接

?

1
2
3
4
5
$ffmpeg = new FfmpegVideo();
$ffmpeg->concatVideo(
    ['XXX', 'XXX'],//需要拼接的视频
    'XXX',//处理后保存视频
);






5:将音频合并到视频中

?

1
2
3
4
5
6
7
$ffmpeg = new FfmpegVideo();
$ffmpeg->mergeVideoAudio(
    'XXX',//视频
    'XXX',//音频
    'XXX',//处理后保存视频
    0//音频插入视频延时时间(秒)
);






6:获取视频信息(长,宽,时长)

?

1
2
3
4
$ffmpeg = new FfmpegVideo();
$ffmpeg->getAttributes(
    'XXX',//视频
);






到此这篇关于php实现ffmpeg处理视频的实践的文章就介绍到这了,更多相关php ffmpeg处理视频内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:segmentfault/a/1190000041122598

免责声明:本文内容来自用户上传并发布,站点仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。请核实广告和内容真实性,谨慎使用。

相关文章

php有序列表或数组中删除指定的值的实现代码

实现:删除给定的值之后,得到一个新的有序列表,长度-1代码:<?php/** * Created by PhpStorm. * User: wkk * Time:...

php-fpm报502问题的解决办法

搭建lnmp完lnmp环境后,测试时出现502报错,看到这个问题,我立刻想到是php-fpm没有起来,但是我用 ps -ef | grep php-fpm 截取 php-fpm 的进程,发现是有的,这...

7款本地PHP服务器环境搭建工具推荐

通常在开发PHP程序时,需要用到服务器环境来调试自己的页面。有时候仅仅是为了学习PHP程序开发,如果是出于这样的理由去购买一个空间或者服务器是完全没必要的。本文推荐7款目前主流的PHP环境搭建工具,可...

PHP对接阿里云虚拟号的实现(号码隐私保护)

博主使用的fastadmin 封装框架实现功能:AXN隐私号绑定、解绑;场景:为店铺手机号开通虚拟号,用户联系店铺展示虚拟号码;官方开放文档地址:help.aliyun/document_detail...

解析PHP中Exception异常机制

异常的基本使用当异常被抛出时,其后的代码不会继续执行,PHP 会尝试查找匹配的 "catch" 代码块。如果异常没有被捕获,而且又没用使用 set_exception_handle...

勒索攻击到底该怎么防御?美国电信高防服务器如何防御DOSS攻击?

当前,勒索攻击、僵尸网络攻击、DDos攻击、APT攻击、挖矿攻击、供应链攻击、网站攻击、电信诈骗等各种攻击手段层出不穷。勒索攻击应该是今年网络安全行业讨论最多的话题,勒索钱财或者窃取商业数据是黑产最主...