File size: 2,576 Bytes
3890c2b
71e6036
3890c2b
 
71e6036
8b74733
 
3890c2b
 
 
 
 
 
 
5a9be52
3890c2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b74733
 
3890c2b
8b74733
 
 
 
 
3890c2b
8b74733
3890c2b
8b74733
 
 
 
 
 
3890c2b
8b74733
 
 
 
 
 
 
 
 
 
 
 
3890c2b
8b74733
 
 
 
 
 
 
 
3890c2b
8b74733
 
3890c2b
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
_id: video_processing
version: 2
title: en=Video processing;ru=Работа с видео
description: Using FFmpeg process videos
readme: "Fix ffmpeg fluent"
author: Anton Breslavskii | https://github.com/breslavsky
url: https://huggingface.co/PiperMy/Node-Packages/resolve/main/video_processing.yaml
nodes:
  split_video_by_frames:
    _id: split_video_by_frames
    arrange:
      x: 280
      y: 110
    category:
      _id: split_video_by_frames
      title: en=Video processing;ru=Обработка видео
    costs: ""
    environment: {}
    inputs:
      video:
        order: 1
        title: Source video
        type: video
        required: true
      fps:
        order: 2
        title: Frame rate
        type: integer
        min: 1
        max: 60
        step: 1
        default: 12
    outputs:
      frames:
        title: Frames
        type: image[]
    package: video_processing
    script: |-
      export async function run({ inputs }) {
          const { video, fps } = inputs;

          const { FatalError, NextNode } = DEFINITIONS;
          const ffmpeg = require('fluent-ffmpeg');
          const path = require('path');
          const fs = require('fs/promises');
          const { fileTypeFromBuffer } = require('file-type');

          const frames = [];

          await useTempFolder(async (tmpFolder) => {

              const { data } = await download(video);
              const { ext } = await fileTypeFromBuffer(data);
              const videoPath = path.join(tmpFolder, `source.${ext}`);
              await fs.writeFile(videoPath, data);

              await new Promise((resolve, reject) => {
                  ffmpeg(videoPath)
                      .outputOptions([
                          `-r ${fps || 12}`,
                          '-q:v 1'
                      ])
                      .output(`${tmpFolder}/%05d.jpg`)
                      .on('start', (cmd) => console.log(cmd))
                      .on('end', () => resolve())
                      .on('error', (err) => reject(new FatalError(err)))
                      .run();
              });

              const files = await fs.readdir(tmpFolder);
              for (const file of files) {
                  if (file.endsWith('jpg')) {
                      frames.push(await fs.readFile(path.join(tmpFolder, file)));
                  }
              }

          });

          return NextNode.from({ outputs: { frames } });
      }
    source: catalog
    title: en=Split video by frames;ru=Разбить видео по кадрам
    version: 6