Last active
July 24, 2024 20:07
-
-
Save kitsook/52f16a90c147ba2be5ed323e607108cd to your computer and use it in GitHub Desktop.
Shell script to rip VCD with VLC and then transcode with ffmpeg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
## Script to rip VCD with VLC and then transcode with ffmpeg | |
if [ "${BASH_VERSINFO}" -lt 4 ]; then | |
echo "Require bash 4 or higher" | |
exit 1 | |
fi | |
# VCD source to rip. /dev/sr0 is the first SATA drive | |
SOURCE_DEV="/dev/sr0" | |
VLC_SOURCE_DRIVE="vcd://$SOURCE_DEV" | |
# ffmpeg transcoding parameters. H.264 with AAC | |
FFMPEG_OPTS="-c:v libx264 -crf 20 -c:a aac -b:a 128k -movflags +faststart" | |
# default output base folder | |
output_base="." | |
# default title | |
timestamp=`date "+%Y%m%d%H%M%S"` | |
# default title. used as output folder and file name | |
title="rip_${timestamp}" | |
# default starting disc count | |
start=1 | |
while getopts t:n:d: flag; do | |
case "${flag}" in | |
t) title=${OPTARG};; | |
n) start=${OPTARG};; | |
d) output_base=${OPTARG};; | |
\? ) echo "Uage: $0 [-t title] [-n starting_num] [-d destination_base_folder]" & exit;; | |
esac | |
done | |
trap breakTrap SIGINT | |
breakTrap() { | |
echo "Interrupted. Exiting..." | |
exit | |
} | |
destination=${output_base}/$title | |
mkdir -p "$destination" | |
declare -a pids=() | |
echo "Ripping \"$VLC_SOURCE_DRIVE\" into \"$destination\"" | |
for i in $(seq -w $start 999); do | |
while read -r -t 0; do read -r; done | |
echo | |
echo "**********************************" | |
echo "Press any key to rip next disc $i to \"${destination}\". Press 'q' to quit" | |
echo | |
read -n 1 key <&1 | |
if [[ $key = q ]]; then | |
break | |
fi | |
if [ -f "${destination}/${title} ${i}.dat" ]; then | |
while read -r -t 0; do read -r; done | |
echo "${destination}/${title} ${i}.dat exists. Press 'q' to quit" | |
read -n 1 key <&1 | |
if [[ $key = q ]]; then | |
break | |
fi | |
fi | |
# the built-in transcoding of vlc creates choppy video if the VCD disc is in poor condition. | |
# so first dump the streams and then transcode with ffmpeg in background | |
vlc -I "dummy" --play-and-exit "$VLC_SOURCE_DRIVE" --demux=dump --demuxdump-file="${destination}/${title} ${i}.dat" | |
eject -T "$SOURCE_DEV" | |
echo "Starting background job to transcode video..." | |
ffmpeg -hide_banner -loglevel error -y -i "${destination}/${title} ${i}.dat" $FFMPEG_OPTS "${destination}/${title} ${i}.mp4" > /dev/null 2>&1 && rm -f "${destination}/${title} ${i}.dat" & | |
pids+=($!) | |
done | |
if [ ${#pids[@]} -ne 0 ]; then | |
echo "Finishing up. Waiting for background transcoding jobs to complete..." | |
for pid in "${pids[@]}"; do | |
wait $pid | |
done | |
fi | |
ls -lA "${destination}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment