The problem is that the MP4 files recorded by the DVR cannot be played on Safari browser, but they can be played on Google Chrome browser when hosted on an nginx server.
qzeno opened this issue · comments
The video recorded using DVR is saved with the file name extension as mp4. To play this mp4 file, place it in the nginx directory and open the file in a browser for on-demand playback. It can be opened in Google Chrome and Android phones, but cannot be played in Firefox and Safari browsers. To enable playback in Safari, you can repackage the video using the command "ffmpeg -i abc.mp4 -codec copy test.mp4".
How can the recorded video be played in Safari without re-packaging?
Below is the DVR configuration:
dvr{
enabled on;
dvr_apply all;
dvr_plan session;
dvr_path /usr/local/nginx/html/dvr/[2006]/[01]/[stream].mp4;
dvr_duration 30;
dvr_wait_keyframe on;
time_jitter full;
}
TRANS_BY_GPT3
Can the mp4 file provide a sample?
TRANS_BY_GPT3
I added an attachment in the email and sent it. Can you receive it?
TRANS_BY_GPT3
The link is invalid
TRANS_BY_GPT3
FireFox browser abc.mp4 also cannot play. I will check it tomorrow.
TRANS_BY_GPT3
Thanks in advance, bro.
TRANS_BY_GPT3
Sorry, I have been busy these past few days and haven't had time to carefully read your question. However, while investigating the issue of some flv files not playing, I found a similar situation. I should have some clues. I will reply to you later.
TRANS_BY_GPT3
This may be related to audio encoding. Check whether your audio encoding is MAIN-AAC or LC-AAC. Some players do not support MAIN-AAC.
TRANS_BY_GPT3
It seems there is a bug with the MP4. A friend said they submitted a PR, but it seems I haven't seen it.
TRANS_BY_GPT3
How to configure this to save as mp4? Every time I save it, it becomes flv instead.
TRANS_BY_GPT3
Hello, how do I record mp4 files? I have set the .MP4 extension, but it still generates flv files???
TRANS_BY_GPT3
Sorry, I've been busy these days and didn't have time to carefully read your question.
However, while investigating the issue of some flv files not playing, I found a similar situation.
I should have some clues. I will reply to you later.
Brother, regarding the issue of MP4 recording that can only be played on Google, can you give me some suggestions?
TRANS_BY_GPT3
Record two MP4 files using SRS and FFMPEG, refer to the attachments below:
ffmpeg.tar.gz
srs.tar.gz
Can be viewed using VLC, FFMPEG, and Chrome:
http://localhost:8080/srs.mp4
http://localhost:8080/ffmpeg.mp4
Can be wrapped with H5:
<video controls="controls" autoplay="autoplay" width="768" height="320">
<source src="ffmpeg.mp4" type="video/mp4"/>
</video>
<video controls="controls" autoplay="autoplay" width="768" height="320">
<source src="srs.mp4" type="video/mp4"/>
</video>
FFMPEG
You can see that Chrome actually initiates multiple Range requests, the first one:
GET /ffmpeg.mp4 HTTP/1.1
Range: bytes=0-
HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: video/mp4
Last-Modified: Thu, 26 Dec 2019 07:19:37 GMT
Server: Oryx/0.0.4
Date: Thu, 26 Dec 2019 07:40:57 GMT
Content-Range: bytes 0-1005528/1005529
Content-Length: 1005529
The second Range request initiated by Chrome is as follows:
GET /ffmpeg.mp4 HTTP/1.1
Range: bytes=950272-
HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: video/mp4
Last-Modified: Thu, 26 Dec 2019 07:19:37 GMT
Server: Oryx/0.0.4
Date: Thu, 26 Dec 2019 07:40:57 GMT
Content-Range: bytes 950272-1005528/1005529
Content-Length: 55257
The third request initiated by Chrome is as follows:
GET /ffmpeg.mp4 HTTP/1.1
Range: bytes=65536-
HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: video/mp4
Last-Modified: Thu, 26 Dec 2019 07:19:37 GMT
Server: Oryx/0.0.4
Date: Thu, 26 Dec 2019 07:40:57 GMT
Content-Range: bytes 65536-1005528/1005529
Content-Length: 939993
When playing, Chrome initiated another request:
GET /ffmpeg.mp4 HTTP/1.1
Range: bytes=0-
HTTP/1.1 206 Partial Content
Accept-Ranges: bytes
Content-Type: video/mp4
Last-Modified: Thu, 26 Dec 2019 07:19:37 GMT
Server: Oryx/0.0.4
Date: Thu, 26 Dec 2019 07:45:25 GMT
Content-Range: bytes 0-1005528/1005529
Content-Length: 1005529
Below is Safari's request:
The MP4 generated by FFMPEG can be played on various browsers.
SRS
For files generated by SRS, VLC and Chrome can play them, Safari cannot open them, and Firefox only plays audio without video.
Comparing the descriptions of the two files using FFMPEG, there are no significant differences. It appears that the encoding is not a problem.
Mac:html chengli.ycl$ ffmpeg -i ffmpeg.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'ffmpeg.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
Duration: 00:00:28.48, start: 0.000000, bitrate: 282 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 768x320 [SAR 1:1 DAR 12:5], 245 kb/s, 25 fps, 25 tbr, 16k tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 30 kb/s (default)
Metadata:
handler_name : SoundHandler
Mac:html chengli.ycl$ ffmpeg -i srs.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'srs.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
Duration: 00:00:28.64, start: 0.000000, bitrate: 281 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 768x320 [SAR 1:1 DAR 12:5], 244 kb/s, 25.03 fps, 25 tbr, 1k tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 30 kb/s (default)
Metadata:
handler_name : SoundHandler
Additionally, in the Finder on Mac, the thumbnail of srs.mp4 cannot be seen.
Using the tool provided by SRS, srs_mp4_parser, let's check the format of the two files.
[root@a09d8d5e3210 trunk]# ./objs/srs_mp4_parser objs/nginx/html/ffmpeg.mp4
ftyp, 32B, brands:isom,512(isom,iso2,avc1,mp41)
free, 8B, free 0B
mdat, 982191B, total 982183 bytes
moov, 23298B, 4 boxes
mvhd, 108B, FB(4B), 28480ms, TBN=1000, nTID=3
trak, 10140B, 3 boxes
tkhd, 92B, FB(4B,V0,0x03), track #1, 28480TBN, size=768x320
edts, 48B, 1 boxes
elst, 40B, FB(4B), 2 childs(+)
Entry, 46TBN, start=-1TBN, rate=1,0
Entry, 28480TBN, start=1280TBN, rate=1,0
mdia, 9992B, 3 boxes
mdhd, 32B, FB(4B), TBN=16000, 455680TBN, LANG=und
hdlr, 45B, FB(4B), vide, VideoHandler
minf, 9907B, 3 boxes
vmhd, 20B, FB(4B,V0,0x01)
dinf, 36B, 1 boxes
dref, 28B, FB(4B), 1 childs(+)
URL: Same file
stbl, 9843B, 7 boxes
stsd, 167B, FB(4B), 1 childs(+)
avc1, 151B, refs#1, size=768x320, 2 boxes
avcC, 49B, AVC Config: 41B
0x01, 0x64, 0x00, 0x20, 0xff, 0xe1, 0x00, 0x19,
0x67, 0x64, 0x00, 0x20, 0xac, 0xd9, 0x40, 0xc0,
0x29, 0xb0, 0x11, 0x00, 0x00, 0x03, 0x00, 0x01,
0x00, 0x00, 0x03, 0x00, 0x32, 0x0f, 0x18, 0x31,
0x96, 0x01, 0x00, 0x05, 0x68, 0xeb, 0xec, 0xb2,
0x2c
pasp, 16B, free 8B
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01
stts, 24B, FB(4B), 1 childs (+)
count=712, delta=640
stss, 60B, FB(4B), count=11
1, 126, 170, 228, 370, 413, 466, 496
ctts, 3824B, FB(4B), 476 childs (+)
count=2, offset=1280
count=1, offset=3200
count=1, offset=1280
count=1, offset=0
count=1, offset=640
count=1, offset=3200
count=1, offset=1280
count=1, offset=0
stsc, 28B, FB(4B), 1 childs (+)
first=1, samples=1, index=1
stsz, 2868B, FB(4B), size=0, 712 childs (+)
5132, 127, 984, 50, 57, 20, 188, 35
stco, 2864B, FB(4B), 712 childs (+)
48, 5267, 5481, 6639, 6863, 7094, 7201, 7563
trak, 12944B, 3 boxes
tkhd, 92B, FB(4B,V0,0x03), track #2, 28421TBN, volume=1.0
edts, 36B, 1 boxes
elst, 28B, FB(4B), 1 childs(+)
Entry, 28421TBN, start=0TBN, rate=1,0
mdia, 12808B, 3 boxes
mdhd, 32B, FB(4B), TBN=44100, 1253366TBN, LANG=und
hdlr, 45B, FB(4B), soun, SoundHandler
minf, 12723B, 3 boxes
smhd, 16B, FB(4B)
dinf, 36B, 1 boxes
dref, 28B, FB(4B), 1 childs(+)
URL: Same file
stbl, 12663B, 5 boxes
stsd, 103B, FB(4B), 1 childs(+)
mp4a, 87B, refs#1, 2 channels, 16 bits, 44100 Hz, 1 boxes
esds, 51B, FB(4B), tag=0x03, ID=2
decoder config, tag=0x04, type=64, stream=5
decoder specific, tag=0x05, ASC 2B
0x12, 0x10
stts, 32B, FB(4B), 2 childs (+)
count=1223, delta=1024
count=1, delta=1014
stsc, 4744B, FB(4B), 394 childs (+)
first=1, samples=1, index=1
first=3, samples=2, index=1
first=6, samples=1, index=1
first=7, samples=2, index=1
first=10, samples=1, index=1
first=11, samples=2, index=1
first=13, samples=1, index=1
first=14, samples=2, index=1
stsz, 4916B, FB(4B), size=0, 1224 childs (+)
87, 87, 87, 87, 87, 87, 87, 87
stco, 2860B, FB(4B), 711 childs (+)
5180, 5394, 6465, 6689, 6920, 7114, 7389, 7598
udta, 98B, total 90B
0x00, 0x00, 0x00, 0x5a, 0x6d, 0x65, 0x74, 0x61
[root@a09d8d5e3210 trunk]# ./objs/srs_mp4_parser objs/nginx/html/srs.mp4
ftyp, 32B, brands:isom,512(isom,iso2,avc1,mp41)
mdat, 984086B, total 984078 bytes
moov, 24908B, 3 boxes
mvhd, 108B, FB(4B), 28640ms, TBN=1000, nTID=3
trak, 10168B, 2 boxes
tkhd, 92B, FB(4B,V0,0x03), track #1, 28640TBN
mdia, 10068B, 3 boxes
mdhd, 32B, FB(4B), TBN=1000, 28640TBN, LANG=und
hdlr, 45B, FB(4B), vide, VideoHandler
minf, 9983B, 3 boxes
vmhd, 20B, FB(4B,V0,0x01)
dinf, 36B, 1 boxes
dref, 28B, FB(4B), 1 childs(+)
URL: Same file
stbl, 9919B, 7 boxes
stsd, 151B, FB(4B), 1 childs(+)
avc1, 135B, refs#0, size=0x0, 1 boxes
avcC, 49B, AVC Config: 41B
0x01, 0x64, 0x00, 0x20, 0xff, 0xe1, 0x00, 0x19,
0x67, 0x64, 0x00, 0x20, 0xac, 0xd9, 0x40, 0xc0,
0x29, 0xb0, 0x11, 0x00, 0x00, 0x03, 0x00, 0x01,
0x00, 0x00, 0x03, 0x00, 0x32, 0x0f, 0x18, 0x31,
0x96, 0x01, 0x00, 0x05, 0x68, 0xeb, 0xec, 0xb2,
0x2c
stts, 32B, FB(4B), 2 childs (+)
count=717, delta=40
count=1, delta=0
stss, 64B, FB(4B), count=12
1, 126, 170, 228, 370, 413, 466, 496
ctts, 3856B, FB(4B), 480 childs (+)
count=1, offset=0
count=1, offset=80
count=1, offset=200
count=1, offset=80
count=1, offset=0
count=1, offset=40
count=1, offset=200
count=1, offset=80
stsc, 28B, FB(4B), 1 childs (+)
first=1, samples=1, index=1
stsz, 2892B, FB(4B), size=0, 718 childs (+)
5132, 127, 984, 50, 57, 20, 188, 35
stco, 2888B, FB(4B), 718 childs (+)
40, 5259, 5473, 6631, 6855, 7086, 7193, 7555
trak, 14624B, 2 boxes
tkhd, 92B, FB(4B,V0,0x03), track #2, 28618TBN, volume=1.0
mdia, 14524B, 3 boxes
mdhd, 32B, FB(4B), TBN=1000, 28618TBN, LANG=und
hdlr, 45B, FB(4B), soun, SoundHandler
minf, 14439B, 3 boxes
smhd, 16B, FB(4B)
dinf, 36B, 1 boxes
dref, 28B, FB(4B), 1 childs(+)
URL: Same file
stbl, 14379B, 5 boxes
stsd, 91B, FB(4B), 1 childs(+)
mp4a, 75B, refs#0, 2 channels, 16 bits, 44100 Hz, 1 boxes
esds, 39B, FB(4B), tag=0x03, ID=2
decoder config, tag=0x04, type=64, stream=5
decoder specific, tag=0x05, ASC 2B
0x12, 0x10
stts, 4360B, FB(4B), 543 childs (+)
count=3, delta=23
count=1, delta=24
count=3, delta=23
count=1, delta=24
count=4, delta=23
count=1, delta=24
count=3, delta=23
count=1, delta=24
stsc, 28B, FB(4B), 1 childs (+)
first=1, samples=1, index=1
stsz, 4948B, FB(4B), size=0, 1232 childs (+)
87, 87, 87, 87, 87, 87, 87, 87
stco, 4944B, FB(4B), 1232 childs (+)
5172, 5386, 6457, 6544, 6681, 6768, 6912, 6999
- First, ffmpeg has an 8-byte free box reserved as a placeholder.
- Secondly, ffmpeg's moov has 4 boxes (mvhd, trak, trak, udta), while SRS only has 3, missing the udta (custom information) box.
- Furthermore, ffmpeg's tkhd contains width and height information, while SRS does not.
- Additionally, ffmpeg's video track has an edts box, with a tbn of 455680, while SRS has 28640.
- Moreover, ffmpeg's video track with avc1 has refs set to 1, size as 768x320, and has avcC and pasp boxes, while SRS has refs set to 0, no size, and no pasp.
- Lastly, ffmpeg's audio track with mp4a has refs set to 1, while SRS has it set to 0.
From the above differences, try modifying to see which ones are crucial.
TRANS_BY_GPT3
free box
ffmpeg will write a free box in front of mdat because mdat is continuously being written, so its length is only known when the writing is finished. At that point, it is necessary to go back to the header of mdat to update the size, which may be longer, so a free box is reserved.
This should not affect playback, and after adding it, there is no impact on Safari, Firefox, or macFinder.
avc1 box
avc1' is SrsMp4VisualSampleEntry, which holds the codec information. It mainly includes two parts:
width
andheight
are the dimensions of the video, which need to be obtained from the SPS/PPS.refs
is thedata_reference_index
field, which should start from 1.
After modifying the width and height, Firefox is able to play the video, and macOS Finder shows the thumbnail. However, Safari still does not work.
After changing the "refs" to 1, Safari also started working correctly.
tkhd box
tkhd" is the track description header, mainly including:
width
andheight
are the dimensions of the video, which need to be obtained from the SPS/PPS.
Similar to the width and height of avc1, it is not necessary, but it would be better to change them together if possible.
Mininum modification
The minimum modification should be the size information of avc1 and tkhd, as well as the refs of avc1.
After testing, it was found that refs are necessary. Without them, Safari cannot play, while Chrome and Firefox work fine.
After testing, it was found that size is also necessary. Without it, Safari cannot play, while Chrome and Firefox work fine.
From this perspective, it seems that Firefox only requires refs or size. Safari requires both refs and size. VLC and Chrome do not require either.
Modified box diagram:
[root@a09d8d5e3210 html]# ../../srs_mp4_parser srs.mp4
ftyp, 32B, brands:isom,512(isom,iso2,avc1,mp41)
free, 8B, free 0B
mdat, 6376857B, total 6376849 bytes
moov, 177924B, 3 boxes
mvhd, 108B, FB(4B), 210662ms, TBN=1000, nTID=3
trak, 72880B, 2 boxes
tkhd, 92B, FB(4B,V0,0x03), track #1, 210520TBN, size=768x320
mdia, 72780B, 3 boxes
mdhd, 32B, FB(4B), TBN=1000, 210520TBN, LANG=und
hdlr, 45B, FB(4B), vide, VideoHandler
minf, 72695B, 3 boxes
vmhd, 20B, FB(4B,V0,0x01)
dinf, 36B, 1 boxes
dref, 28B, FB(4B), 1 childs(+)
URL: Same file
stbl, 72631B, 7 boxes
stsd, 151B, FB(4B), 1 childs(+)
avc1, 135B, refs#1, size=768x320, 1 boxes
avcC, 49B, AVC Config: 41B
0x01, 0x64, 0x00, 0x20, 0xff, 0xe1, 0x00, 0x19,
0x67, 0x64, 0x00, 0x20, 0xac, 0xd9, 0x40, 0xc0,
0x29, 0xb0, 0x11, 0x00, 0x00, 0x03, 0x00, 0x01,
0x00, 0x00, 0x03, 0x00, 0x32, 0x0f, 0x18, 0x31,
0x96, 0x01, 0x00, 0x05, 0x68, 0xeb, 0xec, 0xb2,
0x2c
stts, 32B, FB(4B), 2 childs (+)
count=5264, delta=40
count=1, delta=0
stss, 456B, FB(4B), count=110
1, 126, 170, 228, 370, 413, 466, 496
ctts, 29800B, FB(4B), 3723 childs (+)
count=1, offset=0
count=1, offset=80
count=1, offset=200
count=1, offset=80
count=1, offset=0
count=1, offset=40
count=1, offset=200
count=1, offset=80
stsc, 28B, FB(4B), 1 childs (+)
first=1, samples=1, index=1
stsz, 21080B, FB(4B), size=0, 5265 childs (+)
5132, 127, 984, 50, 57, 20, 188, 35
stco, 21076B, FB(4B), 5265 childs (+)
48, 5267, 5481, 6639, 6863, 7094, 7201, 7563
trak, 104928B, 2 boxes
tkhd, 92B, FB(4B,V0,0x03), track #2, 210662TBN, volume=1.0, size=0x0
mdia, 104828B, 3 boxes
mdhd, 32B, FB(4B), TBN=1000, 210662TBN, LANG=und
hdlr, 45B, FB(4B), soun, SoundHandler
minf, 104743B, 3 boxes
smhd, 16B, FB(4B)
dinf, 36B, 1 boxes
dref, 28B, FB(4B), 1 childs(+)
URL: Same file
stbl, 104683B, 5 boxes
stsd, 91B, FB(4B), 1 childs(+)
mp4a, 75B, refs#1, 2 channels, 16 bits, 44100 Hz, 1 boxes
esds, 39B, FB(4B), tag=0x03, ID=2
decoder config, tag=0x04, type=64, stream=5
decoder specific, tag=0x05, ASC 2B
0x12, 0x10
stts, 31944B, FB(4B), 3991 childs (+)
count=3, delta=23
count=1, delta=24
count=3, delta=23
count=1, delta=24
count=4, delta=23
count=1, delta=24
count=3, delta=23
count=1, delta=24
stsc, 28B, FB(4B), 1 childs (+)
first=1, samples=1, index=1
stsz, 36308B, FB(4B), size=0, 9072 childs (+)
87, 87, 87, 87, 87, 87, 87, 87
stco, 36304B, FB(4B), 9072 childs (+)
5180, 5394, 6465, 6552, 6689, 6776, 6920, 7007
TRANS_BY_GPT3