Files
ffmpeg_collection/bitrate_graph_progress.py
2026-02-26 15:52:01 +01:00

145 lines
3.8 KiB
Python

import subprocess
import sys
from pathlib import Path
import os
import re
import numpy as np
from matplotlib import pyplot as plt
from tqdm import tqdm
import math
output_folder_name ="/bulk_convert_out/"
droppedFile = sys.argv[1]
droppedName = Path(droppedFile).name
originalfiles = sys.orig_argv[2:]
size_list=[]
time_list=[]
seconds_list=[]
summed_list=[]
duration=[]
#originalfiles = ['E:\Projects\VapourSYnth\VapourSynth64Portable\convert\\Lodoss-tou_Senki_-_Record_of_Lodoss_War_-_04_[BD.1080p]_[Iznjie_Biznjie]_[B4481C1E].mkv']
#long path shenanigans
joined = (' '.join(originalfiles))
re.sub (' +', ' ', joined)
listed = re.split(r'(.+?mkv|.+?mov|.+?mp4)', joined)
listed = list(filter(None, listed))
listed = [i.lstrip() for i in listed]
#print(droppedName)
#get path of script location and remove the tail
head_tail = os.path.split(sys.argv[0])
output_path = head_tail[0]
#skip first part of sys.argv since it points to script location
arguments = sys.argv[1:]
#check if output folder exists, otherwise create it
isExist = os.path.exists(output_path+output_folder_name)
if not isExist:
os.makedirs(output_path+output_folder_name)
ffprobe_args = '.\\ffprobe.exe -v error -select_streams v:0 -i "{file}" -show_entries format=duration'.format(file=listed[0])
print(ffprobe_args)
ffprobe = subprocess.Popen(["powershell.exe", ffprobe_args], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='UTF-8')
for line in iter(ffprobe.stdout.readline,""):
if len(duration) < 1:
duration = re.findall('(?<=duration=).*',line)
duration = float(duration[0])
print(duration)
ffprobe_args = '.\\ffprobe.exe -v error -select_streams v:0 -i "{file}" -show_entries frame=pkt_size,pkt_pts_time'.format(file=listed[0])
print(ffprobe_args)
ffprobe = subprocess.Popen(["powershell.exe", ffprobe_args], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='UTF-8')
pbar= tqdm(desc='progress', total = duration, bar_format="{l_bar}{bar:60}{r_bar}{bar:-10b}")
y=0
for line in iter(ffprobe.stdout.readline,""):
time_in = re.findall('(?<=pkt_pts_time=).*',line)
size_in = re.findall('(?<=pkt_size=).*',line)
for each in time_in:
time_list.append(float(each))
i = time_list[-1]
y += 1
if y == 100:
pbar.n = math.floor(i)
pbar.refresh()
y=0
for each in size_in:
size_list.append(float(each)/1024*8)
duration=time_list[-1]
num_time = len(time_list)
num_size = len(size_list)
print("duration: ", duration)
print("time length: ", num_time)
print("size length:" , num_size)
def sum_seconds(list, x):
for each in list:
if each < x:
pass
else:
highest_index = time_list.index(each)
return(int(highest_index))
a=0
b=len(time_list)
x=1
z=0
while x <= math.floor(duration):
outp = sum_seconds(time_list[a:b], x)
x+=1
seconds_list.append(outp)
#print(outp)
if x <= math.floor(duration):
summed_list.append(sum(size_list[a:outp]))
z+=1
#print("summed list: ", summed_list)
a=outp
#print(summed_list)
#print(size_list[-1:-10])
#print(time_list[-1:-10])
length=len(summed_list)
newl=range(length)
print(length)
print("size summed: " ,sum(summed_list))
print("size_ input: ",sum(size_list))
filename = Path(listed[0])
fig, ax = plt.subplots()
ax.plot(newl, summed_list)
ax.set(xlabel='time', ylabel='Kbits (Kilobits per sec)', label="test")
plt.fill_between(newl, summed_list)
plt.ticklabel_format(style='plain')
plt.title(filename.name)
plt.axhline(y=np.nanmean(summed_list), color='red', linestyle='--', label='Avg: '+str(round(np.nanmean(summed_list)))+'KBps')
plt.grid()
plt.margins(0)
plt.legend()
fig.set_size_inches(10,6)
plt.locator_params(axis='y', nbins=10)
plt.show()
#k=input("press close to exit")