From f6f2bbae11330c55da4bac38cbe82d8ee8b1af51 Mon Sep 17 00:00:00 2001 From: drawcall Date: Wed, 24 Jul 2024 11:34:32 +0800 Subject: [PATCH] feat: Modify the usage from ffmpeg-probe * Modify the usage from ffmpeg-probe to ffmpeg.ffprobe approach. * Add a setFFPath method to set the path to ffmpeg on your machine. * Increase the number of catch logs for better error handling and debugging. --- CHANGELOG.md | 5 +++++ examples/gif.js | 2 ++ lib/center/center.js | 4 ++++ lib/creator.js | 4 ++++ lib/node/gif.js | 7 ++++--- lib/node/subtitle.js | 4 ++-- lib/node/video.js | 4 ++-- lib/utils/ffmpeg.js | 15 +++++++++++---- lib/utils/ffprobe.js | 33 +++++++++++++++++++++++++++++++++ package.json | 4 +++- types/lib/FFCreator.d.ts | 2 ++ types/lib/FFCreatorCenter.d.ts | 2 ++ types/lib/FFmpegUtil.d.ts | 2 ++ 13 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 lib/utils/ffprobe.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 821506b6..1fda59e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## v7.5.1 +* Modify the usage from ffmpeg-probe to ffmpeg.ffprobe approach. +* Add a setFFPath method to set the path to ffmpeg on your machine. +* Increase the number of catch logs for better error handling and debugging. + ## v7.3.5 * Add FFmpegUtil captureVideoFrame function. * Add FFmpegUtil convertVideoToGif function. diff --git a/examples/gif.js b/examples/gif.js index d07f675b..2bd36de2 100644 --- a/examples/gif.js +++ b/examples/gif.js @@ -3,6 +3,8 @@ const colors = require('colors'); const startAndListen = require('./listen'); const { FFCreatorCenter, FFAudio, FFScene, FFImage, FFGifImage, FFCreator } = require('../'); +FFCreator.setFFPath(); + const createFFTask = () => { const bg1 = path.join(__dirname, './assets/imgs/bg/06.jpg'); const bg2 = path.join(__dirname, './assets/imgs/bg/01.jpeg'); diff --git a/lib/center/center.js b/lib/center/center.js index 2b78e0fe..38727c33 100755 --- a/lib/center/center.js +++ b/lib/center/center.js @@ -288,6 +288,10 @@ const FFCreatorCenter = { setFFprobePath(path) { FFmpegUtil.setFFprobePath(path); }, + + setFFPath() { + FFmpegUtil.setFFPath(); + } }; module.exports = FFCreatorCenter; diff --git a/lib/creator.js b/lib/creator.js index 1dcfd15c..e8fe5620 100755 --- a/lib/creator.js +++ b/lib/creator.js @@ -436,6 +436,10 @@ class FFCreator extends FFCon { static setFFprobePath(path) { FFmpegUtil.setFFprobePath(path); } + + static setFFPath() { + FFmpegUtil.setFFPath(); + } } module.exports = FFCreator; diff --git a/lib/node/gif.js b/lib/node/gif.js index 5e277893..50957599 100644 --- a/lib/node/gif.js +++ b/lib/node/gif.js @@ -14,10 +14,10 @@ const path = require('path'); const FS = require('../utils/fs'); const FFImage = require('./image'); -const probe = require('ffmpeg-probe'); const { Sprite, Texture } = require('inkpaint'); const FFLogger = require('../utils/logger'); const FFmpegUtil = require('../utils/ffmpeg'); +const ffprobe = require('../utils/ffprobe'); const Materials = require('../utils/materials'); const TimelineUpdate = require('../timeline/update'); @@ -68,7 +68,8 @@ class FFGifImage extends FFImage { const info = await this.geGifInfo(); fps = Number(info.fps); } catch (e) { - console.log(this.getPath(), e); + console.log(this.getPath()); + console.log(e); } return await this.extractGif(fps); } @@ -139,7 +140,7 @@ class FFGifImage extends FFImage { * @private */ async geGifInfo() { - return await probe(this.getPath()); + return await ffprobe(this.getPath()); } /** diff --git a/lib/node/subtitle.js b/lib/node/subtitle.js index bb6060a0..53db2c3f 100755 --- a/lib/node/subtitle.js +++ b/lib/node/subtitle.js @@ -12,10 +12,10 @@ * @class */ const FFNode = require('./node'); -const probe = require('ffmpeg-probe'); const forEach = require('lodash/forEach'); const FFTween = require('../animate/tween'); const DateUtil = require('../utils/date'); +const ffprobe = require('../utils/ffprobe'); const CanvasUtil = require('../utils/canvas'); const Materials = require('../utils/materials'); const TimelineUpdate = require('../timeline/update'); @@ -208,7 +208,7 @@ class FFSubtitle extends FFNode { async preProcessing() { if (this.conf.speech) { - this.materials.info = await probe(this.conf.speech); + this.materials.info = await ffprobe(this.conf.speech); const duration = this.materials.getDuration(); this.setDuration(duration); return null; diff --git a/lib/node/video.js b/lib/node/video.js index 9bf6a0a9..02ba2393 100755 --- a/lib/node/video.js +++ b/lib/node/video.js @@ -20,10 +20,10 @@ const path = require('path'); const min = require('lodash/min'); const FS = require('../utils/fs'); const FFImage = require('./image'); -const probe = require('ffmpeg-probe'); const FFAudio = require('../audio/audio'); const DateUtil = require('../utils/date'); const FFLogger = require('../utils/logger'); +const ffprobe = require('../utils/ffprobe'); const FFmpegUtil = require('../utils/ffmpeg'); const Materials = require('../utils/materials'); const TimelineUpdate = require('../timeline/update'); @@ -350,7 +350,7 @@ class FFVideo extends FFImage { } async getVideoInfo() { - return await probe(this.getPath()); + return await ffprobe(this.getPath()); } getVOutput() { diff --git a/lib/utils/ffmpeg.js b/lib/utils/ffmpeg.js index 2adc667e..9b26d952 100755 --- a/lib/utils/ffmpeg.js +++ b/lib/utils/ffmpeg.js @@ -9,12 +9,14 @@ * * @object */ -const path = require('path'); const siz = require('siz'); -const Utils = require('./utils'); +const path = require('path'); +const ffmpeg = require('fluent-ffmpeg'); const isArray = require('lodash/isArray'); const forEach = require('lodash/forEach'); -const ffmpeg = require('fluent-ffmpeg'); +const ffmpegInstaller = require('@ffmpeg-installer/ffmpeg'); +const ffprobeInstaller = require('@ffprobe-installer/ffprobe'); +const Utils = require('./utils'); const FFmpegUtil = { /** @@ -51,6 +53,11 @@ const FFmpegUtil = { ffmpeg.setFfprobePath(path); }, + setFFPath() { + ffmpeg.setFfmpegPath(ffmpegInstaller.path); + ffmpeg.setFfprobePath(ffprobeInstaller.path); + }, + /** * Set a water mark for swpan stdin * @@ -148,7 +155,7 @@ const FFmpegUtil = { ffmpeg(input) .output(output) .videoFilters([`fps=${fps}`, `scale=${width}:-1`]) - .outputOptions('-loop 0') + .outputOptions('-loop 0') .on('end', () => { resolve(output); }) diff --git a/lib/utils/ffprobe.js b/lib/utils/ffprobe.js new file mode 100644 index 00000000..2db8d1d9 --- /dev/null +++ b/lib/utils/ffprobe.js @@ -0,0 +1,33 @@ +'use strict'; + +/** + * ffprobe - a Compatible FFmpeg Probe Method + * + * ####Example: + * + * + * + * @object + */ +const probe = require('ffmpeg-probe'); +const ffmpeg = require('fluent-ffmpeg'); + +const useFluent = true; +const ffprobe = filePath => { + if (useFluent) { + return new Promise((resolve, reject) => { + ffmpeg.ffprobe(filePath, (err, metadata) => { + if (err) { + reject(err); + } else { + // console.log(metadata); + resolve(metadata); + } + }); + }); + } else { + return probe(filePath); + } +}; + +module.exports = ffprobe; diff --git a/package.json b/package.json index 714f756f..26d3c81e 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ffcreator", - "version": "7.3.6", + "version": "7.5.1", "description": "FFCreator is a lightweight and flexible short video production library", "main": "lib/index.js", "types": "types/index.d.ts", @@ -22,6 +22,8 @@ ], "license": "MIT", "dependencies": { + "@ffmpeg-installer/ffmpeg": "^1.1.0", + "@ffprobe-installer/ffprobe": "^2.1.2", "@tweenjs/tween.js": "18.5.0", "@xsstomy/subsrt": "^1.0.0", "browser-or-node": "^1.3.0", diff --git a/types/lib/FFCreator.d.ts b/types/lib/FFCreator.d.ts index a2d04324..0caa8eea 100644 --- a/types/lib/FFCreator.d.ts +++ b/types/lib/FFCreator.d.ts @@ -142,6 +142,8 @@ declare namespace FFCreatorSpace { */ static setFFprobePath(path: string): void; + static setFFPath(): void; + /** * Create new effect and add to effects object * @param name the new effect name diff --git a/types/lib/FFCreatorCenter.d.ts b/types/lib/FFCreatorCenter.d.ts index 52e2355a..8af4c206 100644 --- a/types/lib/FFCreatorCenter.d.ts +++ b/types/lib/FFCreatorCenter.d.ts @@ -98,6 +98,8 @@ declare namespace FFCreatorSpace { */ setFFprobePath(path: string): void; + setFFPath(): void; + /** * get task progress by task id * @param id diff --git a/types/lib/FFmpegUtil.d.ts b/types/lib/FFmpegUtil.d.ts index a038fcc1..0d7d7291 100644 --- a/types/lib/FFmpegUtil.d.ts +++ b/types/lib/FFmpegUtil.d.ts @@ -8,6 +8,8 @@ declare namespace FFCreatorSpace { setFFprobePath(path: string): void; + setFFPath(): void; + createCommand(conf?: { threads?: number }): any; interceptVideo(options: { video: string; ss: string; to: string; output: string }): void;