forked from mmomtchev/ffmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranscodeVideo.js
158 lines (125 loc) · 3.88 KB
/
transcodeVideo.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
// JavaScript reimplementation of
// https://github.com/h4tr3d/avcpp/blob/master/example/api2-samples/api2-decode-encode-video.cpp
// (video transcoding using the low-level C++ API from JavaScript)
const ffmpeg = require('../lib');
const {
FormatContext,
findEncodingCodecFormat,
VideoDecoderContext,
VideoEncoderContext,
OutputFormat,
Codec
} = ffmpeg;
if (!process.argv[3]) {
console.log('Usage: node transcodeVideo.js <input> <ouput>');
process.exit(1);
}
if (process.argv[4] && process.argv[4].startsWith('v')) {
ffmpeg.setLogLevel(ffmpeg.AV_LOG_DEBUG);
} else {
ffmpeg.setLogLevel(ffmpeg.AV_LOG_ERROR);
}
const inp = process.argv[2];
const outp = process.argv[3];
//
// INPUT
//
const ictx = new FormatContext;
let videoStream = null;
let vst = null;
ictx.openInput(inp);
ictx.findStreamInfo();
for (let i = 0; i < ictx.streamsCount(); i++) {
const st = ictx.stream(i);
if (st.mediaType() == ffmpeg.AV_MEDIA_TYPE_VIDEO) {
videoStream = i;
vst = st;
console.log(`Stream ${i} is a video stream`);
break;
}
}
if (vst === null) {
console.error('Video stream not found');
process.exit(3);
}
if (!vst.isValid()) {
console.error('Video stream not valid');
process.exit(4);
}
const vdec = new VideoDecoderContext(vst);
vdec.setRefCountedFrames(true);
const decodec = new Codec;
vdec.openCodec(decodec);
//
// OUTPUT
//
const ofrmt = new OutputFormat;
const octx = new FormatContext;
ofrmt.setFormat('', outp, '');
octx.setOutputFormat(ofrmt);
const ocodec = findEncodingCodecFormat(ofrmt, true);
console.log(`Using codec ${ocodec.name()}`);
const encoder = new VideoEncoderContext(ocodec);
// Settings
encoder.setWidth(vdec.width());
encoder.setHeight(vdec.height());
if (vdec.pixelFormat().get() > -1) {
encoder.setPixelFormat(vdec.pixelFormat());
console.log(`Pixel format ${encoder.pixelFormat()}`);
} else {
console.warn('Invalid pixel format');
}
encoder.setTimeBase(vst.timeBase());
encoder.setBitRate(vdec.bitRate());
const encodec = new Codec;
encoder.openCodec(encodec);
const ost = octx.addVideoStream(encoder);
ost.setFrameRate(vst.frameRate());
octx.openOutput(outp);
octx.dump();
octx.writeHeader();
octx.flush();
//
// PROCESS
//
while (true) {
// READING
const pkt = ictx.readPacket();
let flushDecoder = false;
if (!pkt.isNull()) {
if (pkt.streamIndex() != videoStream) {
continue;
}
console.log(`Read packet: pts=${pkt.pts()}, dts=${pkt.dts()} / ${pkt.pts().seconds()} / ${pkt.timeBase()} / stream ${pkt.streamIndex()}`);
} else {
flushDecoder = true;
}
do {
// DECODING
const frame = vdec.decode(pkt, true);
let flushEncoder = false;
if (!frame.isComplete()) {
if (flushDecoder) {
flushEncoder = true;
}
} else {
console.log(`Decoded frame: pts=${frame.pts()} / ${frame.pts().seconds()} / ${frame.timeBase()} / ${frame.width()}x${frame.height()}, size=${frame.size()}, ref=${frame.isReferenced()}:${frame.refCount()} / type: ${frame.pictureType()} }`);
frame.setTimeBase(encoder.timeBase());
frame.setStreamIndex(0);
frame.setPictureType(ffmpeg.AV_PICTURE_TYPE_NONE);
// data is in frame.data()
console.log(`Processed frame: pts=${frame.pts()} / ${frame.pts().seconds()} / ${frame.timeBase()} / ${frame.width()}x${frame.height()}, size=${frame.size()}, ref=${frame.isReferenced()}:${frame.refCount()} / type: ${frame.pictureType()} }`);
}
if (frame.isComplete() || flushEncoder) {
// ENCODING
const opkt = frame.isComplete() ? encoder.encode(frame) : encoder.finalize();
opkt.setStreamIndex(0);
console.log(`Write packet: pts=${opkt.pts()}, dts=${opkt.dts()} / ${opkt.pts().seconds()} / ${opkt.timeBase()} / stream ${opkt.streamIndex()}, size: ${opkt.size()}`);
octx.writePacket(opkt);
}
if (flushEncoder) break;
} while (flushDecoder);
if (flushDecoder) break;
}
octx.writeTrailer();
console.log('done');