Skip to content

Instantly share code, notes, and snippets.

@sidusnare
Last active April 12, 2019 15:49
Show Gist options
  • Save sidusnare/190b9f43605d017b41524df526b451aa to your computer and use it in GitHub Desktop.
Save sidusnare/190b9f43605d017b41524df526b451aa to your computer and use it in GitHub Desktop.
Media rip loop
#!/bin/bash
#Copyright (c) 2019 Fred Dinkler IV
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License, version 2, as
#published by the Free Software Foundation.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#
#You can retrieve a copy of the GNU General Public License
#at <http://www.gnu.org/licenses/>.
#
for p in ddrescue blktool file mount smartctl wodim cdrdao bchunk toc2cue lame;do
if ! which "${p}";then
echo "Please install ${p}"
exit 1
fi
done
if [ -z "${1}" ]; then
echo "Please provide a block device"
exit 1
fi
if [ ! -b "${1}" ]; then
echo "${1} is not a block device"
exit 1
fi
workdir="$(pwd)"
removable=no
unremovable=no
major="$((16#$(stat -c '%t' "${1}" ) ))"
vendor="$(smartctl -i "${1}" | grep ^Vendor | awk '{print($2)}')"
devtype="$( grep -m 1 -w "${major}" /proc/devices | awk '{print($2)}' )"
if [ "${devtype}" == 'sr' ] || [ "${vendor}" == 'IOMEGA' ]; then
removable=yes
fi
if [ "${devtype}" == 'sr' ]; then
unremovable=yes
fi
blktool "${1}" readonly on
echo -n "ReadOnly: "
blktool "${1}" readonly
while true;do
disk=default
cd "${workdir}" || exit 1
read -e -p "${1}:Name>" name
if [ -e "${name}.img" ] || [ -e "${name}.map" ] || [ -e "${name}" ]; then
echo "${name} already exists"
sname="$( awk '{print($1)}' <<< "${name}" )"
ls -lah "${sname}"*
continue
fi
if [ -z "${name}" ]; then
echo "Name cannot be blank"
continue
fi
if touch "${name}"; then
rm -f "${name}"
else
echo "Invalid name: ${name}"
continue
fi
clear
if [ "${unremovable}" == 'yes' ]; then
eject -t "${1}"
toc="$( wodim -v "dev=${1}" -toc )"
echo -e "${toc}"
if grep -v lout <<< "${toc}" | grep 'mode: -1' ; then
echo "Audio disk"
disk=audio
fi
fi
echo "Flushing"
fdflush "${1}"
sync
blktool "${1}" readonly on
echo -n "ReadOnly: "
blktool "${1}" readonly
echo "Ripping ${name}"
if [ "${disk}" == 'audio' ]; then
mkdir "${name}"
cd "${name}" || exit 1
mkdir "tm${$}"
echo "If data portion of rip fails, follow up with: ddrescue -S --retry-passes=5 \"${1}\" \"${name}.img\" \"${name}.map\""
cdrdao read-cd --device "${1}" --read-raw --datafile "${name}.bin" "${name}.toc"
toc2cue "${name}.toc" "${name}.cue"
bchunk -w "${name}.bin" "${name}.cue" "${name} "
for w in *.wav;do
lame -V4 -h -b 128 --vbr-new "${w}" "${w/.wav/.mp3}"
done
mv "${name} 01.iso" "${name}.iso"
if mount -o ro,loop "${name}.iso" "./tm${$}";then
ls "./tm${$}/"
umount "./tm${$}"
else
echo "Unable to mount ${name}"
fi
else
echo "ddrescue -S -M -A --retry-passes=5 \"${1}\" \"${name}.img\" \"${name}.map\""
ddrescue -S --retry-passes=5 "${1}" "${name}.img" "${name}.map"
file "${name}.img"
mkdir "tm${$}"
if mount -o ro,loop "${name}.img" "./tm${$}";then
ls "./tm${$}/"
umount "./tm${$}"
else
echo "Unable to mount ${name}"
fi
rmdir "./tm${$}"
fi
rmdir "tm${$}"
sname="$( awk '{print($1)}' <<< "${name}" )"
ls -lah "${sname}"*
if [ "${removable}" == 'yes' ]; then
eject "${1}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment