您的位置:首页 > 运维架构 > Linux

Linux下使用convert对图片缩放和使用ffmpeg对视频进行转换

2017-11-24 23:12 801 查看
鉴于工作需要,研究了一下Linux的convert命令和ffmpeg命令

一、使用convert命令实现图片缩放

工作需求:在用户上传图片和视频时进行异步处理,如果上传的是图片,生成一张中等比例的图片和一张小的缩略图。最终的结果,存储一张原图,一张中等比例的图片,一张缩略图。如果上传的是视频,需要将视频格式统一转化为mp4格式。

上传图片和视频使用的是百度的在线编辑器ueditor。对其上传类Uploader.class.php进行修改,具体操作如下

/**
* 上传文件的主处理方法
* @return mixed
*/
private function upFile() {
$file = $this->file = $_FILES[$this->fileField];
if (!$file) {
$this->stateInfo = $this->getStateInfo("ERROR_FILE_NOT_FOUND");
return;
}
if ($this->file['error']) {
#error_log(json_encode($file), 3, "./logs/error.log");
#error_log(json_encode($this->file), 3, "./logs/error.log");
$this->stateInfo = $this->getStateInfo($file['error']);
return;
} else if (!file_exists($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMP_FILE_NOT_FOUND");
return;
} else if (!is_uploaded_file($file['tmp_name'])) {
$this->stateInfo = $this->getStateInfo("ERROR_TMPFILE");
return;
}

$this->oriName = $file['name'];
$this->fileSize = $file['size'];
$this->fileType = $this->getFileExt();
$this->fullName = $this->getFullName();
$this->filePath = $this->getFilePath();
$this->fileName = $this->getFileName();
$dirname = dirname($this->filePath);

//检查文件大小是否超出限制
if (!$this->checkSize("upFile")) {
$this->stateInfo = $this->getStateInfo("ERROR_SIZE_EXCEED");
return;
}

//检查是否不允许的文件格式
if (!$this->checkType()) {
$this->stateInfo = $this->getStateInfo("ERROR_TYPE_NOT_ALLOWED");
return;
}

//创建目录失败
if (!file_exists($dirname) && !mkdir($dirname, 0777, true)) {
$this->stateInfo = $this->getStateInfo("ERROR_CREATE_DIR");
return;
} else if (!is_writeable($dirname)) {
$this->stateInfo = $this->getStateInfo("ERROR_DIR_NOT_WRITEABLE");
return;
}

//移动文件
//if (!(move_uploaded_file($file["tmp_name"], $this->filePath) && file_exists($this->filePath))) { //移动失败
$tmp_content = file_get_contents($file["tmp_name"]);
$newfile = $this->_compress($tmp_content);
if (!(file_put_contents($this->filePath, $newfile) && file_exists($this->filePath))) { //移动失败
$this->stateInfo = $this->getStateInfo("ERROR_FILE_MOVE");
} else { //移动成功
//异步处理上传图片文件
if(in_array($this->fileType,array('.png','.jpg','.jpeg','.bmp'))){
$getSize=getimagesize($this->filePath);
$middleSize=$getSize[1] * (750 / $getSize[0]);
$smallSize=$getSize[1] * (360 / $getSize[0]);
$srcName=basename($this->fullName);
$cmd = "sh ../shell/deal.sh $srcName $middleSize $smallSize >>logs/syn_upload.log  &";
exec($cmd);
}//异步转换视频为mp4格式
            if(in_array($this->fileType,array('.mp4','.mov','.avi'))){
                $srcName=basename($this->fullName);
                $cmd = "sh ../shell/deal_video.sh $srcName >>logs/syn_upload_video.log  &";
                exec($cmd);
            }
$this->stateInfo = $this->stateMap['SUCCESS'];
}
}

//deal.sh(处理图片的shell脚本)
#!/bin/bash
DATE=$(date +%Y%m%d);
newext='jpg';
for file in ../images/${DATE}/$1; do
echo ${file};
#name=$(ls $file | cut -d. -f1);
ext=${file##*.};
name=$(basename ${file} .${ext});
#echo $name;exit;
if [ $ext = 'png' ];then
ext=${newext};
fi
to_middle_file="../images/$DATE/${name}_m.${ext}";
to_small_file="../images/$DATE/${name}_s.${ext}";
convert -resize 750x$2! -quality 80 ${file} ${to_middle_file};
ls ${to_middle_file};
convert -resize 360x$3! ${file} ${to_small_file};
ls ${to_small_file};
done
//deal_video.sh(处理视频的shell脚本)
#!/bin/bash
DATE=$(date +%Y%m%d);
newext='mp4';
for file in ../video/${DATE}/$1; do
 #echo ${file};
 #name=$(ls $file | cut -d. -f1);
 ext=${file##*.};
 name=$(basename ${file} .${ext});
 #echo $name;
 if [ $ext != 'mp4' ];then
    ext=${newext};
    to_new_file="../video/$DATE/${name}.${ext}";
    #echo $to_new_file;exit;
    ffmpeg -i ${file} -vcodec copy -acodec copy ${to_new_file};
    ls ${to_new_file};
 fi
done
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息