iVikD iVikD - 1 year ago 34
Linux Question

Output certain file info to text file using files in different folders

I'm using the OS X terminal, which has a command called 'afinfo' which outputs information on an audio file. In this scenario, I want the bitrate info for my songs, so I do the following:

MBP:$ afinfo 01\ Strangers\ To\ Ourselves.aiff | grep "bit rate"
bit rate: 1411200 bits per second

Great, so I now know the bitrate for that one song. Evidently, I can use this to get info on every song in that directory:

MBP:$ afinfo * | grep "bit rate"
bit rate: 271000 bits per second
bit rate: 320000 bits per second
bit rate: 248000 bits per second
bit rate: 320000 bits per second
bit rate: 251000 bits per second

What I want to do is to write a script that will output only the song name and bitrate of songs with bitrate under 320000, to a text file. Now, if I were doing just the files in this folder, I could use
grep "File:\|bit rate"
and out put it to my text file
>> sometext
but I'm unsure of how to write the if statement, and my library is organized as such:

Music Folder
\----- Artist1

So I need help with navigating all subfolders and using afinfo on all of them. I've been reading the Linux+ study guide (LPIC-1), but haven't quite gotten to scripting quite yet.

Any help would be much appreciated!!!

EDIT: adding full output for afinfo on one file:

MBP:$ afinfo 01\ Lampshades\ On\ Fire.mp3
File: 01 Lampshades On Fire.mp3
File type ID: MPG3
Num Tracks: 1
Data format: 2 ch, 44100 Hz, '.mp3' (0x00000000) 0 bits/channel, 0 bytes/packet, 1152 frames/packet, 0 bytes/frame
no channel layout.
estimated duration: 187.768150 sec
audio bytes: 7510726
audio packets: 7188
bit rate: 320000 bits per second
packet size upper bound: 1052
maximum packet size: 1045
audio data file offset: 191565

So you see, I'm only interested in the File: and bit rate: strings for this scenario. I was thinking of using grep to get the bitrate into a variable and then somehow using that in my IF statement, but I have a feeling that's overly complicated for what I'm trying to do...

Answer Source

You can use find:

find . -type f -exec afinfo {} +

This will find files recursive and call afinfo with as much file names as possible, output is going to stdout.

You can now pipe to grep, awk, ....

find . -type f -exec afinfo {} + | awk '/File:/ {$1="";f=$0}/bit rate:/ && $3 < 320000{print f}'

The above will print filenames for files with bit rate < 320000