ExifTool Forum

General => Metadata => Topic started by: stryk on November 20, 2019, 12:57:42 PM

Title: [audio] Adding a tag for FLAC Bitrate
Post by: stryk on November 20, 2019, 12:57:42 PM
Hello, ExifTool is incredibly useful and robust. However, unless I missed it, I didn't see Tag or function to display the BitRate of FLAC audio files, at least not in the version I have (11.70). This can be calculated easily enough from the command line on virtually any *nix terminal, but I wanted to avoid calling another process in a script. I wanted ExifTool to include it in a CSV/TSV file I have it generating, so I took a shot at a user-defined 'Composite' tag (these are derived from the values of other Tags).

My Perl skills are amateur at best, but defining the Composite is just hashes and keys, so its not THAT complicated. However, any tips for improvement would be welcome. This seems to be working reliably in my testing thus far:

%Image::ExifTool::UserDefined = (

'Image::ExifTool::Composite' => {
#----------START copy the following into your .ExifTool_config if you have other Composites already defined----------#
FLACBitrate => {
Require => {
0 => 'FileSize',
1 => 'SampleRate',
2 => 'TotalSamples',
},
ValueConv => '($val[0] * 8) / ($val[2] / $val[1])',
PrintConv => 'int($val / 1000)',
},
#-----------END copy the preceding into your .ExifTool_config if you have other Composites already defined-----------#
},

#-----------end config-------------#
);
1;
#---------keep at bottom----------#

USAGE:
Hope this is useful to some folks, cheers!


Question for folks more adept at Perl than myself:
I was unsure if it would be more efficient, or even matter, to have the already-defined "Duration" tag be one of the "Required". As I understand it, and correct me if I'm mistaken please, the "Duration" is simply another Composite tag that is pre-defined by default in ExifTool. I opted to manually do the calculation, and it seems to be working, I don't know if the difference between the two would be negligible or not.
Title: Re: [audio] Adding a tag for FLAC Bitrate
Post by: StarGeek on November 20, 2019, 01:20:55 PM
Quote from: stryk on November 20, 2019, 12:57:42 PM
I was unsure if it would be more efficient, or even matter, to have the already-defined "Duration" tag be one of the "Required". As I understand it, and correct me if I'm mistaken please, the "Duration" is simply another Composite tag that is pre-defined by default in ExifTool. I opted to manually do the calculation, and it seems to be working, I don't know if the difference between the two would be negligible or not.

I doubt it would make a difference, the section on flac duration is basically a copy of what you did
    Duration => {
        Require => {
            0 => 'FLAC:SampleRate',
            1 => 'FLAC:TotalSamples',
        },
        ValueConv => '($val[0] and $val[1]) ? $val[1] / $val[0] : undef',
        PrintConv => 'ConvertDuration($val)',
    },


Any further commentary from Phil will have to wait until he returns some time next week.
Title: Re: [audio] Adding a tag for FLAC Bitrate
Post by: stryk on November 22, 2019, 05:15:18 PM
UPDATED

After some more testing and reading through the relevant (https://perldoc.perl.org/5.30.0/perlfunc.html#int-EXPR) perldoc (https://perldoc.perl.org/5.30.0/perlfunc.html#sprintf-FORMAT%2c-LIST) sections, I've updated this a little bit.

PrintConv was changed to use sprintf() instead of int(). It seems that int() just, basically, truncates the given floating point number at the decimal point (which may lead to rounding errors in certain cases), while sprintf() properly formats it.


%Image::ExifTool::UserDefined = (


'Image::ExifTool::Composite' => {
#----------START copy the following into your .ExifTool_config if you have other Composites already defined----------#
FLACBitrate => {
Require => {
0 => 'FileSize',
1 => 'SampleRate',
2 => 'TotalSamples',
},
ValueConv => '($val[0] * 8) / ($val[2] / $val[1])',
PrintConv => 'sprintf("%.0f", ($val / 1000))',
},
#-----------END copy the preceding into your .ExifTool_config if you have other Composites already defined------------#
},


#-----------end config-------------#
);
1;
#---------keep at bottom----------#



Anything else that can be improved? Suggestions welcome. Thanks!
Title: Re: [audio] Adding a tag for FLAC Bitrate
Post by: Phil Harvey on November 25, 2019, 12:31:51 PM
Looks good.