先給電腦安裝node環(huán)境
node官網(wǎng)地址:https://nodejs.org/en/
安裝完成之后可以將node配置到系統(tǒng)環(huán)境變量中胁住,
最后再檢驗自己node環(huán)境是否ok
? ~ node -v
v12.18.2
下載ffmpeg的安裝包
官網(wǎng)地址:https://ffmpeg.org/
mac下載地址:https://evermeet.cx/ffmpeg/
windows下載地址:https://www.gyan.dev/ffmpeg/builds/
準備好腳本 全部代碼如下
var fs = require('fs');
var path = require('path'); //解析需要遍歷的文件夾
const ffmpeg = require('fluent-ffmpeg')
let cp = require('child_process');
console.log('準備開始處理')
var filePath = path.resolve('./vlist');
//調(diào)用文件遍歷方法
fileDisplay(filePath);
//文件遍歷方法
function fileDisplay(filePath) {
//根據(jù)文件路徑讀取文件,返回文件列表
fs.readdir(filePath, function(err, files) {
if (err) {
console.warn(err)
} else {
//遍歷讀取到的文件列表
files.forEach(function(filename) {
console.log('文件名:', filename);
//獲取當前文件的絕對路徑
var filedir = path.join(filePath, filename);
console.log('文件路徑:', filename);
if (filename.indexOf('DS_Store') == -1) {
//這里預設的是 將轉(zhuǎn)換的圖片放在當前目錄的images文件夾里 文件名是jpg后綴
execJpg(filedir, `${filePath}/images/${filename.split('.')[0]}.jpg`)
}
});
}
});
}
//提取視頻第一幀 保存為圖片
//pathFile:原文件地址
//saveFilePath:需要保存的文件地址
const execJpg = (pathFile, saveFilePath) => {
//這個指令也能轉(zhuǎn)
// const aa = `./ffmpeg -i ${pathFile} -y -f image2 -frames 1 ${saveFilePath}`
// ./ffmpeg -ss 00:00:00 -t 00:00:01 -i f9.mp4 -an -f image2 -s 640:1136 a123.jpeg
const bb = `./ffmpeg -ss 00:00:00 -t 00:00:01 -i ${pathFile} -an -f image2 -s 640:1136 ${saveFilePath}`
console.log('當前指令:', bb)
cp.exec(bb,
function(res) {
console.log('執(zhí)行的結(jié)果:', JSON.stringify(res))
console.log(`${saveFilePath} success...`)
})
}
console.log('處理結(jié)束')
準備需要轉(zhuǎn)換的mp4文件列表
將腳本文件刊咳、ffmpeg彪见、 mp4文件夾放在同一個文件夾下
命令行切換到該目錄 并配置運行環(huán)境
? ~ cd /Users/jalen/Desktop/inv/ffmpeg
腳本中有用到fluent-ffmpeg,所以需要安裝下
npm install fluent-ffmpeg
? vlist npm install fluent-ffmpeg
npm WARN saveError ENOENT: no such file or directory, open '/Users/jalen/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/Users/jalen/package.json'
npm WARN svg-inline-react@1.0.3 requires a peer of react@^0.14.0 || ^15.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN uview@0.1.0 requires a peer of vue@^2.4.1 but none is installed. You must install peer dependencies yourself.
npm WARN jalen No description
npm WARN jalen No repository field.
npm WARN jalen No README data
npm WARN jalen No license field.
+ fluent-ffmpeg@2.1.2
added 3 packages from 3 contributors in 3.668s
6 packages are looking for funding
run `npm fund` for details
╭────────────────────────────────────────────────────────────────╮
│ │
│ New major version of npm available! 6.14.11 → 8.19.2 │
│ Changelog: https://github.com/npm/cli/releases/tag/v8.19.2 │
│ Run npm install -g npm to update! │
│ │
╰────────────────────────────────────────────────────────────────╯
? vlist
執(zhí)行腳本命令
? ffmpeg node transVideo2Jpg.js
準備開始處理
處理結(jié)束
文件名: .DS_Store
文件路徑: .DS_Store
文件名: a1.mp4
文件路徑: a1.mp4
當前指令: ./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/a1.mp4 -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/a1.jpg
文件名: a2.mp4
文件路徑: a2.mp4
當前指令: ./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/a2.mp4 -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/a2.jpg
文件名: a3.mp4
文件路徑: a3.mp4
當前指令: ./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/a3.mp4 -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/a3.jpg
文件名: a4.mp4
文件路徑: a4.mp4
當前指令: ./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/a4.mp4 -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/a4.jpg
文件名: images
文件路徑: images
當前指令: ./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/images -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/images.jpg
執(zhí)行的結(jié)果: {"killed":false,"code":1,"signal":null,"cmd":"./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/images -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/images.jpg"}
/Users/jalen/Desktop/inv/ffmpeg/vlist/images/images.jpg success...
執(zhí)行的結(jié)果: {"killed":false,"code":234,"signal":null,"cmd":"./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/a4.mp4 -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/a4.jpg"}
/Users/jalen/Desktop/inv/ffmpeg/vlist/images/a4.jpg success...
執(zhí)行的結(jié)果: {"killed":false,"code":234,"signal":null,"cmd":"./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/a3.mp4 -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/a3.jpg"}
/Users/jalen/Desktop/inv/ffmpeg/vlist/images/a3.jpg success...
執(zhí)行的結(jié)果: {"killed":false,"code":234,"signal":null,"cmd":"./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/a2.mp4 -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/a2.jpg"}
/Users/jalen/Desktop/inv/ffmpeg/vlist/images/a2.jpg success...
執(zhí)行的結(jié)果: {"killed":false,"code":234,"signal":null,"cmd":"./ffmpeg -ss 00:00:00 -t 00:00:01 -i /Users/jalen/Desktop/inv/ffmpeg/vlist/a1.mp4 -an -f image2 -s 640:1136 /Users/jalen/Desktop/inv/ffmpeg/vlist/images/a1.jpg"}
/Users/jalen/Desktop/inv/ffmpeg/vlist/images/a1.jpg success...
完結(jié)撒花娱挨,歡迎老鐵們點贊加關(guān)注呀~~~余指。