Correcting wrong aspect ratio with passthrough recording

Discussions related to the 1.36.x series of ZoneMinder
Post Reply
jerrypaulick
Posts: 6
Joined: Fri May 11, 2018 5:56 pm

Correcting wrong aspect ratio with passthrough recording

Post by jerrypaulick »

Hi,

I've got one camera that's 4MP and 16/9, which would normally mean a 2560x1440 video. However, the raw video stream out of the camera introduces itself as [SAR 4:3 DAR 64:27], as per the ffprobe output:

Code: Select all

Stream #0:0: Video: h264 (High), yuvj420p(pc, progressive), 2560x1440 [SAR 4:3 DAR 64:27], 10 fps, 10 tbr, 90k tbn
So the resolution is actually correct, but it has weird SAR (Source Aspect Ratio), which should be 16:9 (not 4:3) and even weirder DAR (Display Aspect Ratio), which also should be 16:9 (and not 64:27). At least if I understood correctly what those are suppose to mean.

The decoded (live) view in ZoneMinder is okay, since I have set the correct AR (Capture Resolution [pixels] to 2560x1440) in the camera's settings, but the mp4 files from passthrough recording on the disk are embedded with the bad SAR/DAR (naturally), which means the video is also distorted on playback within ZM and within any video player if opened directly from the disk as well.

Now, the *.mp4 file can be fixed without re-encoding simply by altering the DAR with ffmpeg like this:

Code: Select all

ffmpeg -i in.mp4 -aspect 16:9 -c copy out.mp4
Which changes the original file from:

Code: Select all

Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, progressive), 2560x1440 [SAR 4:3 DAR 64:27], 2890 kb/s, 9.96 fps, 10 tbr, 90k tbn (default)
to

Code: Select all

Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc, progressive), 2560x1440 [SAR 4:3 DAR 64:27], 2916 kb/s, SAR 1:1 DAR 16:9, 10 fps, 10 tbr, 90k tbn (default)
After this, the video plays in 16/9 and finally looks normal.

Now my curious question is – is there somewhere within Zoneminder a way to do this while saving/after saving automatically?

Idea #2: Could Filters and Execute command on all matches be used for this somehow?


Thank you :)


So far I tried the OPTIONS_FFMPEG but while it didn't broke the recording, it is still saving the videos as before (i.e. with unaltered AR). edit: ok, according to this post this setting is for exporting jpgs to video, so probably not useful here?
So far I tried the OPTIONS_FFMPEG but while it didn't broke the recording, it is still saving the videos as before (i.e. with unaltered AR).
So far I tried the OPTIONS_FFMPEG but while it didn't broke the recording, it is still saving the videos as before (i.e. with unaltered AR).
Screen Shot 2022-03-13 at 18.31.31.jpg (57.45 KiB) Viewed 3419 times
dougmccrary
Posts: 1373
Joined: Sat Aug 31, 2019 7:35 am
Location: San Diego

Re: Correcting wrong aspect ratio with passthrough recording

Post by dougmccrary »

Have you considered Storage -> Optional Encoder Parameters (?)
Magic919
Posts: 1381
Joined: Wed Sep 18, 2013 6:56 am

Re: Correcting wrong aspect ratio with passthrough recording

Post by Magic919 »

The dirty world of PAR/SAR/DAR. What a mess.

What happens with 'preserve aspect ratio' ticked? I don't know, but might be worth testing.

Otherwise, yes, I expect you can trigger a script to 'fix' the files.
-
jerrypaulick
Posts: 6
Joined: Fri May 11, 2018 5:56 pm

Re: Correcting wrong aspect ratio with passthrough recording

Post by jerrypaulick »

dougmccrary wrote: Wed Mar 16, 2022 4:47 am Have you considered Storage -> Optional Encoder Parameters (?)
I tried it and it didn't do anything. I don't think these settings are used for passthrough saving, but were used before when the video files were assembled from the individual jpgs.




For the Filters solution, I am wondering how to avoid running the script multiple times on a single Event, or in other words, how to make sure that once the filter ran once on a given Event, it will be excluded from the next run?
Magic919
Posts: 1381
Joined: Wed Sep 18, 2013 6:56 am

Re: Correcting wrong aspect ratio with passthrough recording

Post by Magic919 »

Pretty sure that's by design.
-
User avatar
iconnor
Posts: 3357
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: Correcting wrong aspect ratio with passthrough recording

Post by iconnor »

There's nothing built into ZM to handle this. Maybe the encoderparams will work. It just passes options to the codec, and avformat, so you might get some joy there.

The filters option will work, and the event will be marked that it has been processed.
jerrypaulick
Posts: 6
Joined: Fri May 11, 2018 5:56 pm

Re: Correcting wrong aspect ratio with passthrough recording

Post by jerrypaulick »

I played with the encoderparams yesterday, tried adding the -aspect 16:9 in multiple ways, but it didn't seem to have an effect on the final file.

I will probably write a bash script that will fix the file once the event is complete and run it through the Filters.

It's probably good to say that even the ffmpeg's -aspect 16:9 doesn't actually alter the existing DAR (which is what I said in the first post), but rather adds another parameter somewhere (probably on the mp4 container level, not directly to the video stream inside it). Some players honor it, some (supposedly) don't. For what I do with the video it works, so I didn't dig much deeper.

If anyone ever goes through this topic, here are some resources I found on altering the aspect ratio without re-encoding the file:
- correcting the AR with ffmpeg without re-encoding - this is what I used,
- another way using ffmpeg – however this should alter AR on the stream level, but didn't work for me (maybe older ffmpeg version, haven't
solution using mp4box – should also alter the AR on the stream level, it did something, however the video ended up being even narrow noodle than before; and even then I'd rather solve this with ffmpeg, without having to install another tool

Thanks all so far for the ideas. When I have some time, I'll play with the bash script and Filters.
Magic919
Posts: 1381
Joined: Wed Sep 18, 2013 6:56 am

Re: Correcting wrong aspect ratio with passthrough recording

Post by Magic919 »

It does read as working at container level and in conjunction with a copy - strictly not encoding the video. This is probably why it fails when you add it to ZM.

There are commands that will manipulate at H.264 level, but you have something that works. I'd also stop there.
-
User avatar
iconnor
Posts: 3357
Joined: Fri Oct 29, 2010 1:43 am
Location: Toronto
Contact:

Re: Correcting wrong aspect ratio with passthrough recording

Post by iconnor »

Please note that when adding options to encoder params, it isn't adding a command line option, so there shouldn't be any -'s. It is name=value
Post Reply