Created
September 28, 2023 01:20
-
-
Save chapmanjacobd/7333baa69f0d271560ad11b4d8ab43f1 to your computer and use it in GitHub Desktop.
how to correctly scan a video file to check for corruption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def decode_full_scan(path): | |
ffprobe_cmd = [ | |
'ffprobe', '-show_entries', 'stream=r_frame_rate,nb_read_frames,duration', | |
'-select_streams', 'v', '-count_frames', | |
'-of', 'json', | |
'-threads', '20', | |
'-v', '0', | |
path, | |
] | |
ffprobe = subprocess.Popen(ffprobe_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
output, err = ffprobe.communicate() | |
data = json.loads(output)['streams'][0] | |
r_frame_rate = fractions.Fraction(data['r_frame_rate']) | |
nb_frames = int(data['nb_read_frames']) | |
metadata_duration = float(data['duration']) | |
actual_duration = nb_frames * r_frame_rate.denominator / r_frame_rate.numerator | |
difference = abs(actual_duration - metadata_duration) | |
average_duration = (actual_duration + metadata_duration) / 2 | |
percent_diff = (difference / average_duration) * 100 | |
if difference > 0.1: | |
log.warning(f'[{path}]: Actual duration ({actual_duration:.2f}) does not match metadata duration ({metadata_duration:.2f}) by {difference:.2f}s') | |
return percent_diff |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment