]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/videos.js
Move video duration logic in lib/
[github/Chocobozzz/PeerTube.git] / server / lib / videos.js
1 'use strict'
2
3 const async = require('async')
4 const config = require('config')
5 const ffmpeg = require('fluent-ffmpeg')
6 const pathUtils = require('path')
7 const webtorrent = require('../lib/webtorrent')
8
9 const logger = require('../helpers/logger')
10 const Videos = require('../models/videos')
11
12 const uploadDir = pathUtils.join(__dirname, '..', '..', config.get('storage.uploads'))
13
14 const videos = {
15 getVideoDuration: getVideoDuration,
16 getVideoState: getVideoState,
17 seed: seed,
18 seedAllExisting: seedAllExisting
19 }
20
21 function getVideoDuration (video_path, callback) {
22 ffmpeg.ffprobe(video_path, function (err, metadata) {
23 if (err) return callback(err)
24
25 return callback(null, Math.floor(metadata.format.duration))
26 })
27 }
28
29 function getVideoState (video) {
30 const exist = (video !== null)
31 let owned = false
32 if (exist === true) {
33 owned = (video.namePath !== null)
34 }
35
36 return { exist: exist, owned: owned }
37 }
38
39 function seed (path, callback) {
40 logger.info('Seeding %s...', path)
41
42 webtorrent.seed(path, function (torrent) {
43 logger.info('%s seeded (%s).', path, torrent.magnetURI)
44
45 return callback(null, torrent)
46 })
47 }
48
49 function seedAllExisting (callback) {
50 Videos.listOwned(function (err, videos_list) {
51 if (err) {
52 logger.error('Cannot get list of the videos to seed.')
53 return callback(err)
54 }
55
56 async.each(videos_list, function (video, each_callback) {
57 seed(uploadDir + video.namePath, function (err) {
58 if (err) {
59 logger.error('Cannot seed this video.')
60 return callback(err)
61 }
62
63 each_callback(null)
64 })
65 }, callback)
66 })
67 }
68
69 // ---------------------------------------------------------------------------
70
71 module.exports = videos