简体   繁体   English

使用不同文件夹中的文件将某些文件信息输出到文本文件

[英]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. 我正在使用OS X终端,该终端具有一个名为“ afinfo”的命令,该命令可在音频文件上输出信息。 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. 我要做的是编写一个脚本,该脚本仅将歌曲名称和比特率低于320000的歌曲的比特率输出到文本文件。 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: 现在,如果只执行该文件夹中的文件,则可以使用grep "File:\\|bit rate"并将其放到我的文本文件>> sometext但是我不确定如何编写if语句,并且我的图书馆是这样组织的:

Music Folder
|
\----- Artist1
       |
       \-------Album1
               |
               \-------song1
               \-------song2
               ...

So I need help with navigating all subfolders and using afinfo on all of them. 因此,在导航所有子文件夹并在所有子文件夹上使用afinfo时,我需要帮助。 I've been reading the Linux+ study guide (LPIC-1), but haven't quite gotten to scripting quite yet. 我一直在阅读Linux +学习指南(LPIC-1),但还没有完全了解脚本。

Any help would be much appreciated!!! 任何帮助将非常感激!!!

EDIT: adding full output for afinfo on one file: 编辑:在一个文件上为afinfo添加完整输出:

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. 如此看来,我只对这种情况下的File:和bit rate:字符串感兴趣。 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... 我当时在考虑使用grep将比特率转换为变量,然后以某种方式在IF语句中使用它,但是我的感觉对于我想做的事情来说过于复杂了……

You can use find: 您可以使用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. 这将查找文件递归并使用尽可能多的文件名调用afinfo ,输出将输出到stdout。

If your directory contains files that are not audio files, eg. 如果您的目录包含不是音频文件的文件,例如 .DS_Store , etc. you could filter known file extensions: .DS_Store等,您可以过滤已知的文件扩展名:

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: 但是,所有文件的扩展名可能无法在这种情况下,重定向被称为stderrafinfo将输出Fail: AudioFileOpenURL failed时,指定的文件不是音频文件)来/dev/null将抑制所述错误:

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

You can now pipe to awk to get files with the expected bit rate: 现在,您可以通过管道传输到awk以获取具有预期比特率的文件:

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

The above will print filenames for files with bit rate < 320000 上面将打印比特率<320000的文件的文件名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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