I have a function that is supposed to generate a thumbnail from a mp4 file with fluent-ffmpeg
in Node, and store it as a jpg file.
In my first function I tried to solve this by creating a stream of the external url:
const got = require('got');
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');
const fs = require('fs');
function generateThumbnail() {
const url = 'https://gateway.pinata.cloud/ipfs/QmUWD7dewFZB9bFamyvR5uEUpX1FEkjuoZYzhUZBm8U4mT/nft.mp4'
const request = await got.stream(url);
function asyncThumbnail() {
return new Promise((resolve, reject) => {
ffmpeg(request)
.setFfmpegPath(ffmpeg_static)
.screenshots({
size: '?x512',
count: 1,
timemarks: ['3'],
filename: `filename.jpg`,
folder: __dirname + '/../ffmpeg/output',
})
.on('end', function () {
resolve();
console.log('Thumbnail created');
})
.on('error', (err) => {
return reject(new Error(err));
});
});
}
}
A thumbnail is generated for a lot of videos I have tested, but not for this video (the video loads a bit slow because it's hosted on IPFS, but it doesn't have anything to do with my error), which returns the following error:
ffmpeg exited with code 1: pipe:0: Invalid data found when processing input
Cannot determine format of input stream 0:0 after EOF
After reading that ffmpeg is supposed to work better if I download a video locally before converting it (link), I changed my code to do that:
const got = require('got');
const ffmpeg = require('fluent-ffmpeg');
const ffmpeg_static = require('ffmpeg-static');
const fs = require('fs');
function generateThumbnail() {
const url = 'https://gateway.pinata.cloud/ipfs/QmUWD7dewFZB9bFamyvR5uEUpX1FEkjuoZYzhUZBm8U4mT/nft.mp4'
const request = await got.stream(url);
await request.pipe(
fs.createWriteStream(
__dirname + `/../ffmpeg/input/fileName.mp4`
)
);
function asyncThumbnail() {
return new Promise((resolve, reject) => {
ffmpeg(__dirname + `/../ffmpeg/input/filename.mp4`)
.setFfmpegPath(ffmpeg_static)
.screenshots({
size: '?x512',
count: 1,
timemarks: ['3'],
filename: `filename.jpg`,
folder: __dirname + '/../ffmpeg/output',
})
.on('end', function () {
resolve();
console.log('Thumbnail created');
})
.on('error', (err) => {
return reject(new Error(err));
});
});
}
await asyncThumbnail();
}
This gives me a similar error, but for every video I have tested, without generating a single thumbnail:
ffmpeg exited with code 1: C:\path\src/../ffmpeg/input/baroque-fndnft-945.mp4: Invalid data found when processing input
Running the last function with fs.createReadStream()
as the ffmpeg()
input istead gives me this error:
ffmpeg exited with code 1: pipe:0: Invalid data found when processing input