Skip to content

Instantly share code, notes, and snippets.

@ssokolow
Last active December 18, 2022 00:44
Show Gist options
  • Save ssokolow/4c8680be0e19f5448d9b to your computer and use it in GitHub Desktop.
Save ssokolow/4c8680be0e19f5448d9b to your computer and use it in GitHub Desktop.
Script for generating BIN/CUE pairs under Linux in a manner suitable for ripping Playstation games for emulation
#!/bin/bash
# NOTE: If the generated file contains audio tracks, they will have the wrong
# endianness for the .CUE file and something like BinChunker or my
# swab.py must be used to rewrite the .BIN file before it can be
# used. (This isn't automatic because swapping the byte order
# will break the .TOC file the .CUE was generated from)
#
# Required packages:
# - cdrdao (for the actual ripping and toc2cue)
# - eject (for closing and opening the tray)
# - genisoimage (for getting the volume name via isoinfo)
# - kde-runtime-data (for the default completion notification sound)
# - sox (for audio completion notification)
DRIVE_PATH="/dev/sr1"
DONE_SOUND="/usr/share/sounds/KDE-Im-Nudge.ogg"
# Ensure the disc is loaded
# TODO: Figure out how to wait for the TOC to be read
eject -t "$DRIVE_PATH"
# Get the volume name either from the disc or from the command-line
if [ -n "$1" ]; then
volname="$1"
else
volname=$(isoinfo -d -i "$DRIVE_PATH" | grep 'Volume id:' | awk '{ print $3 }')
fi
volname="${volname// /_}"
# Ensure there's a containing folder and change into it
mkdir -p "$volname"
pushd "$volname" > /dev/null
# Make sure we can get exclusive access to the disc
umount "$DRIVE_PATH"
# Rip it or die
cdrdao read-cd --read-raw --datafile "$volname".bin --device "$DRIVE_PATH" --driver generic-mmc-raw "$volname".toc || exit 2
# Notify success and eject
play -q "$DONE_SOUND"
sleep 1
eject "$DRIVE_PATH"
# Generate a .CUE file and make it clear if there are audio tracks to byte-swap
# TODO: Find a way to generate an ISO if the full strength of BIN is excessive
toc2cue "$volname".toc "$volname".cue > /dev/null
# TODO: Detect if there are audio tracks automatically and, if so, byte-swap
cat "$volname".cue
# Offer a streamlined way to note down the CD key if present
read -p "Does the CD have a CD-Key? " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
$VISUAL "cd_key.txt"
fi
## Properly quote the cue file contents. (an alernative to subbing in underscores)
#sed -i 's@^FILE \([^"].*[^"]\) BINARY@FILE "\1" BINARY@' .cue
popd > /dev/null
#7z a "$volname".7z "$volname" && rm -rf "$volname"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment