Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ScribbleGhost/eb28c3833868b05aa50331404c98a469 to your computer and use it in GitHub Desktop.
Save ScribbleGhost/eb28c3833868b05aa50331404c98a469 to your computer and use it in GitHub Desktop.
Scanning through a folder recursively to detect the overall bit rate of video files with a certain extension.
import ffmpeg # pip install ffmpeg-python (not to be cinfused with python)
import psutil # pip install psutil (might require wheels, which might be incompatible with versions above Python 3.9)
import os
import glob
import csv
# The path in which you would like to look for video files
directory = 'M:\VIDEO\MOVIES'
# The file types you want to check
exts = ['*.mp4', '*.mkv']
# Make a list of files by recursve search
input_files = [f for ext in exts for f in glob.glob(os.path.join(directory, '**', ext), recursive=True)]
# Make sure FFmpeg is not running
if not ("ffmpeg.exe" in (i.name() for i in psutil.process_iter())) == True:
# For each file...
for each in input_files:
# Use ffprobe (part of FFmpeg) to make a dict of metadata for the file
probe = ffmpeg.probe(each)
# Limit the dict of metadata to the video stream specifically
video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
# Find the bitrate value in the dict
bitrate = int(probe['format']['bit_rate'])
# If the bit rate is larger than 8 Mb/s...
if bitrate > 10000000:
print(each)
print('The overall bit rate ', '(', round(int(bitrate)/1000000, 2), ' Mb/s)', ' is higher than 10 MB/s.', '\n', sep='')
data = [each, bitrate]
header = ['File', 'Bit rate in bytes']
with open('Files exceeding the bit rate limit.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow(data)
else:
print(each)
print('The overall bit rate is lower than 10 MB/s.', '\n', sep='')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment