How can I get the location of my screenshot made from video with ffmpeg in Node.js?

huangapple go评论165阅读模式
英文:

How can I get the location of my screenshot made from video with ffmpeg in Node.js?

问题

这是我是如何做的。

  1. const ffmpegPath = require('@ffmpeg-installer/ffmpeg');
  2. import ffmpeg from 'fluent-ffmpeg';
  3. import path from 'path';
  4. const os = require('os');
  5. ffmpeg.setFfmpegPath(ffmpegPath.path);
  6. export const generateThumbnailFromVideo = async (mp4Buffer) => {
  7. console.log('generateThumbnailFromVideo被触发');
  8. const timePosition = '00:00:00.500';
  9. const filename = `temp/temp-${new Date().getTime()}.png`;
  10. return new Promise((resolve, reject) => {
  11. ffmpeg({
  12. source: bufferToStream(mp4Buffer)
  13. })
  14. .on('error', (err) => {
  15. console.error('发生错误: ' + err.message);
  16. reject(err);
  17. })
  18. .on('end', () => {
  19. console.log('成功生成缩略图');
  20. fs.readFile(filename, (err, data) => {
  21. if (err) {
  22. console.error('读取缩略图文件时发生错误:', err);
  23. reject(err);
  24. return;
  25. }
  26. fs.unlink(filename);
  27. uploadBuffer(data, filename, data.length)
  28. })
  29. resolve(filename);
  30. })
  31. .screenshots({
  32. timestamps: [timePosition],
  33. filename: filename,
  34. folder: 'temp/',
  35. size: '320x240',
  36. });
  37. });
  38. }

这是生成的日志。

  1. generateThumbnailFromVideo被触发
  2. 成功生成缩略图
  3. createProjectMedia被触发
  4. userId: 1
  5. projectId: 25
  6. mediaArray: [
  7. {
  8. mediaUrl: 'medias/1/1686843801535/medias_1_1684753043519_1_(1)_(4)_(1).mp4',
  9. thumbnailUrl: 'medias/1/1686843801535/medias_1_1684753043519_1_(1)_(4)_(1)_thumbnail.jpg',
  10. mediaType: 2
  11. }
  12. ]
  13. 读取缩略图文件时发生错误: [Error: ENOENT: no such file or directory, open 'temp/temp-1686843802255.png'] {
  14. errno: -2,
  15. code: 'ENOENT',
  16. syscall: 'open',
  17. path: 'temp/temp-1686843802255.png'
  18. }

它声称已经创建了截图,但我无法在任何地方找到它。尝试使用__dirnameos.tmpdir()的绝对路径,但都没有成功。它声称已经创建的截图并不存在。

有人可以帮我吗?我已经卡在这里5个小时了,没有任何进展。

另外,我已经检查了存储库根目录中的temp文件夹。它是空的。

更新:检查并确认了用于“folder”键的路由器(temp/)是正确的,因为删除该文件夹将触发错误,指示找不到文件夹。重新创建文件夹将删除此错误。但即使打印出“成功生成”的日志,图像也不会保存在该文件夹中,该文件夹在打印该日志后仍然为空。

那个fs.unlink也与此无关。删除它不会造成任何变化。

英文:

This is how I did it.

  1. const ffmpegPath = require('@ffmpeg-installer/ffmpeg');
  2. import ffmpeg from 'fluent-ffmpeg';
  3. import path from 'path';
  4. const os = require('os');
  5. ffmpeg.setFfmpegPath(ffmpegPath.path);
  6. export const generateThumbnailFromVideo = async (mp4Buffer) => {
  7. console.log('generateThumbnailFromVideo is triggered');
  8. const timePosition = '00:00:00.500';
  9. const filename = `temp/temp-${new Date().getTime()}.png`;
  10. return new Promise((resolve, reject) => {
  11. ffmpeg({
  12. source: bufferToStream(mp4Buffer)
  13. })
  14. .on('error', (err) => {
  15. console.error('An error occurred: ' + err.message);
  16. reject(err);
  17. })
  18. .on('end', () => {
  19. console.log('Thumbnail generated successfully');
  20. fs.readFile(filename, (err, data) => {
  21. if (err) {
  22. console.error('An error occurred while reading the thumbnail file:', err);
  23. reject(err);
  24. return;
  25. }
  26. fs.unlink(filename);
  27. uploadBuffer(data, filename, data.length)
  28. })
  29. resolve(filename);
  30. })
  31. .screenshots({
  32. timestamps: [timePosition],
  33. filename: filename,
  34. folder: 'temp/',
  35. size: '320x240',
  36. });
  37. });
  38. }

And this is the log came up.

  1. generateThumbnailFromVideo is triggered
  2. Thumbnail generated successfully
  3. createProjectMedia is triggered
  4. userId: 1
  5. projectId: 25
  6. mediaArray: [
  7. {
  8. mediaUrl: 'medias/1/1686843801535/medias_1_1684753043519_1_(1)_(4)_(1).mp4',
  9. thumbnailUrl: 'medias/1/1686843801535/medias_1_1684753043519_1_(1)_(4)_(1)_thumbnail.jpg',
  10. mediaType: 2
  11. }
  12. ]
  13. An error occurred while reading the thumbnail file: [Error: ENOENT: no such file or directory, open 'temp/temp-1686843802255.png'] {
  14. errno: -2,
  15. code: 'ENOENT',
  16. syscall: 'open',
  17. path: 'temp/temp-1686843802255.png'
  18. }

It claims the screenshot has been created but I cannot find it anywhere. Tried absolute route with __dirname and os.tmpdir() with no luck. The screenshot it claimed has been created are not there.

Can somebody help me out? I have been stuck here for 5 hours with no progress so far.

Also, I have checked the file temp in the root directory of the repo. It is empty.

UPDATE: Checked and can confirm that the router used in the key "folder" (temp/) is correct, since deleting that folder will trigger an error saying that the folder is not found. Creating the folder again will remove this error. But even though the "generated successfully" log is printed, the image is not saved in that folder, and the folder is still empty after that log is printed.

That fs.unlink is also unrelated. Removing it won't cause any change.

答案1

得分: 1

为使此工作正常,您需要执行两个操作。

首先,将ffmpeg函数的参数从流更改为文件路径。
ffmpeg({source: bufferToStream(mp4Buffer)}) => ffmpeg("abc.mp4")
因为根据文档的说明,

它不适用于输入流

其次,在screenshot方法中删除folder属性,因为您已经在filename变量中指定了指定的文件夹。

  1. const filename = `temp/temp-${new Date().getTime()}.png`;
  2. 并且您的`screenshot`方法变为
  3. ```javascript
  4. .screenshots({
  5. timestamps: [timePosition],
  6. filename: filename,
  7. size: '320x240',
  8. });
英文:

To make this work, you are required to do two things.

First, change the argument of ffmpeg fuction, from a stream to a file path.
ffmpeg({source: bufferToStream(mp4Buffer)}) => ffmpeg("abc.mp4").
Because according to docs,

> It will not work on input streams

Second, remove the folder property from screenshot method, as you've already specified designated folder in filename variable.

  1. const filename = `temp/temp-${new Date().getTime()}.png`;

and your screenshot method becomes:

  1. .screenshots({
  2. timestamps: [timePosition],
  3. filename: filename,
  4. size: '320x240',
  5. });

huangapple
  • 本文由 发表于 2023年6月15日 23:47:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483404.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定