Centos安装ffmpeg+ffplay

yum install SDL2-devel
yum install libXext-devel
./configure --disable-x86asm
make && make install

# ffmpeg: error while loading shared libraries: libavdevice.so
# export LD_LIBRARY_PATH=/usr/local/ffmpeg/lib/

php使用

<?php
/**
 * @Author: lpb
 * @Date: 2020/3/26 12:33
 * @Desc: 音频截掉后10秒
 */

class Video
{
    /**
     * 音频减10秒钟
     * @param $pre 资源目录
     * @param $after 转出目录
     */
    function doIt($pre, $after)
    {
        echo "<pre>";
        $arr = $this->getDir($pre);
        foreach ($arr as $v) {
            $seconds = $this->getContent($v)['seconds'];
            $secondsAfterFormat = $this->transitionAudioLen($seconds);
//        echo "路径: {$v} 时长: {$seconds}s", "<br>";
            $shell = "ffmpeg -i {$v} -ss 00:00:00 -t $secondsAfterFormat -acodec copy {$after}/" . basename($v) . " -y";
//        echo $shell, "<br>";
            shell_exec($shell);
        }
    }

    /**
     * 使用scandir 遍历目录
     *
     * @param $path
     * @return array
     */
    function getDir($path)
    {
        //判断目录是否为空
        if (!file_exists($path)) {
            return [];
        }

        $files = scandir($path);
        $fileItem = [];
        foreach ($files as $v) {
            $newPath = $path . DIRECTORY_SEPARATOR . $v;
            if (is_dir($newPath) && $v != '.' && $v != '..') {
                $fileItem = array_merge($fileItem, getDir($newPath));
            } else if (is_file($newPath)) {
                $fileItem[] = $newPath;
            }
        }

        return $fileItem;
    }

//获得视频文件的具体信息
    function getContent($file)
    {
        /$file为视频上传目录
        $command = sprintf('ffmpeg -i "%s" 2>&1', $file); //你的安装路径
        ob_start();
        passthru($command);
        $info = ob_get_contents();
        ob_end_clean();
        $data = array();
        $match = [];
        if (preg_match("/Duration: (.*?), start: (.*?), bitrate: (\d*) kb\/s/", $info, $match)) {
            $arr_duration = explode(':', $match[1]);
            $data['seconds'] = $arr_duration[0] * 3600 + $arr_duration[1] * 60 + $arr_duration[2]; //转换播放时间为秒数
            $data['start'] = $match[2]; //开始时间
            $data['bitrate'] = $match[3]; //码率(kb)
        }
        $match = [];
        if (preg_match("/Video: (.*?), (.*?), (\d+)x(\d+)(.*?), (\d+) kb\/s/", $info, $match)) {
            $data['vcodec'] = $match[1]; //视频编码格式
            $data['vformat'] = $match[2]; //视频格式
            $data['width'] = $match[3];
            $data['height'] = $match[4];
            $data['videorate'] = $match[6]; //视频比特率(kb)
        }
        $match = [];
        if (preg_match("/Audio: (.*?), (\d+) Hz, stereo, fltp, (\d+) kb\/s/", $info, $match)) {
            $data['acodec'] = $match[1]; //音频编码
            $data['asamplerate'] = $match[2]; //音频采样频率
            $data['audiorate'] = $match[3]; //音频比特率(kb)
        }
        if (isset($data['seconds']) && isset($data['start'])) {
            $data['play_time'] = $data['seconds'] + $data['start']; //实际播放时间
        }
        $data['size'] = filesize($file) / 1024 / 1024; //文件大小
        return ($data);
    }

    /**
     * 音频时间长度, 减10秒, 转成格式 00:10:11.
     */
    function transitionAudioLen($seconds)
    {
        $seconds -= 10;

        $hour = floor($seconds / 3600);
        $hourFormat = strlen($hour) > 1 ? $hour : (str_repeat('0', 2 - strlen($hour)) . $hour);//不足两位补两位
        $minutes = floor(($seconds - ($hour * 3600)) / 60);//分
        $minuteFormat = strlen($minutes) > 1 ? $minutes : (str_repeat('0', 2 - strlen($minutes)) . $minutes);//不足两位补两位
        $seconds = $seconds - ($hour * 3600) - ($minutes * 60);
        $secondFormat = strlen(intval($seconds)) > 1 ? $seconds : (str_repeat('0', 2 - strlen(intval($seconds))) . $seconds);//不足两位补两位

        return "{$hourFormat}:{$minuteFormat}:{$secondFormat}";
    }
}

$class = new Video();

$pre = '/home/Go_WorkSpace/old';//资源路径
$after = '/home/Go_WorkSpace/new';//转出路径

$class->doIt($pre, $after);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值