Short code snippets

Some useful (that’s quite a subjective statement) bits of code I have had cause to write

Powershell – Shrink Video File Size

ffmpeg -i in.mp4 -vcodec libx265 -crf 28 out.mp4

Powershell – Flac to MP3

$flac = "C:\Users\richard\Desktop\sp\music\one"
$lame = "D:\apps\audacity\codec\lame.exe"
$tmp =  "C:\Users\richard\Desktop\sp\music\flac2mp3temp.wav"
$ffmpg = "D:\apps\audacity\codec\ffmpeg.exe"

Get-ChildItem $flac -Filter *.flac | 
Foreach-Object {
     $name = $_.FullName
     $bn = $_.Basename
     $dn = $_.DirectoryName
     Write-Host $bn
     Invoke-Expression "$ffmpg -i '$name' -ab 320k -map_metadata 0 -id3v2_version 3 '$dn\$bn.mp3'"
}

JKS to PEM and vice-versa for handling android signing key backup (powershell)

# The password to use for existing or new keystores etc.
[string]$pwd = ""
# location of any existing JKS keystore or where the new keystore will be made
[string]$jkspath = "rightprime.jks"
# location of any existing PEM (public and private key) for use in conversion to jks
[string]$pempath = "rightprime.pem"
# the path to the keytool executable (usually in %java_home%/bin)
# If you're using Android Studio you should find a copy of keytool somwhere on your system
[string]$keytoolpath = "C:\PROGRA~1\Java\RedHatJDK8\bin\keytool.exe"
# the path or command invocation to use for openssl (generally we'd use "wsl openssl")
# If you don't have WSL running you will have to install openssl some other way
[string]$opensslpath = "wsl openssl"
# the name of the app, used as the keyname in the keystore (no spaces etc.)
[string]$appname = "rightprime"
# the info to use in the new key
# note the dc entries, this should be the package name of our app
[string]$dname = "cn=Richard Senior, ou=Dev, o=RightPrime Software, l=Leeds, st=Yorkshire, c=UK, dc=rightprime, dc=android, dc=richardsenior, dc=net"

function generateKeystoreSuitableForGoogleSigningKey {
    Write-Host "Generating a new Java Keystore file at ${jkspath}"
    Write-Host "Make sure one doesn't already exist or you'll get a key error"
    Write-Host ""
    Invoke-Expression "${keytoolpath} -genkey -v -keystore ${jkspath} -alias ${appname} -keyalg RSA -keysize 2048 -validity 10000 -dname '${dname}' -storepass ${pwd} -keypass ${pwd}"
}

function toPem {
    Write-Host "Extracting public and private keys from ${jkspath} into PEM file at ${pempath}"
    Write-Host ""
	# export (convert to) existing keystore to pkcs12 format
	Invoke-Expression "${keytoolpath}  -importkeystore -srckeystore ${jkspath} -destkeystore temp -srcstoretype jks -deststoretype pkcs12 -srcstorepass ${pwd} -deststorepass ${pwd}"
	# export from pkcs12 keystore to pem format
	Invoke-Expression "${opensslpath} pkcs12 -in ./temp -out ${pempath} -passin pass:${pwd} -passout pass:${pwd}"
}

function toJks {
	# import PEM "private and cert" file to a jks in pkcs12 format
    # PEM file should contain "Bag Attributes" taken from a previous export
	Write-Host "Creating new JKS keystore (${jkspath}) and importing the information from the PEM. The PEM should have both public and private keys"
	Write-Host ""
    Invoke-Expression "${opensslpath}  pkcs12 -export -in ${pempath} -out cert.p12 -name '${appname}' -passin pass:_Abq5heyro12 -passout pass:_Abq5heyro12"
    Invoke-Expression "${keytoolpath} -importkeystore -srckeystore cert.p12 -deststoretype pkcs12 -destkeystore ${jkspath} -srcstorepass ${pwd} -deststorepass ${pwd} -dname '${dname}' -alias ${appname}"
}

function exportForGoogle {
    Write-Host "Creating a PEM file (upload_certificate.pem) for upload to google as our signing key"
    Write-Host ""
    # exports in the correct format for submission to Google as your new signing key
    Invoke-Expression "${keytoolpath} -export -rfc -keystore ${jkspath} -alias ${appname} -file upload_certificate.pem -storepass ${pwd} -keypass ${pwd}"
}

# create a new keystore using the configured password for store and key
generateKeystoreSuitableForGoogleSigningKey
# get a human readable text (utf-8) version of the keystore for backup purposes
toPem
# get a pem for upload to google as the signing key for this app
exportForGoogle