-4

I'm using jq, but I didn't manage to get a value extracted from a .json-file (I got generated by mediainfo) in my batch-script.

How can I get the value of "Duration" in a variable for further use?

{
"creatingLibrary": {
"name": "MediaInfoLib",
"version": "21.09",
"url": "https://mediaarea.net/MediaInfo"
},
"media": {
"@ref": "J:\\Austria\\Tag5\\EOS_ben\\100EOS5D\\TW_6248.MOV",
"track": [
{
"@type": "General",
"VideoCount": "1",
"AudioCount": "1",
"OtherCount": "1",
"FileExtension": "MOV",
"Format": "MPEG-4",
"Format_Profile": "QuickTime",
"CodecID": "qt  ",
"CodecID_Version": "2007.09",
"CodecID_Compatible": "qt  /CAEP",
"FileSize": "37229924",
"Duration": "9.520",
"OverallBitRate_Mode": "VBR",
"OverallBitRate": "31285650",
"FrameRate": "25.000",
"FrameCount": "238",
"StreamSize": "100484",
"HeaderSize": "98304",
"DataSize": "37130652",
"FooterSize": "968",
"IsStreamable": "Yes",
"Encoded_Date": "UTC 2014-03-13 22:31:17",
"Tagged_Date": "UTC 2014-03-13 22:31:17",
"File_Created_Date": "UTC 2014-03-13 21:31:26.000",
"File_Created_Date_Local": "2014-03-13 23:31:26.000",
"File_Modified_Date": "UTC 2014-03-13 21:31:26.000",
"File_Modified_Date_Local": "2014-03-13 23:31:26.000",
"Copyright": "BEN PAYA",
"extra": {
"com_apple_quicktime_make": "Canon",
"com_apple_quicktime_model": "Canon EOS 5D Mark III",
"com_apple_quicktime_rating_user": "0.000",
"com_apple_quicktime_author": "PhilFried"
}
},
{
"@type": "Video",
"StreamOrder": "0",
"ID": "1",
.
.
.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    An answer is the subject of [a meta question](https://meta.stackoverflow.com/questions/417123/downvoting-working-answers-disrespects-community-and-op). – Peter Mortensen Apr 02 '22 at 18:23
  • Can you add more clarity to the question? E.g, is it a requirement that 'jq' should be used (fixing the problems using it)? Is it a requirement that 'jq' should ***not*** be used? Must it run under CMD on Windows? Is that a necessary restriction for *an answer*? Could it be in [PowerShell](https://en.wikipedia.org/wiki/PowerShell) on Windows? Please respond by [editing (changing) your question](https://stackoverflow.com/posts/71716898/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the question should appear as if it was written today). – Peter Mortensen May 05 '22 at 14:50
  • The canonical question is *[Parsing JSON with Unix tools](https://stackoverflow.com/questions/1955505/)* with 55 answers (it isn't very platform dependent). – Peter Mortensen May 07 '22 at 16:36

5 Answers5

1

That would be

your_json_file.media.track[0].Duration

Where .track[index] denotes a certain element (track), in this case the first one.


You should try an online JSON formatter like, JSON Formatter & Validator, to get automatic indentation.

This is your file content indented:

{
   "creatingLibrary":{
      "name":"MediaInfoLib",
      "version":"21.09",
      "url":"https://mediaarea.net/MediaInfo"
   },
   "media":{
      "@ref":"J:\\Austria\\Tag5\\EOS_ben\\100EOS5D\\TW_6248.MOV",
      "track":[
         {
            "@type":"General",
            "VideoCount":"1",
            "AudioCount":"1",
            "OtherCount":"1",
            "FileExtension":"MOV",
            "Format":"MPEG-4",
            "Format_Profile":"QuickTime",
            "CodecID":"qt  ",
            "CodecID_Version":"2007.09",
            "CodecID_Compatible":"qt  /CAEP",
            "FileSize":"37229924",
            "Duration":"9.520",
            "OverallBitRate_Mode":"VBR",
            "OverallBitRate":"31285650",
            "FrameRate":"25.000",
            "FrameCount":"238",
            "StreamSize":"100484",
            "HeaderSize":"98304",
            "DataSize":"37130652",
            "FooterSize":"968",
            "IsStreamable":"Yes",
            "Encoded_Date":"UTC 2014-03-13 22:31:17",
            "Tagged_Date":"UTC 2014-03-13 22:31:17",
            "File_Created_Date":"UTC 2014-03-13 21:31:26.000",
            "File_Created_Date_Local":"2014-03-13 23:31:26.000",
            "File_Modified_Date":"UTC 2014-03-13 21:31:26.000",
            "File_Modified_Date_Local":"2014-03-13 23:31:26.000",
            "Copyright":"BEN PAYA",
            "extra":{
               "com_apple_quicktime_make":"Canon",
               "com_apple_quicktime_model":"Canon EOS 5D Mark III",
               "com_apple_quicktime_rating_user":"0.000",
               "com_apple_quicktime_author":"PhilFried"
            }
         }
      ]
   }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

To get to FrameCount in the JSON content, the solution for me is:

jq.exe -r .media.track[0].FrameCount C:\MP4convert\test\%filenam%.json > test1.txt
for /f "delims=" %%x in (test1.txt) do set FrCountVar=%%x

For future use, I put the count in a new variable:

SET FrCount=%FrCountVar%

Thanks Finkle MacGraw's answer.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Why is the extra processing necessary? Can you add an example to illustrate it? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/71725389/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 05 '22 at 14:43
  • From [the Help Center](https://stackoverflow.com/help/promotion): *"...always explain why the solution you're presenting is appropriate and how it works"*. – Peter Mortensen May 05 '22 at 15:20
0

You may solve your problem in a very simple way via a pure Batch file:

@echo off
setlocal

for /F "tokens=2 delims=:, " %%a in ('findstr "Duration" test.txt') do set "Duration=%%~a"

echo Duration = %Duration%

Furthermore, you can get the values of all fields in the same way:

@echo off
setlocal

for /F "tokens=1,2 delims=:, " %%a in ('findstr /V "{ [ ] }" test.txt') do set "%%~a=%%~b"

rem Here you have the values of *all fields* in the json file. For example:

echo Duration = %Duration%
echo FrameCount = %FrameCount%
echo CodecID_Version = %CodecID_Version%

Output:

Duration = 9.520
FrameCount = 238
CodecID_Version = 2007.09
Aacini
  • 65,180
  • 12
  • 72
  • 108
-1
@ECHO OFF
SETLOCAL

rem The following settings for the source directory & filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "filename1=%sourcedir%\q71716898.txt"

REM (
FOR /f "usebackqtokens=1,2delims=:, " %%b IN ("%filename1%") DO (
 IF /i "%%~b"=="duration" SET "duration=%%~c"
)
SET duration
GOTO :EOF

Reasonably standard for /f "tokens processing.

The for/f assigns tokens from each line in turn, token 1 to %%b and token 2 to %%c.

Choosing delimiters of :, , and space, the first token on the target line is "Duration" and the second "9.520".

So, check that "%%~b" (the token, stripped of quotes, then re-enclosed in quotes is the same as the string duration, enclosed in quotes) and if so, assign the value of the second token to the variable duration.

The dequote/enquote ceremony is required in case the token contains separator characters which would interfere with the if parsing, and the /i makes the comparison case-insensitive.

Magoo
  • 77,302
  • 8
  • 62
  • 84
-8

It is best to use a tool that understands the JSON language. This will run in a batch-file from cmd.

I note that track is an array. This code selects the first element of the array.

FOR /F "delims=" %%A IN ('powershell.exe -NoLogo -NoProfile -Command ^
    "(ConvertFrom-Json -InputObject (Get-Content -Path '.\dura.json' -Raw)).media.track[0].Duration"') DO (SET "DURATION=%%~A")
ECHO DURATION is %DURATION%

If you want to process the Duration for all tracks, please try some code and create a new question if needed.

{} is used around a hash table. The use of [] indicates and array.

Zoe
  • 27,060
  • 21
  • 118
  • 148
lit
  • 14,456
  • 10
  • 65
  • 119