使用 MediaInfo 命令行和 jq 从文件目录构建有效的 JSON 播放列表
Building valid JSON playlist from directory of files using MediaInfo Command Line and jq
我在 ubuntu 上使用 MediaInfo 命令行 v18.08 解析目录中的多个文件并输出 JSON,如下所示:mediainfo * --output=JSON
为每个文件输出以下内容 JSON(略微缩减)
{
"media": {
"@ref": "openingmusic.mp3",
"track": [
{
"@type": "General",
"Duration": "17.789",
"Encoded_Library": "LAME3.98r"
},
{
"@type": "Audio",
"Format": "MPEG Audio",
"Encoded_Library_Settings": "-m s -V 2 -q 3 -lowpass 18.6 --vbr-old -b 32"
}
]
}
}
但我只想要 JSON 的一个子集,所以我使用了 jq-1.5-1
mediainfo *.mp3 --output=JSON | jq '. | {duration: .media.track[0].Duration, pubDate: .media.track[0].File_Modified_Date_Local, url: .media."@ref"}'
很好,除了元素之间没有 ,
,所有元素都没有包含在 [] 中,当然,虽然我可以捏造那部分,但如果我加入输出,我会得到一个尾随逗号。
请问正确的 jq 方法是什么?
您可以使用 --slurp
选项:
- --slurp/-s:
Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once.
结合 map
到 运行 您对数组每个元素的过滤器:jq -s 'map({duration: .media.track[0].Duration, pubDate: .media.track[0].File_Modified_Date_Local, url: .media."@ref"})'
那么输出仍然是一个数组。
我在 ubuntu 上使用 MediaInfo 命令行 v18.08 解析目录中的多个文件并输出 JSON,如下所示:mediainfo * --output=JSON
为每个文件输出以下内容 JSON(略微缩减)
{
"media": {
"@ref": "openingmusic.mp3",
"track": [
{
"@type": "General",
"Duration": "17.789",
"Encoded_Library": "LAME3.98r"
},
{
"@type": "Audio",
"Format": "MPEG Audio",
"Encoded_Library_Settings": "-m s -V 2 -q 3 -lowpass 18.6 --vbr-old -b 32"
}
]
}
}
但我只想要 JSON 的一个子集,所以我使用了 jq-1.5-1
mediainfo *.mp3 --output=JSON | jq '. | {duration: .media.track[0].Duration, pubDate: .media.track[0].File_Modified_Date_Local, url: .media."@ref"}'
很好,除了元素之间没有 ,
,所有元素都没有包含在 [] 中,当然,虽然我可以捏造那部分,但如果我加入输出,我会得到一个尾随逗号。
请问正确的 jq 方法是什么?
您可以使用 --slurp
选项:
- --slurp/-s:
Instead of running the filter for each JSON object in the input, read the entire input stream into a large array and run the filter just once.
结合 map
到 运行 您对数组每个元素的过滤器:jq -s 'map({duration: .media.track[0].Duration, pubDate: .media.track[0].File_Modified_Date_Local, url: .media."@ref"})'
那么输出仍然是一个数组。