Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Average bitrate #10867

Open
jimmy-1000 opened this issue Nov 14, 2022 · 14 comments
Open

Average bitrate #10867

jimmy-1000 opened this issue Nov 14, 2022 · 14 comments

Comments

@jimmy-1000
Copy link

Please add a feature to obtain the average bitrate of the videos like other players and applications do. For example with MPC-HC, Avidemux and Freemake Video Converter all the three give me exactly the same value of the video track bitrate, it never changes.

@butterw
Copy link

butterw commented Jun 2, 2023

+1.

please add an average bitrate property.
When playing a media file: average bitrate = filesize / duration.

@jimmy-1000
Copy link
Author

jimmy-1000 commented Jun 2, 2023

Please some help here (input.conf)

F5 no-osd cycle-values osd-msg1 "" "${file-size}/${duration}"

@butterw
Copy link

butterw commented Aug 3, 2023

Unless an avg-bitrate property is added to mpv, you would need an external script to perform the simple math operation:
avg-bitrate_kbps = 0.008 * file-size_bytes / duration_s

@butterw
Copy link

butterw commented Aug 4, 2023

The (keyframe based) video-bitrate property isn't always very useful for two reasons:

  • it is (undefined) at the start of the video and after seeks (calculation requires two keyframes).
  • it varies an awful lot on some medium/low bitrate video.

Video file average_bitrate calculation using filesize_Bytes/duration_s
avg-bitrate (in kilobits per second, kb/s) = 8/1000.0 * file-size_bytes / duration_s
for local video/audio files seems to match mediainfo > Overall bit rate.
Customization: this script can operate every time a new file is loaded or (by default) on demand via a keybinding.

mpv script: avg-bitrate.js v0.1 by butterw
https://github.com/butterw/bShaders/blob/c33139cf0e7bb3b01e2a2b5df02beff06b0af4fa/mpv/settings/scripts/avg-bitrate.js
! images have duration: 0
! video streamed with yt-dlp have filesize: NaN
use --osd-msg1=" ${video-bitrate}" instead.

@jimmy-1000
Copy link
Author

It works good, thanks a lot !! 👌

@butterw
Copy link

butterw commented Aug 6, 2023

The correct link (v0.11) is: https://github.com/butterw/bShaders/blob/master/mpv/settings/scripts/avg-bitrate.js
The script shows that the calculation works and provides a useful result for local files. So I think it would be useful to add an avg-bitrate property to mpv.

Script calculation vs built-in property ?
I'm not sure about the script limitations in mpv, but I haven't seen anything suggesting a script can provide a return value usable directly in input.conf.

If the avg-bitrate was written to any existing unused String property (let's say: osd-msg2) when the video is loaded, you could use this property in input.conf removing the need to customize the script for a specific usage.

F5 no-osd cycle-values osd-msg1 osd-msg2 ""

While this hack works, it would be quite confusing when reading input.conf.

@jimmy-1000
Copy link
Author

....

I agree, this "average bitrate" of video should be part of the mpv core. I have a question about osd-msg:

F1 no-osd cycle-values osd-msg1 "" "File: ${filename} \n Size: ${file-size} \n script-message avg-bitrate"

This doesn't work. Is there a way to call your script from this line with osd-msg using input.conf?

@butterw
Copy link

butterw commented Aug 6, 2023

Instead of using osd-msg2 as suggested in my previous post, you can use a user-data property. I'm testing on Windows, with mpv 0.35.0-542-gcab54488 (Jul 16 2023). I think you need a fairly recent version of mpv for this feature.

You just need to modify the script so that the the property "user-data/avg-bitrate" is set each time a new file is loaded.
mp.set_property("user-data/avg-bitrate", bitrate_kbps +" kb/s");

input.conf:
F1 no-osd cycle-values osd-msg1 "" "File: ${filename} \n Size: ${file-size} \n ${user-data/avg-bitrate}"

EDIT: however this doesn't work exactly the way you want, because you need to cycle F1 for the OSD to update (this is also the case for the properties filename, file-size, etc). You don't have this problem with a permanent display
--osd-msg1="File: ${filename} \n Size: ${file-size} \n ${user-data/avg-bitrate}".

@jimmy-1000
Copy link
Author

Sadly I get "(error)". I use mpv 0.36

@butterw
Copy link

butterw commented Aug 7, 2023

(error) means user-data/avg-bitrate has not been set.

run mpv from terminal for testing.
Ensure you only have one version of the script in the correct scripts directory (ie move out any old versions to avoid conflicts).

Could you maybe clarify what your end goal is: is it having a simplified stats display that you can toggle on/off ?

@butterw
Copy link

butterw commented Aug 7, 2023

Unrelated to the avg-bitrate property, the way you were trying to set your keybinding doesn't work (because the osd doesn't auto-update when a new file is loaded). IMO this is how you can do it:

https://forum.doom9.org/showthread.php?p=1990436#post1990436
how to toggle a custom osd with a keybinding (the osd is permanently visible until disabled, it updates when a new file is loaded):
--osd-msg2="${filename}\n${file-size}"
input.conf: O no-osd cycle-values osd-level 2 1

@jimmy-1000
Copy link
Author

jimmy-1000 commented Aug 7, 2023

  • It doesn't say anything in the terminal.
  • I only have one instance of avg-bitrate.js running.
  • "simplified stats display that you can toggle on/off". It's correct. My F1 line is longer. But for testing I'm using a short line with 3 elements: F1 no-osd cycle-values osd-msg1 "" "File: ${filename} \n Size: ${file-size} \n ${user-data/avg-bitrate}"

Maybe I did an incorrect modification, can you verified? (I added txt extension to upload here)

avg-bitrate.js.txt

@butterw
Copy link

butterw commented Aug 7, 2023

v0.20: https://github.com/butterw/bShaders/blob/master/mpv/settings/scripts/avg-bitrate.js
this version of avg-bitrate.js uses the new user-data feature (requires mpv v0.36).

Basic use:
display avg-bitrate on osd for 2 seconds when you press key a:
input.conf: a no-osd show-text "${user-data/avg-bitrate}" 2000

To toggle a custom osd with a keybinding (the osd is permanently visible until disabled, it updates when a new file is loaded):
mpv.conf or mpv cli: --osd-msg2="${filename}\n${file-size}\n${user-data/avg-bitrate}"
input.conf: O no-osd cycle-values osd-level 2 1

@jimmy-1000
Copy link
Author

v0.20: https://github.com/butterw/bShaders/blob/master/mpv/settings/scripts/avg-bitrate.js this version of avg-bitrate.js uses the new user-data feature (requires mpv v0.36).
...

It works!! 👏 Thanks a lot for your help 👌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants