视频处理工具

ffmpeg非常好用

Win10 下安装

https://github.com/BtbN/FFmpeg-Builds/releases
windows 压缩包,解压,配置好环境路径

改变视频分辨率

$ ffmpeg \
-i input.mp4 \
-vf scale=480:-1 \
output.mp4
cd /d D:\exercise\mp4
ffmpeg -i 1.mp4 -vf scale=1280:-1 1280.mp4

ffmpeg scale
如果想要保持宽高比,那么我们需要先手动固定一个元素
,比如宽度,或者高度,然后另外一个视情况而定。用下面的写法:

ffmpeg -i input.jpg -vf scale=320:-1 output_320.png

https://zhuanlan.zhihu.com/p/143268167

nodejs 批量处理文件


/**
 * 文件说明:
 * 
 *     遍历出所有的视频文件出来,压缩,生成新的视频文件,再删除了旧的视频文件
 *      
 * 运行:
 *     node compress-video
 * 
 */

var fs = require('fs');
var path = require('path');

var exec = require('child_process').exec;


function walkSync(currentDirPath, callback) {
   fs.readdirSync(currentDirPath, { withFileTypes: true }).forEach(function(dirent) {
        var filePath = path.join(currentDirPath, dirent.name);
        if (dirent.isFile()) {
            callback(filePath, dirent);
        } else if (dirent.isDirectory()) {
            walkSync(filePath, callback);
        }
   });
}

var resDir = `.`;

var arrCmd = [];

walkSync(resDir, function(filePath, stat) {

    var isVideo = (filePath.indexOf(".mp4") >= 0);

    if (!isVideo) {
        return ;
    }

    var dirName = path.dirname(filePath);
    var fileName = path.basename(filePath);

    var newFilePath = dirName + "/min-" + fileName;

    var cmd = "ffmpeg -i \"" + filePath +"\" -vf scale=1280:-1 \"" + newFilePath +"\"";

    var obj = {};

    obj.cmd = cmd;
    obj.oldFilePath = filePath;
    obj.newFilePath = newFilePath;

    arrCmd.push(obj);

    console.log(cmd);


});

console.log("-----------");

arrCmd.forEach((obj) => {
    let cmd = obj.cmd;
    let oldFilePath = obj.oldFilePath;
    exec(cmd, function(error, stdout, stderr) {
        if(error){
            console.error(error);
        } else {
            var msg = cmd + " success!";
            console.log(msg);

            fs.unlinkSync(oldFilePath);
        }
    });
});

posted @   lvye1221  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示