-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathtranscode.ts
More file actions
68 lines (57 loc) 路 1.79 KB
/
Copy pathtranscode.ts
File metadata and controls
68 lines (57 loc) 路 1.79 KB
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
import { Storage } from '@google-cloud/storage';
import ffmpegInstaller from '@ffmpeg-installer/ffmpeg';
import ffmpeg from 'fluent-ffmpeg';
ffmpeg.setFfmpegPath(ffmpegInstaller.path);
interface File {
name: string;
bucket: string;
}
interface Resolution {
suffix: string;
size: string;
}
export default async function transcode(
file: File,
resolution: Resolution
): Promise<void> {
return new Promise((resolve, reject) => {
const storage = new Storage();
const originBucket = storage.bucket(file.bucket);
const destinationBucket = storage.bucket('europa.rocketseat.dev');
const originFile = originBucket.file(file.name);
const originStream = originFile.createReadStream();
const destionationFile = file.name.replace(
'.mp4',
`_${resolution.suffix}.mp4`
);
const destinationStream = destinationBucket
.file(destionationFile)
.createWriteStream({
metadata: {
contentType: 'video/mp4',
},
});
ffmpeg(originStream)
.withOutputOption('-f mp4')
.withOutputOption('-preset superfast')
.withOutputOption('-movflags frag_keyframe+empty_moov')
.withOutputOption('-max_muxing_queue_size 9999')
.withVideoCodec('libx264')
.withSize(resolution.size)
.withAspectRatio('16:9')
.on('start', cmdLine => {
console.log(`[${resolution.suffix}] Started FFMpeg`, cmdLine);
})
.on('end', () => {
console.log(`[${resolution.suffix}] Sucess!.`);
resolve();
})
.on('error', (err: Error, stdout, stderr) => {
console.log(`[${resolution.suffix}] Error:`, err.message);
console.error('stdout:', stdout);
console.error('stderr:', stderr);
reject();
})
.pipe(destinationStream, { end: true });
});
}