-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideoProcessor.js
224 lines (194 loc) · 6.37 KB
/
videoProcessor.js
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// videoProcessor.js
const ffmpeg = require('fluent-ffmpeg');
const path = require('path');
const fs = require('fs/promises');
const { existsSync, mkdirSync } = require('fs');
const EventEmitter = require('events');
class VideoProcessor extends EventEmitter {
constructor() {
super();
this.RESOLUTIONS = [
{name:"240p", width:426, height:240, bitrate: '400k' },
{ name: '360p', width: 640, height: 360, bitrate: '800k' },
{ name: '720p', width: 1280, height: 720, bitrate: '2500k' },
{ name: '1080p', width: 1920, height: 1080, bitrate: '5000k' }
];
}
/**
* Ensures output directory exists
* @param {string} dir - Directory path
*/
async ensureDir(dir) {
try {
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
} catch (error) {
throw new Error(`Failed to create directory: ${error.message}`);
}
}
/**
* Get video metadata
* @param {string} inputFile - Input file path
* @returns {Promise<Object>} Video metadata
*/
async getVideoMetadata(inputFile) {
return new Promise((resolve, reject) => {
ffmpeg.ffprobe(inputFile, (err, metadata) => {
if (err) reject(err);
resolve(metadata);
});
});
}
/**
* Create HLS stream for a specific resolution
* @param {string} inputFile - Input file path
* @param {string} outputDir - Output directory
* @param {Object} resolution - Resolution configuration
* @returns {Promise<string>} Output path
*/
async createHLSStream(inputFile, outputDir, resolution) {
const resolutionDir = path.join(outputDir, resolution.name);
await this.ensureDir(resolutionDir);
const segmentPattern = path.join(resolutionDir, 'segment-%03d.ts');
const playlistPath = path.join(resolutionDir, 'playlist.m3u8');
return new Promise((resolve, reject) => {
ffmpeg(inputFile)
.outputOptions([
'-codec:v libx264',
'-codec:a aac',
'-profile:v main',
'-preset veryfast',
'-sc_threshold 0',
'-g 48',
'-keyint_min 48',
`-vf scale=w=${resolution.width}:h=${resolution.height}`,
`-b:v ${resolution.bitrate}`,
'-b:a 128k',
'-hls_time 6',
'-hls_list_size 0',
'-f hls',
`-hls_segment_filename ${segmentPattern}`
])
.output(playlistPath)
.on('progress', (progress) => {
this.emit('progress', {
resolution: resolution.name,
percent: progress.percent
});
})
.on('end', () => resolve(playlistPath))
.on('error', reject)
.run();
});
}
/**
* Generate master playlist
* @param {string} outputDir - Output directory
* @param {Array} variants - Variant streams
*/
async generateMasterPlaylist(outputDir, variants) {
const masterPlaylistPath = path.join(outputDir, 'master.m3u8');
let content = '#EXTM3U\n#EXT-X-VERSION:3\n';
variants.forEach(variant => {
content += `#EXT-X-STREAM-INF:BANDWIDTH=${parseInt(variant.bitrate) * 1000},RESOLUTION=${variant.width}x${variant.height}\n`;
content += `${variant.name}/playlist.m3u8\n`;
});
await fs.writeFile(masterPlaylistPath, content);
return masterPlaylistPath;
}
/**
* Convert video to HLS format
* @param {string} inputFile - Input file path
* @param {string} outputDir - Output directory
* @param {Object} options - Conversion options
* @returns {Promise<Object>} Conversion result
*/
async convertToHLS(inputFile, outputDir, options = {}) {
try {
// Bind the context to ensure 'this' refers to the class instance
this.convertToHLS = this.convertToHLS.bind(this);
// Validate input file
if (!existsSync(inputFile)) {
throw new Error('Input file does not exist');
}
// Ensure output directory exists
await this.ensureDir(outputDir);
// Get video metadata
const metadata = await this.getVideoMetadata(inputFile);
this.emit('metadata', metadata);
// Filter resolutions based on input video
const inputHeight = metadata.streams.find(s => s.codec_type === 'video')?.height || 0;
const applicableResolutions = this.RESOLUTIONS.filter(
res => res.height <= inputHeight
).sort((a, b) => a.height - b.height);
if (applicableResolutions.length === 0) {
throw new Error('No applicable resolutions found for input video');
}
// Create HLS streams for each resolution
const conversionResults = await Promise.all(
applicableResolutions.map(resolution =>
this.createHLSStream(inputFile, outputDir, resolution)
.catch(error => {
this.emit('error', {
resolution: resolution.name,
error: error.message
});
return null;
})
)
);
// Filter out failed conversions
const successfulConversions = conversionResults
.filter(Boolean)
.map((path, index) => ({
...applicableResolutions[index],
playlistPath: path
}));
if (successfulConversions.length === 0) {
throw new Error('All resolution conversions failed');
}
// Generate master playlist
const masterPlaylistPath = await this.generateMasterPlaylist(
outputDir,
successfulConversions
);
this.emit('complete', {
masterPlaylist: masterPlaylistPath,
variants: successfulConversions
});
await fs.unlink(inputFile);
return {
success: true,
masterPlaylist: masterPlaylistPath,
variants: successfulConversions
};
} catch (error) {
this.emit('error', error);
throw error;
}
}
}
// Export singleton instance
module.exports = new VideoProcessor();
// Usage example:
/*
const videoProcessor = require('./videoProcessor');
async function processVideo() {
try {
videoProcessor.on('progress', (progress) => {
console.log(`Processing ${progress.resolution}: ${progress.percent}%`);
});
videoProcessor.on('metadata', (metadata) => {
console.log('Video metadata:', metadata);
});
const result = await videoProcessor.convertToHLS(
'input.mp4',
'./output'
);
console.log('Conversion complete:', result);
} catch (error) {
console.error('Conversion failed:', error);
}
}
*/