]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tests/api/videos/video-transcoder.ts
Support additional video extensions
[github/Chocobozzz/PeerTube.git] / server / tests / api / videos / video-transcoder.ts
CommitLineData
0e1dc3e7
C
1/* tslint:disable:no-unused-expression */
2
0e1dc3e7 3import * as chai from 'chai'
a7ba16b6 4import 'mocha'
7160878c 5import { omit } from 'lodash'
c1c86c15
C
6import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
7import { audio, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
0e1dc3e7 8import {
7160878c 9 buildAbsoluteFixturePath,
2186386c
C
10 doubleFollow,
11 flushAndRunMultipleServers,
d175a6f7 12 generateHighBitrateVideo,
2186386c
C
13 getMyVideos,
14 getVideo,
15 getVideosList,
16 killallServers,
17 root,
18 ServerInfo,
19 setAccessTokensToServers,
20 uploadVideo,
d175a6f7 21 webtorrentAdd
9639bd17 22} from '../../../../shared/utils'
14e2014a 23import { extname, join } from 'path'
9639bd17 24import { waitJobs } from '../../../../shared/utils/server/jobs'
edb4ffc7 25import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
a7ba16b6
C
26
27const expect = chai.expect
0e1dc3e7
C
28
29describe('Test video transcoding', function () {
30 let servers: ServerInfo[] = []
31
32 before(async function () {
e212f887 33 this.timeout(30000)
0e1dc3e7
C
34
35 // Run servers
36 servers = await flushAndRunMultipleServers(2)
37
38 await setAccessTokensToServers(servers)
b2977eec
C
39
40 await doubleFollow(servers[0], servers[1])
0e1dc3e7
C
41 })
42
43 it('Should not transcode video on server 1', async function () {
44 this.timeout(60000)
45
46 const videoAttributes = {
975e6e0e
C
47 name: 'my super name for server 1',
48 description: 'my super description for server 1',
0e1dc3e7
C
49 fixture: 'video_short.webm'
50 }
51 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
52
3cd0734f 53 await waitJobs(servers)
0e1dc3e7 54
b2977eec
C
55 for (const server of servers) {
56 const res = await getVideosList(server.url)
57 const video = res.body.data[ 0 ]
40298b02 58
b2977eec
C
59 const res2 = await getVideo(server.url, video.id)
60 const videoDetails = res2.body
61 expect(videoDetails.files).to.have.lengthOf(1)
5f04dd2f 62
b2977eec
C
63 const magnetUri = videoDetails.files[ 0 ].magnetUri
64 expect(magnetUri).to.match(/\.webm/)
0e1dc3e7 65
b2977eec
C
66 const torrent = await webtorrentAdd(magnetUri, true)
67 expect(torrent.files).to.be.an('array')
68 expect(torrent.files.length).to.equal(1)
69 expect(torrent.files[ 0 ].path).match(/\.webm$/)
70 }
0e1dc3e7
C
71 })
72
73 it('Should transcode video on server 2', async function () {
74 this.timeout(60000)
75
76 const videoAttributes = {
975e6e0e
C
77 name: 'my super name for server 2',
78 description: 'my super description for server 2',
0e1dc3e7
C
79 fixture: 'video_short.webm'
80 }
81 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
82
3cd0734f 83 await waitJobs(servers)
0e1dc3e7 84
b2977eec
C
85 for (const server of servers) {
86 const res = await getVideosList(server.url)
0e1dc3e7 87
b2977eec
C
88 const video = res.body.data.find(v => v.name === videoAttributes.name)
89 const res2 = await getVideo(server.url, video.id)
90 const videoDetails = res2.body
5f04dd2f 91
b2977eec 92 expect(videoDetails.files).to.have.lengthOf(4)
40298b02 93
b2977eec
C
94 const magnetUri = videoDetails.files[ 0 ].magnetUri
95 expect(magnetUri).to.match(/\.mp4/)
0e1dc3e7 96
b2977eec
C
97 const torrent = await webtorrentAdd(magnetUri, true)
98 expect(torrent.files).to.be.an('array')
99 expect(torrent.files.length).to.equal(1)
100 expect(torrent.files[ 0 ].path).match(/\.mp4$/)
101 }
0e1dc3e7
C
102 })
103
7160878c
RK
104 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
105 this.timeout(60000)
106
107 const videoAttributes = {
108 name: 'mp3_256k',
109 fixture: 'video_short_mp3_256k.mp4'
110 }
111 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
112
113 await waitJobs(servers)
114
b2977eec
C
115 for (const server of servers) {
116 const res = await getVideosList(server.url)
7160878c 117
b2977eec
C
118 const video = res.body.data.find(v => v.name === videoAttributes.name)
119 const res2 = await getVideo(server.url, video.id)
120 const videoDetails: VideoDetails = res2.body
7160878c 121
b2977eec 122 expect(videoDetails.files).to.have.lengthOf(4)
7160878c 123
b2977eec 124 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
cdf4cb9e 125 const probe = await audio.get(path)
7160878c 126
b2977eec
C
127 if (probe.audioStream) {
128 expect(probe.audioStream[ 'codec_name' ]).to.be.equal('aac')
129 expect(probe.audioStream[ 'bit_rate' ]).to.be.at.most(384 * 8000)
130 } else {
131 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
132 }
7160878c
RK
133 }
134 })
135
136 it('Should transcode video with no audio and have no audio itself', async function () {
137 this.timeout(60000)
138
139 const videoAttributes = {
140 name: 'no_audio',
141 fixture: 'video_short_no_audio.mp4'
142 }
143 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
144
145 await waitJobs(servers)
146
b2977eec
C
147 for (const server of servers) {
148 const res = await getVideosList(server.url)
7160878c 149
b2977eec
C
150 const video = res.body.data.find(v => v.name === videoAttributes.name)
151 const res2 = await getVideo(server.url, video.id)
152 const videoDetails: VideoDetails = res2.body
7160878c 153
b2977eec
C
154 expect(videoDetails.files).to.have.lengthOf(4)
155 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
cdf4cb9e 156 const probe = await audio.get(path)
b2977eec
C
157 expect(probe).to.not.have.property('audioStream')
158 }
7160878c
RK
159 })
160
161 it('Should leave the audio untouched, but properly transcode the video', async function () {
162 this.timeout(60000)
163
164 const videoAttributes = {
165 name: 'untouched_audio',
166 fixture: 'video_short.mp4'
167 }
168 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
169
170 await waitJobs(servers)
171
b2977eec
C
172 for (const server of servers) {
173 const res = await getVideosList(server.url)
174
175 const video = res.body.data.find(v => v.name === videoAttributes.name)
176 const res2 = await getVideo(server.url, video.id)
177 const videoDetails: VideoDetails = res2.body
178
179 expect(videoDetails.files).to.have.lengthOf(4)
180 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
cdf4cb9e 181 const fixtureVideoProbe = await audio.get(fixturePath)
b2977eec 182 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
cdf4cb9e 183 const videoProbe = await audio.get(path)
b2977eec
C
184 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
185 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
186 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
187 } else {
188 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
189 }
7160878c
RK
190 }
191 })
192
3a6f351b 193 it('Should transcode a 60 FPS video', async function () {
73c69591
C
194 this.timeout(60000)
195
196 const videoAttributes = {
197 name: 'my super 30fps name for server 2',
198 description: 'my super 30fps description for server 2',
3a6f351b 199 fixture: '60fps_720p_small.mp4'
73c69591
C
200 }
201 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
202
3cd0734f 203 await waitJobs(servers)
73c69591 204
b2977eec
C
205 for (const server of servers) {
206 const res = await getVideosList(server.url)
73c69591 207
b2977eec
C
208 const video = res.body.data.find(v => v.name === videoAttributes.name)
209 const res2 = await getVideo(server.url, video.id)
210 const videoDetails: VideoDetails = res2.body
73c69591 211
b2977eec
C
212 expect(videoDetails.files).to.have.lengthOf(4)
213 expect(videoDetails.files[ 0 ].fps).to.be.above(58).and.below(62)
214 expect(videoDetails.files[ 1 ].fps).to.be.below(31)
215 expect(videoDetails.files[ 2 ].fps).to.be.below(31)
216 expect(videoDetails.files[ 3 ].fps).to.be.below(31)
73c69591 217
b2977eec
C
218 for (const resolution of [ '240', '360', '480' ]) {
219 const path = join(root(), 'test2', 'videos', video.uuid + '-' + resolution + '.mp4')
220 const fps = await getVideoFileFPS(path)
73c69591 221
b2977eec
C
222 expect(fps).to.be.below(31)
223 }
3a6f351b 224
b2977eec
C
225 const path = join(root(), 'test2', 'videos', video.uuid + '-720.mp4')
226 const fps = await getVideoFileFPS(path)
3a6f351b 227
b2977eec
C
228 expect(fps).to.be.above(58).and.below(62)
229 }
73c69591
C
230 })
231
edb4ffc7 232 it('Should wait for transcoding before publishing the video', async function () {
2186386c
C
233 this.timeout(80000)
234
2186386c
C
235 {
236 // Upload the video, but wait transcoding
237 const videoAttributes = {
238 name: 'waiting video',
239 fixture: 'video_short1.webm',
240 waitTranscoding: true
241 }
242 const resVideo = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, videoAttributes)
243 const videoId = resVideo.body.video.uuid
244
245 // Should be in transcode state
246 const { body } = await getVideo(servers[ 1 ].url, videoId)
247 expect(body.name).to.equal('waiting video')
248 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
249 expect(body.state.label).to.equal('To transcode')
250 expect(body.waitTranscoding).to.be.true
251
252 // Should have my video
253 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
7160878c 254 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
2186386c
C
255 expect(videoToFindInMine).not.to.be.undefined
256 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
257 expect(videoToFindInMine.state.label).to.equal('To transcode')
258 expect(videoToFindInMine.waitTranscoding).to.be.true
259
260 // Should not list this video
261 const resVideos = await getVideosList(servers[1].url)
7160878c 262 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
2186386c
C
263 expect(videoToFindInList).to.be.undefined
264
265 // Server 1 should not have the video yet
266 await getVideo(servers[0].url, videoId, 404)
267 }
268
3cd0734f 269 await waitJobs(servers)
2186386c
C
270
271 for (const server of servers) {
272 const res = await getVideosList(server.url)
273 const videoToFind = res.body.data.find(v => v.name === 'waiting video')
274 expect(videoToFind).not.to.be.undefined
275
276 const res2 = await getVideo(server.url, videoToFind.id)
277 const videoDetails: VideoDetails = res2.body
278
279 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
280 expect(videoDetails.state.label).to.equal('Published')
281 expect(videoDetails.waitTranscoding).to.be.true
282 }
283 })
284
edb4ffc7
FA
285 it('Should respect maximum bitrate values', async function () {
286 this.timeout(160000)
287
74cd011b
C
288 let tempFixturePath: string
289
edb4ffc7 290 {
74cd011b 291 tempFixturePath = await generateHighBitrateVideo()
edb4ffc7
FA
292
293 const bitrate = await getVideoFileBitrate(tempFixturePath)
294 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 60, VIDEO_TRANSCODING_FPS))
c1c86c15 295 }
edb4ffc7 296
c1c86c15
C
297 const videoAttributes = {
298 name: 'high bitrate video',
299 description: 'high bitrate video',
300 fixture: tempFixturePath
301 }
edb4ffc7 302
c1c86c15 303 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
edb4ffc7 304
c1c86c15 305 await waitJobs(servers)
edb4ffc7 306
c1c86c15
C
307 for (const server of servers) {
308 const res = await getVideosList(server.url)
edb4ffc7 309
c1c86c15 310 const video = res.body.data.find(v => v.name === videoAttributes.name)
edb4ffc7 311
c1c86c15
C
312 for (const resolution of ['240', '360', '480', '720', '1080']) {
313 const path = join(root(), 'test2', 'videos', video.uuid + '-' + resolution + '.mp4')
314 const bitrate = await getVideoFileBitrate(path)
315 const fps = await getVideoFileFPS(path)
316 const resolution2 = await getVideoFileResolution(path)
edb4ffc7 317
c1c86c15
C
318 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
319 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
edb4ffc7
FA
320 }
321 }
322 })
323
14e2014a
C
324 it('Should accept and transcode additional extensions', async function () {
325 this.timeout(300000)
326
327 for (const fixture of [ 'video_short.mkv', 'video_short.avi' ]) {
328 const videoAttributes = {
329 name: fixture,
330 fixture
331 }
332
333 await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, videoAttributes)
334
335 await waitJobs(servers)
336
337 for (const server of servers) {
338 const res = await getVideosList(server.url)
339
340 const video = res.body.data.find(v => v.name === videoAttributes.name)
341 const res2 = await getVideo(server.url, video.id)
342 const videoDetails = res2.body
343
344 expect(videoDetails.files).to.have.lengthOf(4)
345
346 const magnetUri = videoDetails.files[ 0 ].magnetUri
347 expect(magnetUri).to.contain('.mp4')
348 }
349 }
350 })
351
0e1dc3e7
C
352 after(async function () {
353 killallServers(servers)
0e1dc3e7
C
354 })
355})