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

144 lines
3.5 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
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=[]
y=0
#originalfiles = ['E:\\Shadowplay\\Returnal\\ref.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 stream=duration: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)
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 = i
pbar.refresh()
y=0
for each in size_in:
size_list.append(float(each)/1000)
#time_list = time_list + time_in
#size_list = size_list + size_in
num_time = len(time_list)
num_size = len(size_list)
#duration = time_list[num_time-1]
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
while x < duration+1:
outp = sum_seconds(time_list[a:b], x)
x+=1
seconds_list.append(outp)
#print(outp)
if x < duration+1:
summed_list.append(sum(size_list[a:outp]))
#print("summed list: ", summed_list)
a=outp
#print(summed_list)
length=len(summed_list)
newl=range(length)
print(length)
print(sum(summed_list))
print(sum(size_list))
fig, ax = plt.subplots()
ax.plot(newl, summed_list)
ax.set(xlabel='time', ylabel='KBps', label="test")
plt.fill_between(newl, summed_list)
plt.ticklabel_format(style='plain')
plt.title(listed[0])
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()
plt.show()
#k=input("press close to exit")