[audio] Adding a tag for FLAC Bitrate

Started by stryk, November 20, 2019, 12:57:42 PM

Previous topic - Next topic

stryk

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:

  • Copy/paste into your ~/.ExifTool_config [create the file if it doesn't exist]
  • If inserting into a config you already have: WATCH YOUR PLACEMENT & SYNTAX! Perl wants her ',' and ';' placed correctly or she has a fit!
  • exiftool -composite:FLACBitrate <file>  for the Print-Formatted output
  • exiftool -composite:FLACBitrate# <file>  for the raw value output
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.

StarGeek

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.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

stryk

UPDATED

After some more testing and reading through the relevant perldoc 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!

Phil Harvey

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).