Skip to content

Instantly share code, notes, and snippets.

@shantigilbert
Last active February 13, 2019 03:45
Show Gist options
  • Save shantigilbert/b6599bb4cf049460afa96bc220b3099a to your computer and use it in GitHub Desktop.
Save shantigilbert/b6599bb4cf049460afa96bc220b3099a to your computer and use it in GitHub Desktop.
Convert xtras (from emuxtras) Synopsis to gamelist.xml for Emulationstation
#!/bin/bash
# This is a simple script will attempt to create a gamelist.xml to be used in Emulationstation based on emuxtras Synopsis files.
# You will need to have your Roms, Movies, Artwork Xtras in a directory above this script
# convert_xtras.sh --
# |
# NES
# Master System
# SNES
# to run just pass the dirname of the Xtras you want to convert, the dirname should always be the first parameter
# eg. ./convert_xtras.sh "NES"
# gamelist.xml will be inside the folder along with a log that will show you which files didn't have a synopsis file named the same as the ROM
# Note: You will need to have "recode", "perl" and "ffmpeg" (only if you need to convert videos) installed
# if you would like to convert the xmv videos to mp4 pass the -cvideos argument after the dirname
# usage: ./convert_xtras.sh "dirname" [-cvideos]
# 2019 - Shanti Gilbert
check_synopsis() {
# Thanks to https://stackoverflow.com/users/8572380/tshiono for his help on this function
perl -e '
$ARGV[0] =~ s/\.[^\.][^.]*$/\\ /g; # remove extension
@files1 = "$ARGV[0]";
$ARGV[1] =~ s/ /\\ /g; # escape espaces with \
@files2 = glob "$ARGV[1]/*";
foreach (@files1) {
$f2 = $_;
s#.*/##; # remove directory name
s#[\W_]#[\\W_]?#g; # replace non-alphanumric chars
$pat = $_ . "\\.\\w+\$";
# print $pat, "\n"; # uncomment to see the regex pattern
$matched=0;
foreach $f1 (@files2) {
if ($f1 =~ m#/$pat#i) {
print "$f1\n";
$matched=1;
last;
}
}
if ($matched <=0) {
# print "No match for $f2\n";
} else {
last;
}
}' -- "$1" "$2"
}
arguments="$@";
command -v recode >/dev/null 2>&1 || { echo >&2 "This script requires recode but it's not installed. Try \"sudo apt install recode\" - Aborting."; exit 1; }
command -v perl >/dev/null 2>&1 || { echo >&2 "This script requires perl but it's not installed. Try \"sudo apt install perl\" - Aborting."; exit 1; }
if [[ $arguments == *"-cvideos"* ]]; then
command -v ffmpeg >/dev/null 2>&1 || { echo >&2 "This script requires ffmpeg to convert videos but it's not installed. Try \"sudo apt install ffmpeg\" - Aborting."; exit 1; }
fi
OLDIFS="$IFS" # save it
IFS="" # don't split on any white space
echo "Generating gamelist.xml...Please Wait..."
# Initialize log
LOG="./$1/convert.log"
echo "Convert Log" > "$LOG"
# Set folders
synopsis_folder="./$1/*ynopsis"
artwork_folder="./$1/Artwork/Action"
movies_folder="./$1/Movies"
# Initialize XML
read -d '' content <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<gameList>
EOF
echo "$content" > "./$1/gamelist.xml"
# search for Roms
find "./$1/Roms" -iname "*.zip" -print0 | sort -z | while IFS= read -r -d $'\0' f;
do
# populate the fields, TODO: a way to select the image to use from all the options
gpath=${f/.\/$1/.}
movie=$(check_synopsis "$f" "$movies_folder")
if [[ -n $movie ]]; then
if [[ $arguments == *"-cvideos"* ]]; then
ffmpeg -i "$movie" -vcodec h264 -acodec aac -strict -2 "${movie%.xmv}.mp4" < /dev/null
movie=${movie/.\/$1/.}
movie="${movie%.xmv}.mp4"
else
movie=${movie/.\/$1/.}
fi
else
# echo "Movie for $f not found" >> "$LOG"
movie=""
fi
image=$(check_synopsis "$f" "$artwork_folder")
image=${image/.\/$1/.}
if [[ ! -n $image ]]; then
# echo "Image for $f not found" >> "$LOG"
image=""
fi
# Make sure there is a Synopsis entry for this rom else just leave the variables blank
FILE=$(check_synopsis "$f" "$synopsis_folder")
if [[ -n $FILE ]]; then
title=$(sed -n '1{p;q;}' $FILE )
echo "Synopsis found for $title, adding to gamelist.xml"
controller=$(awk '/^Controller: /{print $NF}' $FILE)
genre=$(awk '/^Genre: /{print $NF}' $FILE)
ryear=$(awk '/^Release Year: /{print $NF}' $FILE)
developer=$(awk '/^Developer: /{print $NF}' $FILE)
publisher=$(awk '/^Publisher: /{print $NF}' $FILE)
players=$(awk '/^Players: /{print $NF}' $FILE)
desc=$(cat $FILE | sed '1,/__/d' | tail -n +2 | sed 'N;s/\n//' | tr '\r\n' ' ' | recode ..h0)
else
title="$(basename "$f" .zip)"
echo "Synopsis NOT found for $title, adding blank entry to gamelist.xml"
echo "./Synopsis/$(basename "$f" .zip).txt not found" >> "$LOG"
controller=""
genre=""
ryear=""
developer=""
publisher=""
players=""
desc=""
fi
read -d '' content <<EOF
<game id="" source="Emuxtras-Synopsis-converter">
<path>$gpath</path>
<name>$(echo $title | recode ..h0)</name>
<desc>$(echo $desc | recode ..h0)</desc>
<image>$image</image>
<rating></rating>
<releasedate>$ryear</releasedate>
<developer>$(echo $developer | recode ..h0)</developer>
<publisher>$(echo $publisher | recode ..h0)</publisher>
<genre>$(echo $genre | recode ..h0)</genre>
<players>$(echo $players | recode ..h0)</players>
<video>$movie</video>
</game>
EOF
echo "$content" >> "./$1/gamelist.xml"
done
echo "</gameList>" >> "./$1/gamelist.xml"
echo "Done!"
IFS=$OLDIFS # restore IFS
# not yet sure if needed
# if [[ $arguments == *"-deletexmv"* ]]; then
# read -p "Are you sure? you want to delete al xmv files? " -n 1 -r
# echo # (optional) move to a new line
# if [[ $REPLY =~ ^[Yy]$ ]]
# then
# echo "Deleting all xmv files"
# rm ./"$1"Movies/*.xmv
# fi
#fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment