|
| 1 | +// |
| 2 | +// recorder.js |
| 3 | +// node-rtsp-recorder |
| 4 | +// |
| 5 | +// Created by Sahil Chaddha on 24/08/2018. |
| 6 | +// |
| 7 | + |
| 8 | +const moment = require('moment') |
| 9 | +const childProcess = require('child_process') |
| 10 | +const path = require('path') |
| 11 | +const FileHandler = require('./fileHandler') |
| 12 | + |
| 13 | +const fh = new FileHandler() |
| 14 | + |
| 15 | +const RTSPRecorder = class { |
| 16 | + constructor(config = {}) { |
| 17 | + this.config = config |
| 18 | + this.name = config.name |
| 19 | + this.url = config.url |
| 20 | + this.timeLimit = config.timeLimit || 60 |
| 21 | + this.folder = config.folder || 'media/' |
| 22 | + this.categoryType = config.type || 'video' |
| 23 | + fh.createDirIfNotExists(this.getDirectoryPath()) |
| 24 | + fh.createDirIfNotExists(this.getTodayPath()) |
| 25 | + } |
| 26 | + |
| 27 | + getDirectoryPath() { |
| 28 | + return path.join(this.folder, (this.name ? this.name : '')) |
| 29 | + } |
| 30 | + |
| 31 | + getTodayPath() { |
| 32 | + return path.join(this.getDirectoryPath(), moment().format('MMM-Do-YY')) |
| 33 | + } |
| 34 | + |
| 35 | + getMediaTypePath() { |
| 36 | + return path.join(this.getTodayPath(), this.categoryType) |
| 37 | + } |
| 38 | + |
| 39 | + getFilename(folderPath) { |
| 40 | + return path.join(folderPath, moment().format() + this.getExtenstion()) |
| 41 | + } |
| 42 | + |
| 43 | + getExtenstion() { |
| 44 | + if (this.categoryType === 'audio') { |
| 45 | + return '.avi' |
| 46 | + } |
| 47 | + if (this.categoryType === 'image') { |
| 48 | + return '.jpg' |
| 49 | + } |
| 50 | + |
| 51 | + return '.mp4' |
| 52 | + } |
| 53 | + |
| 54 | + getArguments() { |
| 55 | + if (this.categoryType === 'audio') { |
| 56 | + return ['-vn', '-acodec', 'copy'] |
| 57 | + } |
| 58 | + if (this.categoryType === 'image') { |
| 59 | + return ['-vframes', '1'] |
| 60 | + } |
| 61 | + return ['-acodec', 'copy', '-vcodec', 'copy'] |
| 62 | + } |
| 63 | + |
| 64 | + getChildProcess(fileName) { |
| 65 | + var args = ['-i', this.url] |
| 66 | + const mediaArgs = this.getArguments() |
| 67 | + mediaArgs.forEach((item) => { |
| 68 | + args.push(item) |
| 69 | + }) |
| 70 | + args.push(fileName) |
| 71 | + return childProcess.spawn('ffmpeg', |
| 72 | + args, |
| 73 | + { detached: false, stdio: 'ignore' }) |
| 74 | + } |
| 75 | + |
| 76 | + stopRecording() { |
| 77 | + this.disableStreaming = true |
| 78 | + if (this.timer) { |
| 79 | + clearTimeout(this.timer) |
| 80 | + this.timer = null |
| 81 | + } |
| 82 | + if (this.writeStream) { |
| 83 | + this.killStream() |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + startRecording() { |
| 88 | + if (!this.url) { |
| 89 | + console.log('URL Not Found.') |
| 90 | + return true |
| 91 | + } |
| 92 | + this.recordStream() |
| 93 | + } |
| 94 | + |
| 95 | + captureImage(cb) { |
| 96 | + this.writeStream = null |
| 97 | + const folderPath = this.getMediaTypePath() |
| 98 | + fh.createDirIfNotExists(folderPath) |
| 99 | + const fileName = this.getFilename(folderPath) |
| 100 | + this.writeStream = this.getChildProcess(fileName) |
| 101 | + this.writeStream.once('exit', () => { |
| 102 | + if (cb) { |
| 103 | + cb() |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + killStream() { |
| 109 | + this.writeStream.kill() |
| 110 | + } |
| 111 | + |
| 112 | + recordStream() { |
| 113 | + if (this.categoryType === 'image') { |
| 114 | + return |
| 115 | + } |
| 116 | + const self = this |
| 117 | + if (this.timer) { |
| 118 | + clearTimeout(this.timer) |
| 119 | + } |
| 120 | + |
| 121 | + if (this.writeStream && this.writeStream.binded) { |
| 122 | + return false |
| 123 | + } |
| 124 | + |
| 125 | + if (this.writeStream && this.writeStream.connected) { |
| 126 | + this.writeStream.binded = true |
| 127 | + this.writeStream.once('exit', () => { |
| 128 | + self.recordStream() |
| 129 | + }) |
| 130 | + this.killStream() |
| 131 | + return false |
| 132 | + } |
| 133 | + |
| 134 | + this.writeStream = null |
| 135 | + const folderPath = this.getMediaTypePath() |
| 136 | + fh.createDirIfNotExists(folderPath) |
| 137 | + const fileName = this.getFilename(folderPath) |
| 138 | + this.writeStream = this.getChildProcess(fileName) |
| 139 | + |
| 140 | + this.writeStream.once('exit', () => { |
| 141 | + if (self.disableStreaming) { |
| 142 | + return true |
| 143 | + } |
| 144 | + self.recordStream() |
| 145 | + }) |
| 146 | + this.timer = setTimeout(self.killStream.bind(this), this.timeLimit * 1000) |
| 147 | + |
| 148 | + console.log('Start record ' + fileName) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +module.exports = RTSPRecorder |
0 commit comments