I've googled an entire day experimenting all possible solutions, but noone had worked. I have 2 HIK dvr (one is prehistoric vith analogic video). I've made a bash script to retrieve X seconds (as input) of the DVR stored videos (it cut seconds if video ends early); also record X seconds of Live view (used for saving Motion Alert in Home assistant).
All works well, BUT i have some issues, and the big one is that when the video has no audio, the download hangs at 9.96 seconds for the old device, and 9.97sec for the new (damn!).
Now let's look at the commands and the logs.
Command : this works with cams with audio, but hangs if cam has an audio silent track?
makejob=$(ffmpeg -fflags +genpts -y -ac 1 -channel_layout mono -rtsp_transport tcp -hide_banner -loglevel info -err_detect ignore_err -i "rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/${camnumber}01?starttime=$starttime&endtime=$endTime" -map 0:v -map 0:a? -ar 44100 -filter:a "volume=$volume" -t $seconds -acodec aac -vcodec copy $filepathname)
(A) result Cam with audio : errors too, but works recording both video and audio
Downloading from DVR BeB-DVR a 7 seconds video of the CAM CAMERA-1 recorded from 2023-08-19 10:10:10
Input #0, rtsp, from 'rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/101?starttime=20230819T101010Z&endtime=20230819T101020Z':
Metadata:
title : HIK Media Server V4.32.116
comment : HIK Media Server Session Description : standard
Duration: 462428:22:56.-75, start: 0.000000, bitrate: N/A
Stream #0:0: Video: h264 (Baseline), yuvj420p(pc, bt709, progressive), 1920x1080, 25 fps, 25 tbr, 90k tbn, 180k tbc
Stream #0:1: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s
Output #0, mp4, to '/home/pi/hikdwn/downloaded/BeB-DVR-CAMERA-1-2023-08-19-h01010-(10s).mp4':
Metadata:
title : HIK Media Server V4.32.116
comment : HIK Media Server Session Description : standard
encoder : Lavf57.56.101
Stream #0:0: Video: h264 (Baseline) ([33][0][0][0] / 0x0021), yuvj420p(pc, bt709, progressive), 1920x1080, q=2-31, 25 fps, 25 tbr, 90k tbn, 90k tbc
Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, mono, fltp, 69 kb/s
Metadata:
encoder : Lavc57.64.101 aac
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (pcm_mulaw (native) -> aac (native))
Press [q] to stop, [?] for help
[mp4 @ 0x212a9d0] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
frame= 175 fps= 33 q=-1.0 Lsize= 1708kB time=00:00:07.00 bitrate=1998.8kbits/s speed=1.34x
video:1643kB audio:60kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.350157%
[aac @ 0x212cf00] Qavg: 111.884
Download completed!
(B) result Cam withouth audio (on both DVRs) : hangs before completing the download, the file saved is 0kb
(B) on DVR1 (newer one) hangs
Input #0, rtsp, from 'rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/501?starttime=20230819T101010Z&endtime=20230819T101020Z':
Metadata:
title : HIK Media Server V4.32.116
comment : HIK Media Server Session Description : standard
Duration: 462428:23:09.-90, start: 0.020000, bitrate: N/A
Stream #0:0: Video: hevc (Main), yuv420p(tv), 2560x1440, 50 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s
Output #0, mp4, to '/home/pi/hikdwn/downloaded/BeB-DVR-CAMERA-5-2023-08-19-h01010-(10s).mp4':
Metadata:
title : HIK Media Server V4.32.116
comment : HIK Media Server Session Description : standard
encoder : Lavf57.56.101
Stream #0:0: Video: hevc (Main) ([35][0][0][0] / 0x0023), yuv420p(tv), 2560x1440, q=2-31, 50 fps, 25 tbr, 90k tbn, 90k tbc
Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, mono, fltp, 69 kb/s
Metadata:
encoder : Lavc57.64.101 aac
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (pcm_mulaw (native) -> aac (native))
Press [q] to stop, [?] for help
frame= 251 fps=149 q=-1.0 size= 0kB time=00:00:09.97 bitrate= 0.0kbits/s speed=5.94x
(B) on DVR2 (older) hangs with errors also
Input #0, rtsp, from 'rtsp://$user:$dvrpasswd@$dvraddress:554/ISAPI/streaming/tracks/501?starttime=20230819T101010Z&endtime=20230819T101020Z':
Metadata:
title : HIK Media Server V3.1.4
comment : HIK Media Server Session Description : standard
Duration: 97123:37:11.-81, start: 0.240000, bitrate: N/A
Stream #0:0: Video: h264 (Baseline), yuv420p(progressive), 352x288, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1: Audio: pcm_mulaw, 8000 Hz, mono, s16, 64 kb/s
Output #0, mp4, to '/home/pi/hikdwn/downloaded/HomeDVR-VIALE-2023-08-19-h01010-(10s).mp4':
Metadata:
title : HIK Media Server V3.1.4
comment : HIK Media Server Session Description : standard
encoder : Lavf57.56.101
Stream #0:0: Video: h264 (Baseline) ([33][0][0][0] / 0x0021), yuv420p(progressive), 352x288, q=2-31, 25 fps, 25 tbr, 90k tbn, 90k tbc
Stream #0:1: Audio: aac (LC) ([64][0][0][0] / 0x0040), 44100 Hz, mono, fltp, 69 kb/s
Metadata:
encoder : Lavc57.64.101 aac
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #0:1 -> #0:1 (pcm_mulaw (native) -> aac (native))
Press [q] to stop, [?] for help
[mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 2, current: -17998; changing to 3. This may result in incorrect timestamps in the output file.
[mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 3, current: -14398; changing to 4. This may result in incorrect timestamps in the output file.
[mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 4, current: -10798; changing to 5. This may result in incorrect timestamps in the output file.
[mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 5, current: -7198; changing to 6. This may result in incorrect timestamps in the output file.
[mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 6, current: -3600; changing to 7. This may result in incorrect timestamps in the output file.
[mp4 @ 0x1377ce0] Non-monotonous DTS in output stream 0:0; previous: 7, current: 2; changing to 8. This may result in incorrect timestamps in the output file.
frame= 256 fps= 49 q=-1.0 size= 0kB time=00:00:09.96 bitrate= 0.0kbits/s speed=1.89x
I suppose it's due to the missing timestamps. Note that also when there is no audio, there is an audio track in the video. It hangs when trying to encode that void track.
(i will comment with logs where no audio is recorded, it works with some errors... maybe other useful infos are there)