Skip to content

Instantly share code, notes, and snippets.

@eduo
Created November 18, 2009 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eduo/237427 to your computer and use it in GitHub Desktop.
Save eduo/237427 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
### Get Lists
ls -1 {episodes,movies}.txt | head -n 1 | while read LIST
do
### Go Through Lists
cat "${LIST}" | sort | while read FILE
do
### Get filename
FILENAME=${FILE##*/}
### Get File Name
NOEXT=${FILENAME%\.*}
### Define New Filename
NEWFILE=${NOEXT}".m4v"
### Get file Path
FILEPATH=${FILE%/*}
### Find subs
rm "${NOEXT}.subs" 2>/dev/null
touch "${NOEXT}.subs"
ls "${FILEPATH}/${NOEXT}"*{srt,sub} 2>/dev/null | while read sub
do
THESUB=${sub##*/}
SUBEXT=$(echo ${THESUB##*\.})
NOEXTSUB=${THESUB%\.*}
LANGSUB=${NOEXTSUB##*\.}
LANGUAGE="eng"
tempfile=$$
### Find if sub has language. Match against table. Get Language Full Name
if [ ${#LANGSUB} -eq 2 ]
then
LANGUAGE=$(cat apps/langs-3 | tr '\t' '\|' | grep \|${LANGSUB}\| | cut -f1 -d\|)
else
LANGUAGE="eng"
fi
### If Subtitle is of type "SUB" then convert to SRT
if [ "${SUBEXT}" = "sub" ]
then
if [ ! -e "${FILEPATH}/${NOEXT}".idx ]
then
cp "$sub" ./"${THESUB}"
cp ./"${THESUB}" "$$-${THESUB}"
### Get bitrate (for Subs)
FPS=$(apps/mplayer -vo null -ao null -frames 0 -identify "${FILE}" 2>/dev/null | grep ID_VIDEO_FPS | cut -f2 -d=)
echo "Converting $$-${THESUB} to ${NOEXTSUB}.srt with fps ${FPS}"
apps/sub2srt -f="${FPS}" "./$$-${THESUB}" > ./"${NOEXTSUB}".srt
echo "${LANGUAGE}|${NOEXTSUB}.srt" >> "${NOEXT}".subs
rm "$$-${THESUB}"
fi
elif [ "${SUBEXT}" = "srt" ]
then
cp "$sub" ./"${THESUB}"
cp ./"${THESUB}" "$$-${THESUB}"
cp ./"$$-${THESUB}" ./"${NOEXTSUB}".srt
echo "${LANGUAGE}|${NOEXTSUB}.srt" >> "${NOEXT}".subs
rm "$$-${THESUB}"
fi
### Finish Processing Subs
done
TOTALSUBS=$(cat "${NOEXT}.subs" | wc -l)
if [[ "${TOTALSUBS}" -gt 0 ]]
then
THESELANGS=$(cat "${NOEXT}.subs"|cut -d\| -f1| tr '\n' ',')
THESESUBS=$(cat "${NOEXT}.subs"|cut -d\| -f2| tr '\n' ',')
LANGSALL=${THESELANGS%\,}
SUBSALL=${THESESUBS%\,}
### Uncomment encoding to use
# Small and good quality - Slow coding
# apps/HandBrakeCLI -i "$FILE" -o "$NEWFILE" --preset "iPhone & iPod Touch" < /dev/null
# # echo "apps/HandBrakeCLI -i \"${FILE}\" -o \"${NEWFILE}\" --srt-file ${SUBSALL} --srt-lang ${LANGSALL} -e x264 -q 0.589999973773956 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 480 -m -x level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1 < /dev/null" >> commands
apps/HandBrakeCLI -i "${FILE}" -o "${NEWFILE}" --srt-file "${SUBSALL}" --srt-lang "${LANGSALL}" -e x264 -q 0.589999973773956 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 480 -m -x level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1 < /dev/null
# Not Small and Medium quality - Fast coding
# apps/ffmpeg -i "$FILE" -threads 4 -f mp4 -vcodec mpeg4 -maxrate 2500k -b 700k -r 23.98 -s 480x320 -bufsize 4M -acodec libfaac -qmin 3 -qmax 5 -g 300 -ab 192k "$NEWFILE" < /dev/null
else
# # echo "apps/HandBrakeCLI -i \"${FILE}\" -o \"${NEWFILE}\" -e x264 -q 0.589999973773956 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 480 -m -x level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1 < /dev/null" >> commands
apps/HandBrakeCLI -i "${FILE}" -o "${NEWFILE}" -e x264 -q 0.589999973773956 -a 1 -E faac -B 128 -R 48 -6 dpl2 -f mp4 -X 480 -m -x level=30:cabac=0:ref=2:mixed-refs:analyse=all:me=umh:no-fast-pskip=1 < /dev/null
fi
### Define new file name
FILENAME=${NEWFILE%%\.*}
### Get File List Type
TYPE=${LIST%%\.*}
if [ ${LIST} = "episodes.txt" ]
then
### This is a TV Episode Listing. Get Episode ID, Season Nbr., Episode Nbr., Short Name, Episode Title
EPISODE=$(echo $FILENAME | sed -e's/.* \([0-9]*x[0-9]*\) .*/\1/g')
SEASON=${EPISODE%%x*}
EPID=${EPISODE##*x}
TVSHOW=${NEWFILE%%$EPISODE*}
EPISODETITLE=${FILENAME##$TVSHOW $EPISODE - }
EPISODETITLE=${FILENAME##*- }
### Tag Files
if [ -f "${NEWFILE}" ]
then
apps/AtomicParsley "${NEWFILE}" \
--DeepScan iPod-uuid 1200 \
--overWrite \
--stik "TV Show" \
--title "${EPISODETITLE}" \
--TVShowName "${TVSHOW}" \
--TVEpisode "${EPISODE}" \
--TVSeasonNum "${SEASON}" \
--TVEpisodeNum "${EPID}" \
--comment "Converted and Tagged for iPod by Eduo with SolEol" \
--artist "${TVSHOW}" \
--albumArtist "${TVSHOW}" \
--album "${TVSHOW}, Season ${SEASON}" \
--tracknum "${EPID}/${EPID}" \
--disk "${SEASON}"
fi
elif [ ${LIST} = "movies.txt" ]
then
### File is a movie. Simpler tagging
if [ -f "${NEWFILE}" ]
then
apps/AtomicParsley "${NEWFILE}" \
--DeepScan iPod-uuid 1200 \
--overWrite \
--stik "Movie" \
--title "${FILENAME}" \
--comment "Converted and Tagged for iPod by Eduo with SolEol"
fi
fi
### Metemos el fichero en iTunes
if [ -f "${NEWFILE}" ]
then
apps/addmp4 "${NEWFILE}"
mv "$NEWFILE" movs/.
echo ""
fi
### Finish Processing File
done
### Finish Processing Files
echo ""
done
rm *.subs 2>/dev/null
rm *.srt 2>/dev/null
rm *.sub 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment