这是之前开发的一个项目的时候写的,脚本批量将目录里的视频批量的截图 ,用到了 ffmpeg 转码工具
截图脚本事例:

#!/bin/bash

run_ls(){
        pwd;

        for x in $(ls -l| grep ".mp4$" | awk '{print $9}')
        do
            echo $x
            ffmpeg -y -i $x -frames 1 -f image2 $x".jpg"
        done
}

myfunc(){
        cd $1;
        #run_make;
        run_ls;

        for x in $(ls .)
        do
                if [ -d "$x" ];then
                        myfunc $x;
                        cd ..
                fi

        done
}

p=.
if [ $1 ];then
    p=$1
fi
echo $p

myfunc $p

转码并上传到OSS:

<?php /**
 * ===================================
 * @author      Joyber
 * @date        04/26/2018
 * ===================================
 */

namespace app\api\sell;

use OSS\OssClient;
use util\Redis;
use wumashi\core\Conf;
use wumashi\core\Db;

require "./_shell.php";

include_once ROOT . 'vendor/aliyun/core/Config.php';
include_once ROOT . 'vendor/aliyun/aliyun-oss.phar';

set_time_limit(0);
define('UPLOAD_PREFIX', 'http://file.www.cn/');
define('UPLOAD_PATH', '/data/upload/');
define('USLEEP_TIME', 4e5);


if (!function_exists('exec')) {
    outmsg('ERROR: exec function not exists');
    exit(1);
}

$config = Conf::get('app', 'alioss');
//var_dump($config);
//$clientProfile = \DefaultProfile::getProfile(...$config);

$ossClient = new OssClient($config['accessKey'], $config['accessSecret'], $config['endpoint']);
$redis = Redis::getInstance();
$db = Db::getInstance();

//上传文件
//$ossClient->putObject($config['bucket'], 'test/test.bat', ROOT . 'wepy.bat');

//列举文件
//$option = ['prefix'=>'20180427/46c12c6858875fbab7c0956ab1a9ab28.mp4'];
//$listObjectInfo = $ossClient->listObjects($config['bucket'], $option);
//$objectList = $listObjectInfo->getObjectList();
//if (!empty($objectList)) {
//    foreach ($objectList as $objectInfo) {
//        print($objectInfo->getKey() . "\t" . $objectInfo->getSize() . "\t" . $objectInfo->getLastModified() . "\n");
//    }
//}

$key = 'task_for_ts';

function uploadDir($dirname) {
    global $config, $ossClient;
    foreach (scandir($dirname) as $file) {
        if (!in_array($file, [".", ".."])) {
            $filepath = $dirname . '/' . $file;
            if (is_dir($filepath)) {
                uploadDir($filepath);
                continue;
            }
            $object = str_replace(UPLOAD_PATH, '', $filepath);
            outmsg("upload OSS : {$object} => {$filepath}");

            $res = $ossClient->uploadFile($config['bucket'], $object, $filepath);
            outmsg($res);
        }
    }
};

while (true) {

    if (time() > WMS_TIME + 1800) {
        exit(1); //退出为1,无限重启,0 将不再启动
    }


    $filename = $redis->lPop($key);
    if (!$filename) {
        usleep(USLEEP_TIME);
        continue;
    }

    if (!file_exists($filename)) {
        outmsg('file not exists: '. $filename);
        usleep(USLEEP_TIME);
        continue;
    }

    //执行转码任务
    $pathinfo = pathinfo($filename);
    $dir = $pathinfo['dirname'] . '/' . $pathinfo['filename'];
    if (file_exists($dir)) {
        //已经转码过了,跳过
        outmsg('jump: ts dir exists: '. $dir);
        usleep(USLEEP_TIME);
        continue;
    }

    //创建目录
    mkdir($dir, 0777, true);

    $tsfilename = $dir . '/ts.m3u8';
    $picfilename = $tsfilename.'.jpg';
    $cmd = "ffmpeg -i {$filename} -profile:v baseline -level 3.0 -start_number 0 -hls_time 10 -hls_list_size 0 -f hls {$tsfilename}";
    //重新截图命令
    $piccmd = "ffmpeg -y -i {$filename} -frames 1 -f image2 {$picfilename}";
    outmsg("{$filename} => {$cmd}");

    //必需开启exec函数
    @exec($cmd, $res, $status_code);
    if ($status_code != 0) {
        //错误
        outmsg("Error res: [{$status_code}]" . serialize($res));
        $redis->lPush($key.'_retry', $filename);
        continue;
    }

    @exec($piccmd);
    $ourl = str_replace(UPLOAD_PATH, UPLOAD_PREFIX, $filename);
    $url = str_replace(UPLOAD_PATH, UPLOAD_PREFIX, $tsfilename);

    $db->insert('tsfile', [
        'md5' => md5($ourl),
        'ourl' => $ourl,
        'url' => $url,
    ]);

    //上传切片目录文件
    uploadDir($dir);

    usleep(USLEEP_TIME);
}

标签: none

添加新评论