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

相關內容