Skip to content

Instantly share code, notes, and snippets.

@manero6
Last active October 18, 2022 07:09
Show Gist options
  • Save manero6/418f9232919f2e29d640cd4758ebb4e1 to your computer and use it in GitHub Desktop.
Save manero6/418f9232919f2e29d640cd4758ebb4e1 to your computer and use it in GitHub Desktop.
Create .m3u playlist for multi-disc games (unzip .bin/.cue, create .chd files, create .m3u playlist)
#!/bin/bash
IFS=$'\n'
ARGUMENTS=$#
GAME_ZIP="$1"
GAME_NOZIP="${GAME_ZIP%.zip}"
GAME_NODISC="${GAME_NOZIP% *isc*}"
GAME_DIR=$GAME_NODISC
DISC=1
ctrl_c () {
echo -e "\n\e[1;31mAborting.\e[0m"
exit 130
}
check_arguments () {
if [ $ARGUMENTS = 1 ]
then
if [[ "$GAME_ZIP" == *.zip ]]
then
echo -e "\e[1mGame file:\e[0m"
echo "=> $GAME_ZIP"
else
echo -e "\e[1;31mPlease provide a .zip file. Aborting.\e[0m"
exit 1
fi
elif [ $ARGUMENTS = 0 ]
then
echo -e "\e[1;31mPlease provide one .zip file. Aborting.\e[0m"
exit 1
else
echo -e "\e[1;31mPlease provide no more than one .zip file. Aborting.\e[0m"
exit 1
fi
}
get_games () {
echo -e "\e[1mRelated files:\e[0m"
for i in $GAME_NODISC*.zip
do
echo "=> Disc $DISC: \"$i\""
((DISC++))
GAMES+=("$i")
GAMES_NOZIP+=("${i%.zip}")
done
}
make_dir () {
if [ ! -d "$GAME_DIR" ]
then
echo -e "\e[1mCreating \"$GAME_DIR\" folder:\e[0m"
mkdir "$GAME_DIR" && \
printf "=> " && realpath $GAME_DIR
else
echo -e "\e[1;31mDirectory \"$GAME_DIR\" already exist. Aborting.\e[0m"
exit 1
fi
}
extract_zip () {
echo -e "\e[1mExtracting .zip files into \"$GAME_DIR\":\e[0m"
for i in ${GAMES[@]}
do
echo "=> Extracting \"$i\""
7z x "$i" -o$GAME_DIR 1>/dev/null
done
}
create_chd () {
echo -e "\e[1mCreating .chd files:\e[0m"
for i in ${GAMES_NOZIP[@]}
do
echo "=> Creating \"$i.chd\""
chdman createcd -i $GAME_DIR/$i.cue -o $GAME_DIR/$i.chd 1>/dev/null
done
}
create_m3u () {
echo -e "\e[1mCreating .m3u playlist:\e[0m"
for i in ${GAMES_NOZIP[@]}
do
echo "=> Appending \"$i.chd\""
echo $i.chd >> $GAME_DIR/$GAME_NODISC.m3u
done
}
cleanup () {
echo -e "\e[1mRemoving .bin and .cue files:\e[0m"
for i in ${GAMES_NOZIP[@]}
do
echo "=> Removing \"$i.bin\""
if rm $GAME_DIR/$i.bin 2>/dev/null
then
:
else
echo -e "\e[1;31mNo \"$i.bin\" found!\e[0m"
echo -e "\e[1mRemoving other .bin files:\e[0m"
for games in $GAME_DIR/*.bin
do
game=`basename $games`
echo -e "=> Removing \"$game\""
rm $games
done
fi
echo "=> Removing \"$i.cue\""
rm $GAME_DIR/$i.cue
done
}
main () {
trap ctrl_c SIGINT
check_arguments
get_games
for i in make_dir \
extract_zip \
create_chd \
create_m3u \
cleanup
do
if $i
then
echo -e "\e[1;32m=> Done\e[0m"
else
echo -e "\n\e[1;31mAborting\e[0m"
exit 1
fi
done
}
main
@manero6
Copy link
Author

manero6 commented Oct 17, 2022

Before:

tree .
.
├── Final Fantasy VII (Germany) (Disc 1).zip
├── Final Fantasy VII (Germany) (Disc 2).zip
├── Final Fantasy VII (Germany) (Disc 3).zip

After:

tree .
.
├── Final Fantasy VII (Germany)
│   ├── Final Fantasy VII (Germany) (Disc 1).chd
│   ├── Final Fantasy VII (Germany) (Disc 2).chd
│   ├── Final Fantasy VII (Germany) (Disc 3).chd
│   └── Final Fantasy VII (Germany).m3u
├── Final Fantasy VII (Germany) (Disc 1).zip
├── Final Fantasy VII (Germany) (Disc 2).zip
├── Final Fantasy VII (Germany) (Disc 3).zip

Usage:

zip2m3u Final\ Fantasy\ VII\ \(Germany\)\ \(Disc\ 3\).zip
Game file:
=> Final Fantasy VII (Germany) (Disc 3).zip
Related files:
=> Disc 1: "Final Fantasy VII (Germany) (Disc 1).zip"
=> Disc 2: "Final Fantasy VII (Germany) (Disc 2).zip"
=> Disc 3: "Final Fantasy VII (Germany) (Disc 3).zip"
Creating "Final Fantasy VII (Germany)" folder:
=> /home/path/to/ps1/Final Fantasy VII (Germany)
=> Done
Extracting .zip files into "Final Fantasy VII (Germany)":
=> Extracting "Final Fantasy VII (Germany) (Disc 1).zip"
=> Extracting "Final Fantasy VII (Germany) (Disc 2).zip"
=> Extracting "Final Fantasy VII (Germany) (Disc 3).zip"
=> Done
Creating .chd files:
=> Creating "Final Fantasy VII (Germany) (Disc 1).chd"
Compression complete ... final ratio = 62.1%            
=> Creating "Final Fantasy VII (Germany) (Disc 2).chd"
Compression complete ... final ratio = 61.1%            
=> Creating "Final Fantasy VII (Germany) (Disc 3).chd"
Compression complete ... final ratio = 61.4%            
=> Done
Creating .m3u playlist:
=> Appending "Final Fantasy VII (Germany) (Disc 1).chd"
=> Appending "Final Fantasy VII (Germany) (Disc 2).chd"
=> Appending "Final Fantasy VII (Germany) (Disc 3).chd"
=> Done
Removing .bin and .cue files:
=> Removing "Final Fantasy VII (Germany) (Disc 1).bin"
=> Removing "Final Fantasy VII (Germany) (Disc 1).cue"
=> Removing "Final Fantasy VII (Germany) (Disc 2).bin"
=> Removing "Final Fantasy VII (Germany) (Disc 2).cue"
=> Removing "Final Fantasy VII (Germany) (Disc 3).bin"
=> Removing "Final Fantasy VII (Germany) (Disc 3).cue"
=> Done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment