Mac OS xargs 将原始参数与新参数结合起来

Mac OS xargs 将原始参数与新参数结合起来

我能够列出具有完整路径的音频文件:(使用“-onlyin”进行测试)

$ mdfind -0 -onlyin ~/Music kind:audio | xargs -0 -n 1
    /Users/rrasch/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/1998-04-29_OB07264seg6of6_Z86.wav
    /Users/rrasch/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/1998-04-29_OB07264seg4of6_Z86.wav
    /Users/rrasch/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/1998-04-29_OB07264seg1of6_Z86.wav
    /Users/rrasch/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/1993-11-04_illiteracy_laura_jaeger_jim_latemer_2448.wav

我还可以通过运行 mdls 作为 xargs 中的参数来获取位深度和采样率:

$ mdfind -0 -onlyin ~/Music kind:audio | xargs -0 mdls -name kMDItemAudioSampleRate -name kMDItemBitsPerSample -raw | xargs -0 -n 2
    44100 24
    44100 24
    44100 24
    48000 24

如果我将 kMDItemFSName 添加到 mdls,我可以获得文件名以及位深度和采样率,但我丢失了路径:

$ mdfind -0 -onlyin ~/Music kind:audio | xargs -0 mdls -name kMDItemAudioSampleRate -name kMDItemBitsPerSample -name kMDItemFSName -raw | xargs -0 -n 3
44100 24 1998-04-29_OB07264seg6of6_Z86.wav
44100 24 1998-04-29_OB07264seg4of6_Z86.wav
44100 24 1998-04-29_OB07264seg1of6_Z86.wav
48000 24 1993-11-04_illiteracy_laura_jaeger_jim_latemer_2448.wav

我想扫描多个目录中的音频(部署时我将省略“-onlyin”)如何列出具有位深度和采样率的完整路径...如下所示:

44100 24 /Users/rrasch/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/1998-04-29_OB07264seg6of6_Z86.wav
44100 24 /Users/rrasch/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/1998-04-29_OB07264seg4of6_Z86.wav
44100 24 /Users/rrasch/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/1998-04-29_OB07264seg1of6_Z86.wav
48000 24 /Users/rrasch/Music/iTunes/iTunes Media/Music/Unknown Artist/Unknown Album/1993-11-04_illiteracy_laura_jaeger_jim_latemer_2448.wav

答案1

我使用gfind(brew install findutils)来实现与上面类似的:

$ gfind /tmp/temp -iname "*.mp4" -printf '\n' -exec ls {} \; -exec mdls -name kMDItemAudioSampleRate -name kMDItemBitsPerSample -raw {} \; | xargs -0 -n 2 

/tmp/temp/a.mp4
(null) (null)
/tmp/temp/b.mp4
(null)
(null)
/tmp/temp/c.mp4
(null) (null)
/tmp/temp/d.mp4
(null)
(null)

答案2

从以下位置调用一个简短的内联脚本xargs

mdfind -0 -onlyin ~/Music kind:audio | xargs -0 sh -c '
for pathname do
    printf "%s %s %s\n" \
        "$( mdls -name kMDItemAudioSampleRate -raw "$pathname" )" \
        "$( mdls -name kMDItemBitsPerSample   -raw "$pathname" )" \
        "$pathname"
done' sh

该循环从 接收批量找到的路径名mdfind。对于每个路径名,mdls用于提取您感兴趣的两个属性,并且使用 与路径名本身一起打印这些值printf

或者,尝试使用该kMDItemPath属性,它允许您mdls每个路径名仅调用一次:

mdfind -0 -onlyin ~/Music kind:audio | xargs -0 sh -c '
for pathname do
    mdls -name kMDItemAudioSampleRate \
         -name kMDItemBitsPerSample \
         -name kMDItemPath \
         -raw "$pathname" | tr "\0" " "
    echo
done' sh

相关内容