1 /* tslint:disable:no-unused-expression */
3 import * as chai from 'chai'
5 import { omit } from 'lodash'
6 import * as ffmpeg from 'fluent-ffmpeg'
7 import { getMaxBitrate, VideoDetails, VideoResolution, VideoState } from '../../../../shared/models/videos'
8 import { audio, getVideoFileBitrate, getVideoFileFPS, getVideoFileResolution } from '../../../helpers/ffmpeg-utils'
10 buildAbsoluteFixturePath,
12 flushAndRunMultipleServers,
19 setAccessTokensToServers,
23 import { join } from 'path'
24 import { waitJobs } from '../../utils/server/jobs'
25 import { pathExists } from 'fs-extra'
26 import { VIDEO_TRANSCODING_FPS } from '../../../../server/initializers/constants'
28 const expect = chai.expect
30 describe('Test video transcoding', function () {
31 let servers: ServerInfo[] = []
33 before(async function () {
37 servers = await flushAndRunMultipleServers(2)
39 await setAccessTokensToServers(servers)
41 await doubleFollow(servers[0], servers[1])
44 it('Should not transcode video on server 1', async function () {
47 const videoAttributes = {
48 name: 'my super name for server 1',
49 description: 'my super description for server 1',
50 fixture: 'video_short.webm'
52 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
54 await waitJobs(servers)
56 for (const server of servers) {
57 const res = await getVideosList(server.url)
58 const video = res.body.data[ 0 ]
60 const res2 = await getVideo(server.url, video.id)
61 const videoDetails = res2.body
62 expect(videoDetails.files).to.have.lengthOf(1)
64 const magnetUri = videoDetails.files[ 0 ].magnetUri
65 expect(magnetUri).to.match(/\.webm/)
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$/)
74 it('Should transcode video on server 2', async function () {
77 const videoAttributes = {
78 name: 'my super name for server 2',
79 description: 'my super description for server 2',
80 fixture: 'video_short.webm'
82 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
84 await waitJobs(servers)
86 for (const server of servers) {
87 const res = await getVideosList(server.url)
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
93 expect(videoDetails.files).to.have.lengthOf(4)
95 const magnetUri = videoDetails.files[ 0 ].magnetUri
96 expect(magnetUri).to.match(/\.mp4/)
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$/)
105 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
108 const videoAttributes = {
110 fixture: 'video_short_mp3_256k.mp4'
112 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
114 await waitJobs(servers)
116 for (const server of servers) {
117 const res = await getVideosList(server.url)
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
123 expect(videoDetails.files).to.have.lengthOf(4)
125 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
126 const probe = await audio.get(ffmpeg, path)
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)
132 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
137 it('Should transcode video with no audio and have no audio itself', async function () {
140 const videoAttributes = {
142 fixture: 'video_short_no_audio.mp4'
144 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
146 await waitJobs(servers)
148 for (const server of servers) {
149 const res = await getVideosList(server.url)
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
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')
162 it('Should leave the audio untouched, but properly transcode the video', async function () {
165 const videoAttributes = {
166 name: 'untouched_audio',
167 fixture: 'video_short.mp4'
169 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
171 await waitJobs(servers)
173 for (const server of servers) {
174 const res = await getVideosList(server.url)
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
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))
189 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
194 it('Should transcode a 60 FPS video', async function () {
197 const videoAttributes = {
198 name: 'my super 30fps name for server 2',
199 description: 'my super 30fps description for server 2',
200 fixture: '60fps_720p_small.mp4'
202 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
204 await waitJobs(servers)
206 for (const server of servers) {
207 const res = await getVideosList(server.url)
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
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)
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)
223 expect(fps).to.be.below(31)
226 const path = join(root(), 'test2', 'videos', video.uuid + '-720.mp4')
227 const fps = await getVideoFileFPS(path)
229 expect(fps).to.be.above(58).and.below(62)
233 it('Should wait for transcoding before publishing the video', async function () {
237 // Upload the video, but wait transcoding
238 const videoAttributes = {
239 name: 'waiting video',
240 fixture: 'video_short1.webm',
241 waitTranscoding: true
243 const resVideo = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, videoAttributes)
244 const videoId = resVideo.body.video.uuid
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
253 // Should have my video
254 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
255 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
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
261 // Should not list this video
262 const resVideos = await getVideosList(servers[1].url)
263 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
264 expect(videoToFindInList).to.be.undefined
266 // Server 1 should not have the video yet
267 await getVideo(servers[0].url, videoId, 404)
270 await waitJobs(servers)
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
277 const res2 = await getVideo(server.url, videoToFind.id)
278 const videoDetails: VideoDetails = res2.body
280 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
281 expect(videoDetails.state.label).to.equal('Published')
282 expect(videoDetails.waitTranscoding).to.be.true
286 const tempFixturePath = buildAbsoluteFixturePath('video_high_bitrate_1080p.mp4', true)
287 it('Should respect maximum bitrate values', async function () {
291 const exists = await pathExists(tempFixturePath)
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) => {
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)
310 const bitrate = await getVideoFileBitrate(tempFixturePath)
311 expect(bitrate).to.be.above(getMaxBitrate(VideoResolution.H_1080P, 60, VIDEO_TRANSCODING_FPS))
314 const videoAttributes = {
315 name: 'high bitrate video',
316 description: 'high bitrate video',
317 fixture: tempFixturePath
320 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
322 await waitJobs(servers)
324 for (const server of servers) {
325 const res = await getVideosList(server.url)
327 const video = res.body.data.find(v => v.name === videoAttributes.name)
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)
335 expect(resolution2.videoFileResolution.toString()).to.equal(resolution)
336 expect(bitrate).to.be.below(getMaxBitrate(resolution2.videoFileResolution, fps, VIDEO_TRANSCODING_FPS))
341 after(async function () {
342 killallServers(servers)