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 { VideoDetails, VideoState } from '../../../../shared/models/videos'
8 import { getVideoFileFPS, audio } from '../../../helpers/ffmpeg-utils'
10 buildAbsoluteFixturePath,
12 flushAndRunMultipleServers,
19 setAccessTokensToServers,
23 import { join } from 'path'
24 import { waitJobs } from '../../utils/server/jobs'
26 const expect = chai.expect
28 describe('Test video transcoding', function () {
29 let servers: ServerInfo[] = []
31 before(async function () {
35 servers = await flushAndRunMultipleServers(2)
37 await setAccessTokensToServers(servers)
39 await doubleFollow(servers[0], servers[1])
42 it('Should not transcode video on server 1', async function () {
45 const videoAttributes = {
46 name: 'my super name for server 1',
47 description: 'my super description for server 1',
48 fixture: 'video_short.webm'
50 await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
52 await waitJobs(servers)
54 for (const server of servers) {
55 const res = await getVideosList(server.url)
56 const video = res.body.data[ 0 ]
58 const res2 = await getVideo(server.url, video.id)
59 const videoDetails = res2.body
60 expect(videoDetails.files).to.have.lengthOf(1)
62 const magnetUri = videoDetails.files[ 0 ].magnetUri
63 expect(magnetUri).to.match(/\.webm/)
65 const torrent = await webtorrentAdd(magnetUri, true)
66 expect(torrent.files).to.be.an('array')
67 expect(torrent.files.length).to.equal(1)
68 expect(torrent.files[ 0 ].path).match(/\.webm$/)
72 it('Should transcode video on server 2', async function () {
75 const videoAttributes = {
76 name: 'my super name for server 2',
77 description: 'my super description for server 2',
78 fixture: 'video_short.webm'
80 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
82 await waitJobs(servers)
84 for (const server of servers) {
85 const res = await getVideosList(server.url)
87 const video = res.body.data.find(v => v.name === videoAttributes.name)
88 const res2 = await getVideo(server.url, video.id)
89 const videoDetails = res2.body
91 expect(videoDetails.files).to.have.lengthOf(4)
93 const magnetUri = videoDetails.files[ 0 ].magnetUri
94 expect(magnetUri).to.match(/\.mp4/)
96 const torrent = await webtorrentAdd(magnetUri, true)
97 expect(torrent.files).to.be.an('array')
98 expect(torrent.files.length).to.equal(1)
99 expect(torrent.files[ 0 ].path).match(/\.mp4$/)
103 it('Should transcode high bit rate mp3 to proper bit rate', async function () {
106 const videoAttributes = {
108 fixture: 'video_short_mp3_256k.mp4'
110 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
112 await waitJobs(servers)
114 for (const server of servers) {
115 const res = await getVideosList(server.url)
117 const video = res.body.data.find(v => v.name === videoAttributes.name)
118 const res2 = await getVideo(server.url, video.id)
119 const videoDetails: VideoDetails = res2.body
121 expect(videoDetails.files).to.have.lengthOf(4)
123 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
124 const probe = await audio.get(ffmpeg, path)
126 if (probe.audioStream) {
127 expect(probe.audioStream[ 'codec_name' ]).to.be.equal('aac')
128 expect(probe.audioStream[ 'bit_rate' ]).to.be.at.most(384 * 8000)
130 this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
135 it('Should transcode video with no audio and have no audio itself', async function () {
138 const videoAttributes = {
140 fixture: 'video_short_no_audio.mp4'
142 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
144 await waitJobs(servers)
146 for (const server of servers) {
147 const res = await getVideosList(server.url)
149 const video = res.body.data.find(v => v.name === videoAttributes.name)
150 const res2 = await getVideo(server.url, video.id)
151 const videoDetails: VideoDetails = res2.body
153 expect(videoDetails.files).to.have.lengthOf(4)
154 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
155 const probe = await audio.get(ffmpeg, path)
156 expect(probe).to.not.have.property('audioStream')
160 it('Should leave the audio untouched, but properly transcode the video', async function () {
163 const videoAttributes = {
164 name: 'untouched_audio',
165 fixture: 'video_short.mp4'
167 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
169 await waitJobs(servers)
171 for (const server of servers) {
172 const res = await getVideosList(server.url)
174 const video = res.body.data.find(v => v.name === videoAttributes.name)
175 const res2 = await getVideo(server.url, video.id)
176 const videoDetails: VideoDetails = res2.body
178 expect(videoDetails.files).to.have.lengthOf(4)
179 const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
180 const fixtureVideoProbe = await audio.get(ffmpeg, fixturePath)
181 const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
182 const videoProbe = await audio.get(ffmpeg, path)
183 if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
184 const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
185 expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
187 this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
192 it('Should transcode a 60 FPS video', async function () {
195 const videoAttributes = {
196 name: 'my super 30fps name for server 2',
197 description: 'my super 30fps description for server 2',
198 fixture: '60fps_720p_small.mp4'
200 await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
202 await waitJobs(servers)
204 for (const server of servers) {
205 const res = await getVideosList(server.url)
207 const video = res.body.data.find(v => v.name === videoAttributes.name)
208 const res2 = await getVideo(server.url, video.id)
209 const videoDetails: VideoDetails = res2.body
211 expect(videoDetails.files).to.have.lengthOf(4)
212 expect(videoDetails.files[ 0 ].fps).to.be.above(58).and.below(62)
213 expect(videoDetails.files[ 1 ].fps).to.be.below(31)
214 expect(videoDetails.files[ 2 ].fps).to.be.below(31)
215 expect(videoDetails.files[ 3 ].fps).to.be.below(31)
217 for (const resolution of [ '240', '360', '480' ]) {
218 const path = join(root(), 'test2', 'videos', video.uuid + '-' + resolution + '.mp4')
219 const fps = await getVideoFileFPS(path)
221 expect(fps).to.be.below(31)
224 const path = join(root(), 'test2', 'videos', video.uuid + '-720.mp4')
225 const fps = await getVideoFileFPS(path)
227 expect(fps).to.be.above(58).and.below(62)
231 it('Should wait transcoding before publishing the video', async function () {
235 // Upload the video, but wait transcoding
236 const videoAttributes = {
237 name: 'waiting video',
238 fixture: 'video_short1.webm',
239 waitTranscoding: true
241 const resVideo = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, videoAttributes)
242 const videoId = resVideo.body.video.uuid
244 // Should be in transcode state
245 const { body } = await getVideo(servers[ 1 ].url, videoId)
246 expect(body.name).to.equal('waiting video')
247 expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
248 expect(body.state.label).to.equal('To transcode')
249 expect(body.waitTranscoding).to.be.true
251 // Should have my video
252 const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
253 const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
254 expect(videoToFindInMine).not.to.be.undefined
255 expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
256 expect(videoToFindInMine.state.label).to.equal('To transcode')
257 expect(videoToFindInMine.waitTranscoding).to.be.true
259 // Should not list this video
260 const resVideos = await getVideosList(servers[1].url)
261 const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
262 expect(videoToFindInList).to.be.undefined
264 // Server 1 should not have the video yet
265 await getVideo(servers[0].url, videoId, 404)
268 await waitJobs(servers)
270 for (const server of servers) {
271 const res = await getVideosList(server.url)
272 const videoToFind = res.body.data.find(v => v.name === 'waiting video')
273 expect(videoToFind).not.to.be.undefined
275 const res2 = await getVideo(server.url, videoToFind.id)
276 const videoDetails: VideoDetails = res2.body
278 expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
279 expect(videoDetails.state.label).to.equal('Published')
280 expect(videoDetails.waitTranscoding).to.be.true
284 after(async function () {
285 killallServers(servers)