Bash and Compact Disc(CD) backups

 On the previous post, I went over how to automate copying floppy disks with only providing a Title (identifier) for the image created.

This script is smiliar, but uses HandBrakeCLI to query the disc in the drive for the Chapters/Titles.  In this case, we are only querying the chapters in the Title 0 on the disc.  HandBrake has a GUI to do this manually.

        
#!/bin/bash

NAME="UNKNOWN";

function wait_for_cd_dvd_mount() {
    # Query the CD/DVD Drive to see if the disc is detected
    blkid /dev/sr0 2&>1;
    last_run=$?;
    
    # If blkid's exit code is not 0(Zero), then the disc detected
    while [[ $last_run -ne 0 ]]; do
        echo "Device not mounted. Waiting...";
        sleep 1;
        blkid /dev/sr0;
        last_run=$?;
    done
}

function extract_videos() {
    # Query all CD/DVD Titles via HandBrakeCLI  (-t 0 is to scan all titles)
    for TITLE in $(HandBrakeCLI -i /dev/sr0 --no-dvdnav -t 0 2>&1 | grep title | grep -vE "]|subtitle" | awk '{print $3}' | tr -d ":"); do
        echo "NAME: $1 Title: $TITLE";
        # Extract a single Title and convert it to an MP4 video
        HandBrakeCLI -i /dev/sr0 --no-dvdnav -t $TITLE -o "$1-$TITLE.mp4";
        echo "";
        echo "";
    done
}

while /bin/true; do
   read -p "Insert new CD, Type in CD title and press enter. [CTRL+C to exit.]" NAME
   wait_for_cd_mount;
   extract_videos "$NAME";
   sudo eject /dev/sr0;
done
        
    

References:



As always, if you have any questions, just leave a comment.

Comments

Popular posts from this blog

Bash, here-document and Python

Linux and Logitech Unify Dongles: Part 2

Linux and Logitech Unify Dongles