aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-12-12 10:54:01 +0100
committerChocobozzz <me@florianbigard.com>2019-12-12 10:56:34 +0100
commit907b8f9347cca117c77a83cbea27140a10e3311e (patch)
tree1d9dd354e8bcd326692329ba780c675feeebe8e8
parent458218d2baf286835754c666cfc635cce467c24e (diff)
downloadPeerTube-907b8f9347cca117c77a83cbea27140a10e3311e.tar.gz
PeerTube-907b8f9347cca117c77a83cbea27140a10e3311e.tar.zst
PeerTube-907b8f9347cca117c77a83cbea27140a10e3311e.zip
Use a migration script to generate HLS torrent files
-rw-r--r--scripts/migrations/peertube-2.1.ts77
-rw-r--r--server/initializers/migrations/0450-streaming-playlist-files.ts55
-rw-r--r--server/models/video/video-file.ts2
3 files changed, 78 insertions, 56 deletions
diff --git a/scripts/migrations/peertube-2.1.ts b/scripts/migrations/peertube-2.1.ts
new file mode 100644
index 000000000..892497a88
--- /dev/null
+++ b/scripts/migrations/peertube-2.1.ts
@@ -0,0 +1,77 @@
1import { registerTSPaths } from '../../server/helpers/register-ts-paths'
2registerTSPaths()
3
4import { initDatabaseModels, sequelizeTypescript } from '../../server/initializers'
5import * as Sequelize from 'sequelize'
6import { join } from 'path'
7import { HLS_STREAMING_PLAYLIST_DIRECTORY, STATIC_PATHS, WEBSERVER } from '@server/initializers/constants'
8import { pathExists, stat, writeFile } from 'fs-extra'
9import { createTorrentPromise } from '@server/helpers/webtorrent'
10import { CONFIG } from '@server/initializers/config'
11import * as parseTorrent from 'parse-torrent'
12import { logger } from '@server/helpers/logger'
13
14run()
15 .then(() => process.exit(0))
16 .catch(err => {
17 console.error(err)
18 process.exit(-1)
19 })
20
21async function run () {
22 logger.info('Creating torrents and updating database for HSL files.')
23
24 await initDatabaseModels(true)
25
26 const query = 'select "videoFile".id as id, "videoFile".resolution as resolution, "video".uuid as uuid from "videoFile" ' +
27 'inner join "videoStreamingPlaylist" ON "videoStreamingPlaylist".id = "videoFile"."videoStreamingPlaylistId" ' +
28 'inner join video ON video.id = "videoStreamingPlaylist"."videoId" ' +
29 'WHERE video.remote IS FALSE'
30 const options = {
31 type: Sequelize.QueryTypes.SELECT
32 }
33 const res = await sequelizeTypescript.query(query, options)
34
35 for (const row of res) {
36 const videoFilename = `${row['uuid']}-${row['resolution']}-fragmented.mp4`
37 const videoFilePath = join(HLS_STREAMING_PLAYLIST_DIRECTORY, row['uuid'], videoFilename)
38
39 logger.info('Processing %s.', videoFilePath)
40
41 if (!await pathExists(videoFilePath)) {
42 console.warn('Cannot generate torrent of %s: file does not exist.', videoFilePath)
43 continue
44 }
45
46 const createTorrentOptions = {
47 // Keep the extname, it's used by the client to stream the file inside a web browser
48 name: `video ${row['uuid']}`,
49 createdBy: 'PeerTube',
50 announceList: [
51 [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
52 [ WEBSERVER.URL + '/tracker/announce' ]
53 ],
54 urlList: [ WEBSERVER.URL + join(STATIC_PATHS.STREAMING_PLAYLISTS.HLS, row['uuid'], videoFilename) ]
55 }
56 const torrent = await createTorrentPromise(videoFilePath, createTorrentOptions)
57
58 const torrentName = `${row['uuid']}-${row['resolution']}-hls.torrent`
59 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentName)
60
61 await writeFile(filePath, torrent)
62
63 const parsedTorrent = parseTorrent(torrent)
64 const infoHash = parsedTorrent.infoHash
65
66 const stats = await stat(videoFilePath)
67 const size = stats.size
68
69 const queryUpdate = 'UPDATE "videoFile" SET "infoHash" = ?, "size" = ? WHERE id = ?'
70
71 const options = {
72 type: Sequelize.QueryTypes.UPDATE,
73 replacements: [ infoHash, size, row['id'] ]
74 }
75 await sequelizeTypescript.query(queryUpdate, options)
76 }
77}
diff --git a/server/initializers/migrations/0450-streaming-playlist-files.ts b/server/initializers/migrations/0450-streaming-playlist-files.ts
index 4e177bef8..460dac8be 100644
--- a/server/initializers/migrations/0450-streaming-playlist-files.ts
+++ b/server/initializers/migrations/0450-streaming-playlist-files.ts
@@ -48,61 +48,6 @@ async function up (utils: {
48 48
49 await utils.sequelize.query(query, { transaction: utils.transaction }) 49 await utils.sequelize.query(query, { transaction: utils.transaction })
50 } 50 }
51
52 {
53 const query = 'select "videoFile".id as id, "videoFile".resolution as resolution, "video".uuid as uuid from "videoFile" ' +
54 'inner join "videoStreamingPlaylist" ON "videoStreamingPlaylist".id = "videoFile"."videoStreamingPlaylistId" ' +
55 'inner join video ON video.id = "videoStreamingPlaylist"."videoId" ' +
56 'WHERE video.remote IS FALSE'
57 const options = {
58 type: Sequelize.QueryTypes.SELECT,
59 transaction: utils.transaction
60 }
61 const res = await utils.sequelize.query(query, options)
62
63 for (const row of res) {
64 const videoFilename = `${row['uuid']}-${row['resolution']}-fragmented.mp4`
65 const videoFilePath = join(HLS_STREAMING_PLAYLIST_DIRECTORY, row['uuid'], videoFilename)
66
67 if (!await pathExists(videoFilePath)) {
68 console.warn('Cannot generate torrent of %s: file does not exist.', videoFilePath)
69 continue
70 }
71
72 const createTorrentOptions = {
73 // Keep the extname, it's used by the client to stream the file inside a web browser
74 name: `video ${row['uuid']}`,
75 createdBy: 'PeerTube',
76 announceList: [
77 [ WEBSERVER.WS + '://' + WEBSERVER.HOSTNAME + ':' + WEBSERVER.PORT + '/tracker/socket' ],
78 [ WEBSERVER.URL + '/tracker/announce' ]
79 ],
80 urlList: [ WEBSERVER.URL + join(HLS_STREAMING_PLAYLIST_DIRECTORY, row['uuid'], videoFilename) ]
81 }
82 const torrent = await createTorrentPromise(videoFilePath, createTorrentOptions)
83
84 const torrentName = `${row['uuid']}-${row['resolution']}-hls.torrent`
85 const filePath = join(CONFIG.STORAGE.TORRENTS_DIR, torrentName)
86
87 await writeFile(filePath, torrent)
88
89 const parsedTorrent = parseTorrent(torrent)
90 const infoHash = parsedTorrent.infoHash
91
92 const stats = await stat(videoFilePath)
93 const size = stats.size
94
95 const queryUpdate = 'UPDATE "videoFile" SET "infoHash" = ?, "size" = ? WHERE id = ?'
96
97 const options = {
98 type: Sequelize.QueryTypes.UPDATE,
99 replacements: [ infoHash, size, row['id'] ],
100 transaction: utils.transaction
101 }
102 await utils.sequelize.query(queryUpdate, options)
103
104 }
105 }
106} 51}
107 52
108function down (options) { 53function down (options) {
diff --git a/server/models/video/video-file.ts b/server/models/video/video-file.ts
index cacef0106..fa5a6e76d 100644
--- a/server/models/video/video-file.ts
+++ b/server/models/video/video-file.ts
@@ -26,7 +26,7 @@ import { VideoStreamingPlaylistModel } from './video-streaming-playlist'
26import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize' 26import { FindOptions, Op, QueryTypes, Transaction } from 'sequelize'
27import { MIMETYPES } from '../../initializers/constants' 27import { MIMETYPES } from '../../initializers/constants'
28import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../typings/models/video/video-file' 28import { MVideoFile, MVideoFileStreamingPlaylistVideo, MVideoFileVideo } from '../../typings/models/video/video-file'
29import { MStreamingPlaylist, MStreamingPlaylistVideo, MVideo } from '@server/typings/models' 29import { MStreamingPlaylistVideo, MVideo } from '@server/typings/models'
30 30
31@Table({ 31@Table({
32 tableName: 'videoFile', 32 tableName: 'videoFile',