简体   繁体   中英

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
       |
       \-------Album1
               |
               \-------song1
               \-------song2
               ...

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
optimized
----

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...

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.

If your directory contains files that are not audio files, eg. .DS_Store , etc. you could filter known file extensions:

find . -type f \( -name '*.mp3' -o -name '*.flac' \) -exec afinfo {} +

But all file extensions might not be known in which case redirecting stderr ( afinfo will output Fail: AudioFileOpenURL failed when the specified file is not an audio file) to /dev/null will suppress said error:

find . -type f -exec afinfo {} + 2> /dev/null | ...    

You can now pipe to awk to get files with the expected bit rate:

... | awk '/File:/ {$1="";f=$0}/bit rate:/ && $3 < 320000{print f}'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM