0

Hello and thanks for reading.

So I'm using Handbrake (video encoding) GUI version (not the CLI version).

I have a script that configures the settings for the GUI version. But I want to specify the default preset. Unfortunately, the default preset is not in the settings.json file.

The default preset is stored in the presets.json file.

The presets.json file contains all the presets (such as Very Fast 1080p30 and Fast 720p30, etc). I want to set Fast 720p30 as the default preset.

So I can find this in presets.json:

"PresetName": "Fast 720p30",

And just above that, I see this:

"Default": false,

Then I also look for this:

"PresetName": "Fast 1080p30",

And just above that I see this:

"Default": true,

So obviously 'Fast 1080p30' is the current default. I want to change that to 'Fast 720p30'.

To do that manually, I need to set 'Default' to true for 'Fast 720p30' and set 'Default' to false (or just remove the line) for 'Fast 1080p30'.

How can I do this using script? I am using Windows 7 SP1 so can use any scripting language that runs on Windows 7 (Batch, VBScript, PowerShell, JavaScript, maybe even R, etc).

My main problem is that I cannot find a way to access the '"Default": false,' line for any given preset because the JSON elements don't seem to have names. So I'm thinking maybe I need to find '"PresetName": "Fast 720p30",' then find the '"Default": false,' above that and change it, all using script. But I don't know how to do that.

I cannot paste the full presets.json file here because it's nearly 9000 lines long. But here is an edited version featuring the main parts:

{
  "PresetList": [
    {
      "ChildrenArray": [
        {
          "AlignAVStart": true,
          "AudioCopyMask": [
            "copy:aac"
          ],
          "AudioEncoderFallback": "ac3",
          "AudioLanguageList": [],
          "AudioList": [
            {
              "AudioBitrate": 160
            }
          ],
          "AudioSecondaryEncoderMode": true,
          "AudioTrackSelectionBehavior": "first",
          "ChapterMarkers": true,
          "ChildrenArray": [],
          "Default": false,
          "PresetName": "Very Fast 1080p30",
          "Type": 0
        },
        {
          "AlignAVStart": true,
          "AudioCopyMask": [
            "copy:aac"
          ],
          "AudioEncoderFallback": "ac3",
          "AudioLanguageList": [],
          "AudioList": [
            {
              "AudioBitrate": 160
            }
          ],
          "Default": false,
          "PresetName": "Very Fast 720p30",
          "Type": 0
        },
        {
          "AlignAVStart": true,
          "AudioCopyMask": [
            "copy:aac"
          ],
          "AudioEncoderFallback": "ac3",
          "AudioLanguageList": [],
          "AudioList": [
            {
              "AudioBitrate": 160
            }
          ],
          "AudioSecondaryEncoderMode": true,
          "AudioTrackSelectionBehavior": "first",
          "ChapterMarkers": true,
          "ChildrenArray": [],
          "Default": false,
          "PresetName": "Very Fast 576p25",
          "Type": 0,
        },
        {
          "AlignAVStart": true,
          "AudioCopyMask": [
            "copy:aac",
            "copy:ac3",
            "copy:dtshd",
            "copy:dts",
            "copy:mp3",
            "copy:truehd",
            "copy:flac",
            "copy:eac3"
          ],
          "AudioEncoderFallback": "ac3",
          "AudioLanguageList": [],
          "AudioList": [
            {
              "AudioBitrate": 160,
              "AudioCompressionLevel": 0.0,
              "AudioDitherMethod": null,
              "AudioEncoder": "av_aac",
              "AudioMixdown": "stereo",
              "AudioNormalizeMixLevel": false,
              "AudioSamplerate": "auto",
              "AudioTrackQualityEnable": false,
              "AudioTrackQuality": -1.0,
              "AudioTrackGainSlider": 0.0,
              "AudioTrackDRCSlider": 0.0
            }
          ],
          "AudioSecondaryEncoderMode": true,
          "AudioTrackSelectionBehavior": "first",
          "ChapterMarkers": true,
          "ChildrenArray": [],
          "Default": false,
          "PresetName": "Very Fast 480p30",
          "Type": 0
        },
        {
          "AlignAVStart": true,
          "AudioCopyMask": [
            "copy:aac",
            "copy:ac3",
            "copy:dtshd",
            "copy:dts",
            "copy:mp3",
            "copy:truehd",
            "copy:flac",
            "copy:eac3"
          ],
          "AudioEncoderFallback": "ac3",
          "AudioLanguageList": [],
          "AudioList": [
            {
              "AudioBitrate": 160,
              "AudioCompressionLevel": 0.0,
              "AudioDitherMethod": null,
              "AudioEncoder": "av_aac",
              "AudioMixdown": "stereo",
              "AudioNormalizeMixLevel": false,
              "AudioSamplerate": "auto",
              "AudioTrackQualityEnable": false,
              "AudioTrackQuality": -1.0,
              "AudioTrackGainSlider": 0.0,
              "AudioTrackDRCSlider": 0.0
            }
          ],
          "AudioSecondaryEncoderMode": true,
          "AudioTrackSelectionBehavior": "first",
          "ChapterMarkers": true,
          "ChildrenArray": [],
          "Default": true,
          "PresetName": "Fast 1080p30",
          "Type": 0
        },
        {
          "AlignAVStart": true,
          "AudioCopyMask": [
            "copy:aac",
            "copy:ac3",
            "copy:dtshd",
            "copy:dts",
            "copy:mp3",
            "copy:truehd",
            "copy:flac",
            "copy:eac3"
          ],
          "AudioEncoderFallback": "ac3",
          "AudioLanguageList": [],
          "AudioList": [
            {
              "AudioBitrate": 160,
              "AudioCompressionLevel": 0.0,
              "AudioDitherMethod": null,
              "AudioEncoder": "av_aac",
              "AudioMixdown": "stereo",
              "AudioNormalizeMixLevel": false,
              "AudioSamplerate": "auto",
              "AudioTrackQualityEnable": false,
              "AudioTrackQuality": -1.0,
              "AudioTrackGainSlider": 0.0,
              "AudioTrackDRCSlider": 0.0
            }
          ],
          "AudioSecondaryEncoderMode": true,
          "AudioTrackSelectionBehavior": "first",
          "ChapterMarkers": true,
          "ChildrenArray": [],
          "Default": false,
          "PresetName": "Fast 720p30",
          "Type": 0
        }
      ],
      "Folder": true,
      "PresetName": "Production",
      "PresetDescription": null,
      "Type": 0
    }
  ],
  "VersionMajor": 40,
  "VersionMicro": 0,
  "VersionMinor": 0
}

Many thanks.

Jstation
  • 407
  • 4
  • 14
  • Can you share the complete `presets.json` file (in the question?) – iRon Dec 14 '19 at 14:42
  • I cannot paste the full presets.json as it's nearly 9000 lines long. But I edited my post to include the parts of the file that are relevant to my question. – Jstation Dec 14 '19 at 14:55

1 Answers1

1

Using PowerShell and JSON is not too difficult.

In this case, you can do:

$jsonPath = Join-Path -Path $env:APPDATA -ChildPath 'HandBrake\presets.json'
# load the file and convert from json to get an array of objects
$json = Get-Content -Path $jsonPath | ConvertFrom-Json
# look for the item where the PresetName property is 'Fast 1080p30' and set its Default property to $false
$json.PresetList.ChildrenArray | Where-Object { $_.PresetName -eq 'Fast 1080p30' } | ForEach-Object { $_.Default = $false }

# look for the item where the PresetName property is 'Fast 720p30' and set its Default property to $true
$json.PresetList.ChildrenArray | Where-Object { $_.PresetName -eq 'Fast 720p30' } | ForEach-Object { $_.Default = $true }

# next save the updated data as json file.
# for demo, I'm using a new name so the original file is not overwritten.
# if you have inspected the output file and all is correct, remove or comment out the next line:
$jsonPath = Join-Path -Path $env:APPDATA -ChildPath 'HandBrake\presets2.json'

$json | ConvertTo-Json -Depth 99 | Set-Content -Path $jsonPath

Hope that helps

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thanks. This works but the new file is massively indented. Each element section in the JSON table is indented by many, many columns. Example, 'AlignAVStart' at the top of the file starts at column 6 in presets.json while in presets2.json it starts at column 54. Is there any way of preserving the formatting or at least not have it indented so much? – Jstation Dec 14 '19 at 15:08
  • I fixed the formatting using the function on https://github.com/PowerShell/PowerShell/issues/2736. presets2.json now has the same indents as presets.json. It contains extra lines because it splits lines containing [] over 3 lines but I guess I can live with that. – Jstation Dec 14 '19 at 15:17
  • @Jstation You didn't mention the formatting, but you're right, ConvertTo-Json doesn't make a pretty looking json file. I wrote a function [Format-Json](https://stackoverflow.com/a/56324939/9898643) for that once. See if you like that better. – Theo Dec 14 '19 at 15:21
  • Thanks @Theo. I tried your Format-Json function and found that it still indented too much whereas the Format-Json from the GitHub link above your answer indents by the right amount. I will mark your answer as correct though because your PowerShell script modifies the JSON file most excellently. Many thanks. – Jstation Dec 14 '19 at 15:44