2010-07-18, 18:42
I wrote a little script which copies a xbmc video library database while adjusting path names on the fly.
The script takes special care to copy thumbnails and fanarts using recalculated hashes, which is needed because of changed paths. It also correctly handles thumbs for movie sets, tv shows, seasons, multi episode files and such.
I needed this because I don't have uniform path names to my video collection on different machines in my network (think nfs mount vs. "smb://"). It may also be useful for people who want to move their video collection around without rescraping everything. (One would think that export/import is already sufficient for the later case, but I found it isn't, because tv show episode thumbs do get lost.)
Maybe someone would find it usefull.
The script takes special care to copy thumbnails and fanarts using recalculated hashes, which is needed because of changed paths. It also correctly handles thumbs for movie sets, tv shows, seasons, multi episode files and such.
I needed this because I don't have uniform path names to my video collection on different machines in my network (think nfs mount vs. "smb://"). It may also be useful for people who want to move their video collection around without rescraping everything. (One would think that export/import is already sufficient for the later case, but I found it isn't, because tv show episode thumbs do get lost.)
Maybe someone would find it usefull.
Code:
#!/bin/bash
#
# Create copies of xbmc video library database and thumbnails
# with adjusted path names of all referenced video files.
#
# Usage: ./copyprofile.sh SED SRC DST...
#
# SED sed script to filter path names, e.g. "s/\/mnt\/pub/smb:\/\/srv\/pub/"
# SRC source dir containing .xbmc/ to be used as master profile
# DST destination dir(s) where cloned .xbmc/ profiles will pe placed
#
# Required packages: sqlite3 libdigest-crc-perl
#
sed=$1
src=$2
shift 2
dst=("$@")
videodb=.xbmc/userdata/Database/MyVideos34.db
vthumbs=.xbmc/userdata/Thumbnails/Video
vfanart=.xbmc/userdata/Thumbnails/Video/Fanart
set -e
videofiles()
{
sqlite3 $1 "
SELECT strPath FROM path;
SELECT strPath||strFilename FROM files
JOIN path ON files.idPath = path.idPath;
SELECT strPath||strFilename||'episode'||c13 FROM episode
JOIN files ON episode.idFile = files.idFile
JOIN path ON files.idPath = path.idPath;
SELECT 'season'||strPath||'season '||c12 FROM episode
JOIN tvshowlinkepisode ON episode.idEpisode = tvshowlinkepisode.idEpisode
JOIN tvshowlinkpath ON tvshowlinkepisode.idShow = tvshowlinkpath.idShow
JOIN path ON tvshowlinkpath.idPath = path.idPath
GROUP BY tvshowlinkepisode.idShow,c12;
SELECT 'season'||strPath||'* all seasons' FROM tvshow
JOIN tvshowlinkpath ON tvshow.idShow = tvshowlinkpath.idShow
JOIN path ON tvshowlinkpath.idPath = path.idPath;
SELECT 'season'||strPath||'specials' FROM tvshow
JOIN tvshowlinkpath ON tvshow.idShow = tvshowlinkpath.idShow
JOIN path ON tvshowlinkpath.idPath = path.idPath;
SELECT 'videodb://1/7/'||idSet||'/' FROM sets;"
}
thumbcrc()
{
perl -mDigest::CRC=crc -ne'chomp; printf "%08x\n",crc(lc,32,-1,0,0,79764919)'
}
thumbcopy()
{
[ ! -e $1 ] || ( mkdir -p $2 && cp --preserve=timestamps $1 $2/$3 )
}
tmp=$(mktemp -d)
mkdir -p $tmp/$(dirname $videodb)
sqlite3 $src/$videodb .dump | sqlite3 $tmp/tmp.db
sqlite3 $tmp/tmp.db .dump | sed "$sed" | sqlite3 $tmp/$videodb
videofiles $tmp/tmp.db | sed "p;$sed" | thumbcrc | while read a && read b
do
thumbcopy "$src/$vthumbs/${a::1}/${a}.tbn" "$tmp/$vthumbs/${b::1}" "${b}.tbn"
thumbcopy "$src/$vfanart/${a}.tbn" "$tmp/$vfanart" "${b}.tbn"
done
for d in "${dst[@]}"
do
cp -r --preserve=timestamps $tmp/.xbmc $d/
[ "$(id -u)" == "0" ] && chown -R $(stat -c %U:%G $d) $d/.xbmc
done
rm -r $tmp/.xbmc/ $tmp/tmp.db
rmdir $tmp