shinobi-mp4frag 0.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +224 -0
- package/index.js +1146 -0
- package/lib/buffer-pool.js +44 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 - 2023 Kevin Godell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# mp4frag
|
|
2
|
+
|
|
3
|
+
###### [](https://github.com/kevinGodell/mp4frag/actions/workflows/node.js.yml) [](https://ci.appveyor.com/project/kevinGodell/mp4frag/branch/master) [](https://github.com/kevinGodell/mp4frag/issues) [](https://raw.githubusercontent.com/kevinGodell/mp4frag/master/LICENSE) [](https://www.npmjs.com/package/mp4frag)
|
|
4
|
+
|
|
5
|
+
A parser that reads piped data from ffmpeg containing a fragmented mp4 and splits it into an initialization segment and media segments. Designed for streaming live video relayed from cctv cameras.
|
|
6
|
+
|
|
7
|
+
***You must use the correct output args with ffmpeg to create a compatible fragmented mp4 format similar to the following real world examples:***
|
|
8
|
+
* `ffmpeg -loglevel quiet -rtsp_transport tcp -i rtsp://192.168.1.21:554/user=admin_password=pass_channel=0_stream=1.sdp?real_stream -reset_timestamps 1 -an -c:v copy -f mp4 -movflags +frag_every_frame+empty_moov+default_base_moof -min_frag_duration 500000 pipe:1`
|
|
9
|
+
* `ffmpeg -loglevel quiet -rtsp_transport tcp -i rtsp://192.168.1.18:554/user=admin&password=pass&channel=1&stream=1.sdp -reset_timestamps 1 -an -c:v copy -f mp4 -movflags +frag_keyframe+empty_moov+default_base_moof pipe:1`
|
|
10
|
+
|
|
11
|
+
### [Documents](https://kevingodell.github.io/mp4frag/) generated by jsdocs.
|
|
12
|
+
|
|
13
|
+
### Interesting projects using mp4frag:
|
|
14
|
+
* [Shinobi - Simple CCTV and NVR Solution](https://shinobi.video/)
|
|
15
|
+
* [Live Video Experience (LiVE)](https://video-experience.live/)
|
|
16
|
+
* [ffmpeg-streamer](https://github.com/kevinGodell/ffmpeg-streamer)
|
|
17
|
+
* [node-red-contrib-mp4frag](https://github.com/kevinGodell/node-red-contrib-mp4frag)
|
|
18
|
+
|
|
19
|
+
### Known Limitations:
|
|
20
|
+
* only supports fragmented mp4 video encoded with h.264, h.265, and aac
|
|
21
|
+
|
|
22
|
+
# Changes v0.6.1 => v0.7.0
|
|
23
|
+
* dropping support for node.js < 14
|
|
24
|
+
* experimental buffer pool support added
|
|
25
|
+
|
|
26
|
+
# Changes v0.6.0 => v0.6.1
|
|
27
|
+
* readableObjectMode can be set in [constructor](https://kevingodell.github.io/mp4frag/Mp4Frag.html#Mp4Frag) (defaults to false)
|
|
28
|
+
* piping and data event outputs segment buffer by default
|
|
29
|
+
|
|
30
|
+
# Changes v0.5.4 => v0.6.0
|
|
31
|
+
* dropping support for node.js < 10
|
|
32
|
+
* h.265 codec parsing
|
|
33
|
+
* [keyframe](https://kevingodell.github.io/mp4frag/Mp4Frag.html#keyframe) now returns a boolean
|
|
34
|
+
|
|
35
|
+
# Changes v0.5.2 => v0.5.4
|
|
36
|
+
* [getSegmentObject](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getSegmentObject) internal improvements
|
|
37
|
+
|
|
38
|
+
# Changes v0.5.1 => v0.5.2
|
|
39
|
+
### Property getters added
|
|
40
|
+
* [totalDuration](https://kevingodell.github.io/mp4frag/Mp4Frag.html#totalDuration)
|
|
41
|
+
* [totalByteLength](https://kevingodell.github.io/mp4frag/Mp4Frag.html#totalByteLength)
|
|
42
|
+
|
|
43
|
+
# Changes v0.5.0 => v0.5.1
|
|
44
|
+
* better media segment duration handling
|
|
45
|
+
|
|
46
|
+
# Changes v0.4.1 => v0.5.0
|
|
47
|
+
|
|
48
|
+
### Method signature change
|
|
49
|
+
* removed methods: getBuffer, getSegment, getSegmentList, getSegmentObjectList
|
|
50
|
+
|
|
51
|
+
# Changes v0.4.0 => v0.4.1
|
|
52
|
+
|
|
53
|
+
### Method signature change
|
|
54
|
+
* methods that retrieve segment buffer can now accept a stopping `count` parameter
|
|
55
|
+
* affects: [getBuffer](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getBuffer),
|
|
56
|
+
[getSegment](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getSegment), [getSegmentList](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getSegmentList),
|
|
57
|
+
[getSegmentObject](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getSegmentObject),
|
|
58
|
+
[getSegmentObjectList](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getSegmentObjectList)
|
|
59
|
+
|
|
60
|
+
# Changes v0.3.0 => v0.4.0
|
|
61
|
+
|
|
62
|
+
### SegmentObject changed
|
|
63
|
+
* keyframe property added to segmentObject `{ segment, sequence, duration, timestamp, keyframe }`
|
|
64
|
+
* segment contains a keyframe if `keyframe >= 0`
|
|
65
|
+
|
|
66
|
+
### Convenience methods added
|
|
67
|
+
* [getBuffer](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getBuffer)
|
|
68
|
+
* [getSegment](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getSegment)
|
|
69
|
+
* [getSegmentList](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getSegmentList)
|
|
70
|
+
* [getSegmentObject](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getSegmentObject)
|
|
71
|
+
* [getSegmentObjectList](https://kevingodell.github.io/mp4frag/Mp4Frag.html#getSegmentObjectList)
|
|
72
|
+
|
|
73
|
+
# Changes v0.2.0 => v0.3.0
|
|
74
|
+
|
|
75
|
+
### Constructor options changed ***---> BREAKING <---***
|
|
76
|
+
|
|
77
|
+
* `hlsBase` => `hlsPlaylistBase` _string_, accepts `_`, `a-z`, and `A-Z`
|
|
78
|
+
* `hlsSize` => `hlsPlaylistSize` _integer_, ranges from `2` to `20`, defaults to `4`
|
|
79
|
+
* `hlsInit` => `hlsPlaylistInit` _boolean_, defaults to `true`
|
|
80
|
+
* `bufferListSize` => `segmentCount` _integer_, ranges from `2` to `30`, defaults to `2`
|
|
81
|
+
|
|
82
|
+
### Segment event changed ***---> BREAKING <---***
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
mp4frag.on('segment', data => {
|
|
86
|
+
console.log(data);
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
* previously, data was a Buffer.
|
|
90
|
+
* currently, data is a segmentObject structured as `{ segment, sequence, duration, timestamp }`
|
|
91
|
+
|
|
92
|
+
# Options for a new instance of Mp4Frag
|
|
93
|
+
|
|
94
|
+
#### segmentCount: integer (2 - 30), *setting this value will store specified number of media segments in the buffer*
|
|
95
|
+
|
|
96
|
+
* will be ignored if setting `hlsPlaylistBase`
|
|
97
|
+
```javascript
|
|
98
|
+
const mp4frag = new Mp4Frag({segmentCount: 3});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### hlsPlaylistBase: string (`_`, `a-z`, and `A-Z`), *setting this will generate a live fmp4 HLS m3u8 playlist*
|
|
102
|
+
|
|
103
|
+
#### hlsPlaylistSize: integer (2 - 20), *setting this will determine the number of segments in the fmp4 HLS m3u8 playlist*
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
const mp4frag = new Mp4Frag({hlsPlaylistSize: 4, hlsPlaylistBase: 'my_String'});
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
# Possible usage examples
|
|
110
|
+
|
|
111
|
+
## Example 1: *Generate a live fmp4 HLS m3u8 playlist with ffmpeg*
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
const { spawn } = require('child_process');
|
|
115
|
+
|
|
116
|
+
const Mp4Frag = require('mp4frag');
|
|
117
|
+
|
|
118
|
+
const mp4frag = new Mp4Frag({hlsPlaylistSize: 3, hlsPlaylistBase: 'back_yard'});
|
|
119
|
+
|
|
120
|
+
const ffmpeg = spawn(
|
|
121
|
+
'ffmpeg',
|
|
122
|
+
['-loglevel', 'quiet', '-probesize', '64', '-analyzeduration', '100000', '-reorder_queue_size', '5', '-rtsp_transport', 'tcp', '-i', 'rtsp://216.4.116.29:554/axis-media/media.3gp', '-an', '-c:v', 'copy', '-f', 'mp4', '-movflags', '+frag_keyframe+empty_moov+default_base_moof', '-metadata', 'title="ip 216.4.116.29"', '-reset_timestamps', '1', 'pipe:1'],
|
|
123
|
+
{ stdio: ['ignore', 'pipe', 'inherit'] }
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
ffmpeg.stdio[1].pipe(mp4frag);
|
|
127
|
+
```
|
|
128
|
+
* **m3u8 playlist will now be available via `mp4frag.m3u8` and can be served to a client browser via express**
|
|
129
|
+
* **segments in playlist can be accessed by sequence number via `mp4frag.getSegmentObject(6)`, with `6` being the current sequence number**
|
|
130
|
+
|
|
131
|
+
#### Generated m3u8 playlist will look like the following example pulled from my live feed
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
#EXTM3U
|
|
135
|
+
#EXT-X-VERSION:7
|
|
136
|
+
#EXT-X-ALLOW-CACHE:NO
|
|
137
|
+
#EXT-X-TARGETDURATION:4
|
|
138
|
+
#EXT-X-MEDIA-SEQUENCE:6
|
|
139
|
+
#EXT-X-MAP:URI="init-back_yard.mp4"
|
|
140
|
+
#EXTINF:4.780000,
|
|
141
|
+
back_yard6.m4s
|
|
142
|
+
#EXTINF:5.439000,
|
|
143
|
+
back_yard7.m4s
|
|
144
|
+
#EXTINF:4.269000,
|
|
145
|
+
back_yard8.m4s
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
#### Setting up server routes to respond to http requests for playing live HLS feed
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
app.get('/back_yard.m3u8', (req, res) => {
|
|
152
|
+
if (mp4frag.m3u8) {
|
|
153
|
+
res.writeHead(200, {'Content-Type': 'application/vnd.apple.mpegurl'});
|
|
154
|
+
res.end(mp4frag.m3u8);
|
|
155
|
+
} else {
|
|
156
|
+
res.sendStatus(503);
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
app.get('/init-back_yard.mp4', (req, res) => {
|
|
161
|
+
if (mp4frag.initialization) {
|
|
162
|
+
res.writeHead(200, {'Content-Type': 'video/mp4'});
|
|
163
|
+
res.end(mp4frag.initialization);
|
|
164
|
+
} else {
|
|
165
|
+
res.sendStatus(503);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
app.get('/back_yard:id.m4s', (req, res) => {
|
|
170
|
+
const segmentObject = mp4frag.getSegmentObject(req.params.id);
|
|
171
|
+
|
|
172
|
+
if (segmentObject) {
|
|
173
|
+
res.writeHead(200, {'Content-Type': 'video/mp4'});
|
|
174
|
+
res.end(segmentObject.segment);
|
|
175
|
+
} else {
|
|
176
|
+
res.sendStatus(503);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Example 2: *Create a buffer of past video to store for later recording*
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
const { spawn } = require('child_process');
|
|
185
|
+
|
|
186
|
+
const Mp4Frag = require('mp4frag');
|
|
187
|
+
|
|
188
|
+
// 3 past segments will be held in memory for later access via mp4frag.segmentObjects
|
|
189
|
+
// if each segment has a duration of 2 seconds, then buffer will contain 6 seconds of video
|
|
190
|
+
const mp4frag = new Mp4Frag({segmentCount: 3});
|
|
191
|
+
|
|
192
|
+
const ffmpeg = spawn(
|
|
193
|
+
'ffmpeg',
|
|
194
|
+
['-loglevel', 'quiet', '-probesize', '64', '-analyzeduration', '100000', '-reorder_queue_size', '5', '-rtsp_transport', 'tcp', '-i', 'rtsp://131.95.3.162:554/axis-media/media.3gp', '-an', '-c:v', 'copy', '-f', 'mp4', '-movflags', '+frag_keyframe+empty_moov+default_base_moof', '-metadata', 'title="ip 131.95.3.162"', '-reset_timestamps', '1', 'pipe:1'],
|
|
195
|
+
{ stdio: ['ignore', 'pipe', 'inherit'] }
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
ffmpeg.stdio[1].pipe(mp4frag);
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
##### Moments later, a triggering event occurs such as motion detection, and we need to record buffered video from before the event occurred:
|
|
202
|
+
|
|
203
|
+
```javascript
|
|
204
|
+
const fs = require('fs');
|
|
205
|
+
|
|
206
|
+
const { initialization, segmentObjects } = mp4frag;
|
|
207
|
+
|
|
208
|
+
if (initialization && segmentObjects) {
|
|
209
|
+
const fileName = `${Date.now()}.mp4`;
|
|
210
|
+
|
|
211
|
+
const writeStream = fs.createWriteStream(fileName);
|
|
212
|
+
|
|
213
|
+
// write the initialization fragment
|
|
214
|
+
writeStream.write(initialization);
|
|
215
|
+
|
|
216
|
+
// write the media segments
|
|
217
|
+
segmentObjects.forEach(({segment}) => {
|
|
218
|
+
writeStream.write(segment);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// end
|
|
222
|
+
writeStream.end();
|
|
223
|
+
}
|
|
224
|
+
```
|