JQ:使用模式提取特定键的值

JQ: Extract value of a particular Key using pattern

mediainfo --Output=JSON 'Brazil.mp4' | jq '.media.track[]'
{
  "@type": "General",
  "VideoCount": "1",
  "AudioCount": "1",
  "FileExtension": "mp4",
  "Format": "MPEG-4",
  "Format_Profile": "Base Media",
  "CodecID": "isom",
  "CodecID_Compatible": "isom/iso2/avc1/mp41",
  "FileSize": "620542062",
  "Duration": "1531.263",
  "OverallBitRate_Mode": "VBR",
  "OverallBitRate": "3241988",
  "FrameRate": "30.000",
  "FrameCount": "45934",
  "StreamSize": "1454348",
  "HeaderSize": "40",
  "DataSize": "619584223",
  "FooterSize": "957799",
  "IsStreamable": "No",
  "File_Modified_Date": "UTC 2022-02-06 19:28:26",
  "File_Modified_Date_Local": "2022-02-06 20:28:26"
}
{
  "@type": "Video",
  "StreamOrder": "0",
  "ID": "1",
  "Format": "AVC",
  "Format_Profile": "High",
  "Format_Level": "4",
  "Format_Settings_CABAC": "Yes",
  "Format_Settings_RefFrames": "4",
  "CodecID": "avc1",
  "Duration": "1531.238",
  "Source_Duration": "1531.538",
  "BitRate": "3019356",
  "Width": "1912",
  "Width_Original": "1920",
  "Height": "1088",
  "Height_Original": "1080",
  "Stored_Height": "1088",
  "Sampled_Width": "1920",
  "Sampled_Height": "1080",
  "PixelAspectRatio": "1.000",
  "DisplayAspectRatio": "1.778",
  "Rotation": "0.000",
  "FrameRate_Mode": "VFR",
  "FrameRate": "30.000",
  "FrameRate_Minimum": "6.000",
  "FrameRate_Maximum": "120.000",
  "FrameCount": "45934",
  "ColorSpace": "YUV",
  "ChromaSubsampling": "4:2:0",
  "BitDepth": "8",
  "ScanType": "Progressive",
  "StreamSize": "577879629",
  "Source_StreamSize": "577914508",
  "Encoded_Library": "x264 - core 159",
  "Encoded_Library_Name": "x264",
  "Encoded_Library_Version": "core 159",
  "Encoded_Library_Settings": "cabac=1 / ref=3 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=7 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=1 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=12 / lookahead_threads=2 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=2 / keyint=90 / keyint_min=46 / scenecut=0 / intra_refresh=0 / rc_lookahead=40 / rc=crf / mbtree=1 / crf=23.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / vbv_maxrate=4000 / vbv_bufsize=133 / crf_max=27.0 / nal_hrd=none / filler=0 / ip_ratio=1.40 / aq=1:1.00",
  "extra": {
    "Source_Delay": "-67",
    "Source_Delay_Source": "Container",
    "mdhd_Duration": "1531238",
    "CodecConfigurationBox": "avcC"
  }
}
{
  "@type": "Audio",
  "StreamOrder": "1",
  "ID": "2",
  "Format": "AAC",
  "Format_AdditionalFeatures": "LC",
  "CodecID": "mp4a-40-2",
  "Duration": "1531.263",
  "BitRate_Mode": "VBR",
  "BitRate": "215289",
  "BitRate_Maximum": "427563",
  "Channels": "2",
  "ChannelPositions": "Front: L R",
  "ChannelLayout": "L R",
  "SamplesPerFrame": "1024",
  "SamplingRate": "44100",
  "SamplingCount": "67528698",
  "FrameRate": "43.066",
  "FrameCount": "65946",
  "Compression_Mode": "Lossy",
  "StreamSize": "41208085",
  "StreamSize_Proportion": "0.06641"
}

mediainfo --Output=JSON 'Brazil.mp4' | jq '.media.track[] | select(@type==General) | .Duration'
jq: error: General/0 is not defined at <top-level>, line 1:
.media.track[] | select(@type==General) | .Duration                               
jq: 1 compile error

您需要在字段名称(因为它包含 @ 字符)和查询字符串(因为它是一个字符串)两边加上引号:

jq '.media.track[] | select(."@type" == "General") | .Duration'
"1531.263"

Demo

注意:如果您想将输出作为文本 1531.263 而不是 JSON 字符串 "1531.263"(引号),则添加 --raw-output-r调用jq时的标志。 (Demo)