-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
76 lines (61 loc) · 2.3 KB
/
index.ts
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
import alfy from 'alfy';
import { spawn } from 'child_process';
import { AudioStream, Stream, StreamType } from './types.js';
import { createExecutor } from './process-executor.js';
const runner = createExecutor({ spawn })
const ffprobePath = '/usr/local/bin/ffprobe';
const args = [
'-hide_banner',
'-show_error',
'-show_entries',
'stream=duration,r_frame_rate,codec_name,codec_type,width,height,bit_rate',
'-v',
'quiet',
'-of',
'json',
'-i',
alfy.input
];
const isAudioStream = (stream: Stream): stream is AudioStream => {
return stream.hasOwnProperty('width');
}
const buildStreamInfo = (stream: Stream) => {
let resolution = ''
if(isAudioStream(stream)){
resolution = stream.width ? ` | ${stream.width}x${stream.height}` : '';
}
const duration = stream.duration ? ` | ${parseFloat(stream.duration).toFixed(4)}s` : '';
const framerate = stream.r_frame_rate && eval(stream.r_frame_rate) ? ` | ${parseFloat(eval(stream.r_frame_rate)).toFixed(2)}fps` : '';
const bitrate = stream.bit_rate ? ` | ${(parseFloat(stream.bit_rate) / 1024 / 1024).toFixed(2)}Mbps` : '';
return `[${stream.codec_type.toUpperCase()}]: ${stream.codec_name}${bitrate}${resolution}${duration}${framerate}`;
}
const getStreamCount = (streams: Stream[]) => (streamType: StreamType) => {
return streams.filter(stream => stream.codec_type === streamType).length
}
const main = async (): Promise<void> => {
const output = await runner.run(ffprobePath, args)
const streams = JSON.parse(output).streams as Stream[]
// console.log(streams)
let audioCount = 0;
let videoCount = 0;
const countStreams = getStreamCount(streams);
const numberOfVideoStream = countStreams('video');
const numberOfAudioStreams = countStreams('audio')
alfy.output(streams.map((stream) => {
let count = 0;
let subtitle = '';
if (stream.codec_type === 'video') {
count = ++videoCount;
subtitle = `${count} of ${numberOfVideoStream} video track(s)`;
}
if (stream.codec_type === 'audio') {
count = ++audioCount;
subtitle = `${count} of ${numberOfAudioStreams} audio track(s)`;
}
return {
title: buildStreamInfo(stream),
subtitle
}
}));
}
main();