Via Homebrew Cask (Recommended)
brew install --cask --no-quarantine artginzburg/tap/piposs
Check out the cask if you're interested.
Run the app, enable the PiPOSS extension in Safari, and give it access if it requests anything.
Now you'll be able to use P on your keyboard to toggle Picture in Picture mode for currently playing or the first appearing video on the page.
Also, YouTube will have a button for Picture in Picture.
If macOS refuses to run the app, run it via Ctrl+Click > Open
.