Skip to content

Instantly share code, notes, and snippets.

@hldr4
Created June 30, 2022 21:14
Show Gist options
  • Save hldr4/112066a78c103f8610703d44335b9d51 to your computer and use it in GitHub Desktop.
Save hldr4/112066a78c103f8610703d44335b9d51 to your computer and use it in GitHub Desktop.
Calculates the SAR required to achieve the desired new DAR
import sys
from fractions import Fraction
"""
Calculates the SAR required to achieve the desired new DAR
SAR = Desired DAR / Current DAR
eg. (4/3)/(391/284) = 1136/1173
usage: sar.py [current_dar] [desired_dar]
(input as fractions)
"""
current = sys.argv[1].split('/')
wanted = sys.argv[2].split('/')
fs = list(map(Fraction, wanted+current))
sar = Fraction((fs[0]/fs[1])/(fs[2]/fs[3]))
print(f'\nRequired SAR: {sar}')
print(f'\nExample command: ffmpeg -i old.h264 -c copy -bsf:v "h264_metadata=sample_aspect_ratio={sar}" new.h264')
print(f'\n + Tip: to extract the bistream, use something like ffmpeg -i video.mkv -c:v copy -bsf:v h264_mp4toannexb old.h264')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment