adding tests for audio conversions
[oweals/peertube.git] / server / tests / api / videos / video-transcoder.ts
1 /* tslint:disable:no-unused-expression */
2
3 import * as chai from 'chai'
4 import 'mocha'
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'
9 import {
10   buildAbsoluteFixturePath,
11   doubleFollow,
12   flushAndRunMultipleServers,
13   getMyVideos,
14   getVideo,
15   getVideosList,
16   killallServers,
17   root,
18   ServerInfo,
19   setAccessTokensToServers,
20   uploadVideo,
21   webtorrentAdd
22 } from '../../utils'
23 import { join } from 'path'
24 import { waitJobs } from '../../utils/server/jobs'
25
26 const expect = chai.expect
27
28 describe('Test video transcoding', function () {
29   let servers: ServerInfo[] = []
30
31   before(async function () {
32     this.timeout(30000)
33
34     // Run servers
35     servers = await flushAndRunMultipleServers(2)
36
37     await setAccessTokensToServers(servers)
38   })
39
40   it('Should not transcode video on server 1', async function () {
41     this.timeout(60000)
42
43     const videoAttributes = {
44       name: 'my super name for server 1',
45       description: 'my super description for server 1',
46       fixture: 'video_short.webm'
47     }
48     await uploadVideo(servers[0].url, servers[0].accessToken, videoAttributes)
49
50     await waitJobs(servers)
51
52     const res = await getVideosList(servers[0].url)
53     const video = res.body.data[0]
54
55     const res2 = await getVideo(servers[0].url, video.id)
56     const videoDetails = res2.body
57     expect(videoDetails.files).to.have.lengthOf(1)
58
59     const magnetUri = videoDetails.files[0].magnetUri
60     expect(magnetUri).to.match(/\.webm/)
61
62     const torrent = await webtorrentAdd(magnetUri)
63     expect(torrent.files).to.be.an('array')
64     expect(torrent.files.length).to.equal(1)
65     expect(torrent.files[0].path).match(/\.webm$/)
66   })
67
68   it('Should transcode video on server 2', async function () {
69     this.timeout(60000)
70
71     const videoAttributes = {
72       name: 'my super name for server 2',
73       description: 'my super description for server 2',
74       fixture: 'video_short.webm'
75     }
76     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
77
78     await waitJobs(servers)
79
80     const res = await getVideosList(servers[1].url)
81
82     const video = res.body.data[0]
83     const res2 = await getVideo(servers[1].url, video.id)
84     const videoDetails = res2.body
85
86     expect(videoDetails.files).to.have.lengthOf(4)
87
88     const magnetUri = videoDetails.files[0].magnetUri
89     expect(magnetUri).to.match(/\.mp4/)
90
91     const torrent = await webtorrentAdd(magnetUri)
92     expect(torrent.files).to.be.an('array')
93     expect(torrent.files.length).to.equal(1)
94     expect(torrent.files[0].path).match(/\.mp4$/)
95   })
96
97   it('Should transcode high bit rate mp3 to proper bit rate', async function () {
98     this.timeout(60000)
99
100     const videoAttributes = {
101       name: 'mp3_256k',
102       fixture: 'video_short_mp3_256k.mp4'
103     }
104     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
105
106     await waitJobs(servers)
107
108     const res = await getVideosList(servers[1].url)
109
110     const video = res.body.data.find(v => v.name === videoAttributes.name)
111     const res2 = await getVideo(servers[1].url, video.id)
112     const videoDetails: VideoDetails = res2.body
113
114     expect(videoDetails.files).to.have.lengthOf(4)
115
116     const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
117     const probe = await audio.get(ffmpeg, path)
118
119     if (probe.audioStream) {
120       expect(probe.audioStream['codec_name']).to.be.equal('aac')
121       expect(probe.audioStream['bit_rate']).to.be.at.most(384 * 8000)
122     } else {
123       this.fail('Could not retrieve the audio stream on ' + probe.absolutePath)
124     }
125   })
126
127   it('Should transcode video with no audio and have no audio itself', async function () {
128     this.timeout(60000)
129
130     const videoAttributes = {
131       name: 'no_audio',
132       fixture: 'video_short_no_audio.mp4'
133     }
134     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
135
136     await waitJobs(servers)
137
138     const res = await getVideosList(servers[1].url)
139
140     const video = res.body.data.find(v => v.name === videoAttributes.name)
141     const res2 = await getVideo(servers[1].url, video.id)
142     const videoDetails: VideoDetails = res2.body
143
144     expect(videoDetails.files).to.have.lengthOf(4)
145     const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
146     const probe = await audio.get(ffmpeg, path)
147     expect(probe).to.not.have.property('audioStream')
148   })
149
150   it('Should leave the audio untouched, but properly transcode the video', async function () {
151     this.timeout(60000)
152
153     const videoAttributes = {
154       name: 'untouched_audio',
155       fixture: 'video_short.mp4'
156     }
157     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
158
159     await waitJobs(servers)
160
161     const res = await getVideosList(servers[1].url)
162
163     const video = res.body.data.find(v => v.name === videoAttributes.name)
164     const res2 = await getVideo(servers[1].url, video.id)
165     const videoDetails: VideoDetails = res2.body
166
167     expect(videoDetails.files).to.have.lengthOf(4)
168     const fixturePath = buildAbsoluteFixturePath(videoAttributes.fixture)
169     const fixtureVideoProbe = await audio.get(ffmpeg, fixturePath)
170     const path = join(root(), 'test2', 'videos', video.uuid + '-240.mp4')
171     const videoProbe = await audio.get(ffmpeg, path)
172     if (videoProbe.audioStream && fixtureVideoProbe.audioStream) {
173       const toOmit = [ 'max_bit_rate', 'duration', 'duration_ts', 'nb_frames', 'start_time', 'start_pts' ]
174       expect(omit(videoProbe.audioStream, toOmit)).to.be.deep.equal(omit(fixtureVideoProbe.audioStream, toOmit))
175     } else {
176       this.fail('Could not retrieve the audio stream on ' + videoProbe.absolutePath)
177     }
178   })
179
180   it('Should transcode a 60 FPS video', async function () {
181     this.timeout(60000)
182
183     const videoAttributes = {
184       name: 'my super 30fps name for server 2',
185       description: 'my super 30fps description for server 2',
186       fixture: '60fps_720p_small.mp4'
187     }
188     await uploadVideo(servers[1].url, servers[1].accessToken, videoAttributes)
189
190     await waitJobs(servers)
191
192     const res = await getVideosList(servers[1].url)
193
194     const video = res.body.data.find(v => v.name === videoAttributes.name)
195     const res2 = await getVideo(servers[1].url, video.id)
196     const videoDetails: VideoDetails = res2.body
197
198     expect(videoDetails.files).to.have.lengthOf(4)
199     expect(videoDetails.files[0].fps).to.be.above(58).and.below(62)
200     expect(videoDetails.files[1].fps).to.be.below(31)
201     expect(videoDetails.files[2].fps).to.be.below(31)
202     expect(videoDetails.files[3].fps).to.be.below(31)
203
204     for (const resolution of [ '240', '360', '480' ]) {
205       const path = join(root(), 'test2', 'videos', video.uuid + '-' + resolution + '.mp4')
206       const fps = await getVideoFileFPS(path)
207
208       expect(fps).to.be.below(31)
209     }
210
211     const path = join(root(), 'test2', 'videos', video.uuid + '-720.mp4')
212     const fps = await getVideoFileFPS(path)
213
214     expect(fps).to.be.above(58).and.below(62)
215   })
216
217   it('Should wait transcoding before publishing the video', async function () {
218     this.timeout(80000)
219
220     await doubleFollow(servers[0], servers[1])
221
222     await waitJobs(servers)
223
224     {
225       // Upload the video, but wait transcoding
226       const videoAttributes = {
227         name: 'waiting video',
228         fixture: 'video_short1.webm',
229         waitTranscoding: true
230       }
231       const resVideo = await uploadVideo(servers[ 1 ].url, servers[ 1 ].accessToken, videoAttributes)
232       const videoId = resVideo.body.video.uuid
233
234       // Should be in transcode state
235       const { body } = await getVideo(servers[ 1 ].url, videoId)
236       expect(body.name).to.equal('waiting video')
237       expect(body.state.id).to.equal(VideoState.TO_TRANSCODE)
238       expect(body.state.label).to.equal('To transcode')
239       expect(body.waitTranscoding).to.be.true
240
241       // Should have my video
242       const resMyVideos = await getMyVideos(servers[1].url, servers[1].accessToken, 0, 10)
243       const videoToFindInMine = resMyVideos.body.data.find(v => v.name === videoAttributes.name)
244       expect(videoToFindInMine).not.to.be.undefined
245       expect(videoToFindInMine.state.id).to.equal(VideoState.TO_TRANSCODE)
246       expect(videoToFindInMine.state.label).to.equal('To transcode')
247       expect(videoToFindInMine.waitTranscoding).to.be.true
248
249       // Should not list this video
250       const resVideos = await getVideosList(servers[1].url)
251       const videoToFindInList = resVideos.body.data.find(v => v.name === videoAttributes.name)
252       expect(videoToFindInList).to.be.undefined
253
254       // Server 1 should not have the video yet
255       await getVideo(servers[0].url, videoId, 404)
256     }
257
258     await waitJobs(servers)
259
260     for (const server of servers) {
261       const res = await getVideosList(server.url)
262       const videoToFind = res.body.data.find(v => v.name === 'waiting video')
263       expect(videoToFind).not.to.be.undefined
264
265       const res2 = await getVideo(server.url, videoToFind.id)
266       const videoDetails: VideoDetails = res2.body
267
268       expect(videoDetails.state.id).to.equal(VideoState.PUBLISHED)
269       expect(videoDetails.state.label).to.equal('Published')
270       expect(videoDetails.waitTranscoding).to.be.true
271     }
272   })
273
274   after(async function () {
275     killallServers(servers)
276   })
277 })