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