Log in

View Full Version : New tool to change SAR in H264 bitstream


Pages : 1 2 3 4 [5]

hello_hello
29th May 2017, 14:01
It has to be in numerator and denominator (x:y) format. ie for 23.976fps it'd be:
-vbsf h264_changesps=fps=24000:1001
or for 25fps fps=25:1 etc

I'm also pretty sure it only changes the frame rate written to the video stream. To also change it at the container level you'd need to remux it again with an MP4 muxer while setting the same frame rate.

If you don't do that, you'll probably find MediaInfo will display two frames rates. Something like this after changing the frame rate from 25fps to 23.976fps:

Frame rate : 25.000 FPS
Original frame rate : 23.976 (24000/1001) FPS

I'm not sure if ffmpeg will change the frame rate with the -r option if it's only copying the video stream. I don't use it much and almost never work with MP4. Someone else will probably know.

sneaker_ger
29th May 2017, 15:15
I'm also pretty sure it only changes the frame rate written to the video stream. To also change it at the container level you'd need to remux it again with an MP4 muxer while setting the same frame rate.
I believe it is supposed to also manipulate the mp4 container but since this ffmpeg version is ancient I'd rather use something else for muxing.

For example:
mkvmerge.exe -o "temp.mkv" --default-duration 0:24000/1001p --fix-bitstream-timing-information 0:1 "input.mp4"
ffmpeg -i "temp.mkv" -map 0 -c copy "output.mp4"

(using latest ffmpeg and latest mkvtoolnix)


If you only want to manipulate H.264 elementary streams you can also try videofan3d's new "h264Modify" tool (https://forum.doom9.org/showthread.php?t=174563). (mux and demux using e.g. mp4box)

Luke M
29th May 2017, 21:36
I believe it is supposed to also manipulate the mp4 container

It doesn't.

but since this ffmpeg version is ancient I'd rather use something else for muxing.

For example:
mkvmerge.exe -o "temp.mkv" --default-duration 0:24000/1001p --fix-bitstream-timing-information 0:1 "input.mp4"
ffmpeg -i "temp.mkv" -map 0 -c copy "output.mp4"

(using latest ffmpeg and latest mkvtoolnix)


That works. Thanks!

Something strange I noticed, when I play the .mkv in VLC, it says the frame rate is 24.000384 (I used a frame rate of 24). When playing the .mp4, it says the frame rate is 24.

hello_hello
30th May 2017, 03:04
For example:
mkvmerge.exe -o "temp.mkv" --default-duration 0:24000/1001p --fix-bitstream-timing-information 0:1 "input.mp4"
ffmpeg -i "temp.mkv" -map 0 -c copy "output.mp4"

I'd either forgotten about, or never knew about, the --fix-bitstream-timing-information option. Now I look, there's even a checkbox in the GUI. Doh!

Cheers.

Something strange I noticed, when I play the .mkv in VLC, it says the frame rate is 24.000384 (I used a frame rate of 24). When playing the .mp4, it says the frame rate is 24.

It's probably a jitter thing and the way the frame rate is interpreted.

The MKV timecodes are in milliseconds but for 24fps each frame would have a 41.6666667ms duration. If you extract the timecodes from a 24fps MKV they should look like this, with a pattern of 41ms and 42ms durations.

0
42
83
125
167
208
250
292
333
375

I don't know where VLC gets the frame rate from, but I tried the same thing myself and VLC reported 24.000384fps for the MKV, whereas MPC-HC says it's 24fps.
sneaker_ger probably understand the intricacies of it all far more than I do, but it wouldn't be anything to worry about.

sneaker_ger
30th May 2017, 08:01
Yes, that should be correct. But if ffmpeg reads/writes from mkv to mp4 there will probably also be jitter. If you want 100% perfect mp4 timestamps you need to remux differently, e.g for an mp4 file with 1 video and 1 audio track:
mkvmerge.exe -o "temp.mkv" --default-duration 0:24000/1001p --fix-bitstream-timing-information 0:1 --no-audio "input.mp4"
mkvextract tracks "temp.mkv" "0:temp.264"
mp4box -add "temp.264"#fps=24 -add "input.mp4"#audio -new "output.mp4"

But as you see it gets complicated quickly. Easiest would probably be to use the mkv.

AVCHDfreak
3rd July 2017, 04:57
Hi all,
is it possible to use this tool to modify the "videoFormat" and the "videoSignalTypePresentFlag" parameters in the VUI of the H264 bitstream? Thanks in advance.

ScarAce1989
29th September 2019, 10:26
-vbsf h264_changesps=sar=0:0
I was wondering if this also is possible for x265?

poisondeathray
29th September 2019, 15:09
-vbsf h264_changesps=sar=0:0
I was wondering if this also is possible for x265?


Probably not with roozhou's tool (old), but ffmpeg has bitstream filters now for HEVC and AVC

https://www.ffmpeg.org/ffmpeg-bitstream-filters.html#hevc_005fmetadata

https://www.ffmpeg.org/ffmpeg-bitstream-filters.html#h264_005fmetadata

nbarzgar
3rd October 2020, 12:13
Hello,

highly appreciated tool! Have used it for quite some time now.

I am looking at demuxed .h624 files in a folder I want to change the AR for.
I use a .bat file with the following lines:
For %%a in (*.h246) do ffmpeg -i "%%a.h264" -vcodec copy -acodec copy -vbsf h264_changesps=sar=178:125 "new\%%~na.h264"
pause
ffmpeg.exe copied to that folder.
But when I start the .bat file, nothing happens. The result displays this only:
E:\path\test>For %%a in (*.h246) do ffmpeg -i "%%a.h264" -vcodec copy -acodec copy -vbsf h264_changesps=sar=178:125 "new\%%~na.h264"
%%a was unexpected at this time.

E:\path\test>pause
I may be missing out on something basic, am not that often into these batch scripts...
Would someone help, please?
Thank you!

poisondeathray
3rd October 2020, 14:44
I am looking at demuxed .h624 files in a folder I want to change the AR for.
I use a .bat file with the following lines:
For %%a in (*.h246) do ffmpeg -i "%%a.h264" -vcodec copy -acodec copy -vbsf h264_changesps=sar=178:125 "new\%%~na.h264"
pause


Typos ? You have .h624, .h246, .h264

(*.h246) is probably supposed to be *.h264

Also, the -i entry shouldn't have the extension -i "%%a"


for %%a in (*.h264) do ffmpeg -i "%%a" -vcodec copy -acodec copy -vbsf h264_changesps=sar=178:125 "new\%%~na.h264"
pause

maldon
15th July 2021, 10:49
Hi, everyone! I fed Handbrake with one H264 video (m2ts, 1920x1080) and this open-source transcoder automatically cropped the black bars:

Top: 130
Bottom: 132

How can I get a new m2ts file with identical black bars (132 top and bottom) without re-encoding? What arg should I use? I suppose I have to use "crop" to crop just 2 pixels. Or do I have to modify SAR, DAR or PAR?

SeeMoreDigital
16th July 2021, 11:19
The 'black bars' are hard encoded and form part of the overall 1920x1080 [1.77:1] image.

If you want to remove them and create an 1920x818 (816 would be preferable [2.35:1]) 'image only' video file, or re-centre the image within the black bars, you will need to re-encode...

maldon
17th July 2021, 17:46
Thanks, I want to create a 1920x1080 video, m2ts, but with a 132 pixel horizontal black bar to the top and bottom. But If I have to re-encode how can I keep same original settings? I've created a new thread for this:

zn
7th September 2022, 05:40
How to change profile flag from main to high in bitstream (without transcoding)

4d4028 (AVC Constrained Main Level 4.0)
or
4d0028 (AVC Main Level 4.0)
to
640028 (AVC High Level 4.0)

update: I have tried to do this with hex editor, and result file is not valid h264, it looks like this is not possible from specification stand point.

thrunk
16th February 2023, 00:21
May I use the bitstream filters to crop my video from 1920x876 to 1920x800? If so what command line should I try? Thanks.:o

SeeMoreDigital
16th February 2023, 09:54
May I use the bitstream filters to crop my video from 1920x876 to 1920x800? If so what command line should I try? Thanks.:oNope... Cropping requires re-encoding!