Sharing some of my scripts

Discussion in 'Tech Talk' started by Kris, Jan 20, 2012.

  1. Kris Rookie

    These are some short scripts I wrote in BASH that you can use on your machines (or modify to work on whatever machine you are working on).

    ## Batch convert FRAPS encoded AVI to MKV ##
    Code:
    #!/bin/bash
    echo -e '\E[37;43m'"\033[1m$(echo -e "Script started on $(date)")\033[0m"
    find -name "* *" -type f | rename 's/ /_/g'
    for file in *.avi ; do
        if [ -f $file.audiorip.ac3 ];
            then
                echo -e '\E[37;44m'"\033[1m$(echo $file.audiorip.ac3 already exists, skipping...)\033[0m"
            else
                ffmpeg -i $file -acodec ac3 -ab 192000 $file.audiorip.ac3
        fi
        if [ -f $file.videorip.mkv ];
            then
                echo -e '\E[37;44m'"\033[1m$(echo $file.videorip.mkv already exists, skipping...)\033[0m"
            else
                ## Change x264 option -crf to 0 for lossless video quality (in exchange for compression value)
                ffmpeg -i $file -vcodec rawvideo -f yuv4mpegpipe -an -pix_fmt yuv420p - | x264 - --stdin y4m --crf 19 --bframes 5 --b-adapt 2 --ref 4 --mixed-refs --no-fast-pskip --direct auto --deblock -3:-3 --subme 10 --trellis 2 --analyse all --8x8dct --me umh --output $file.videorip.mkv
        fi
        if [ -f $file.mkv ];
            then
                echo -e '\E[37;44m'"\033[1m$(echo $file.mkv already exists, skipping...)\033[0m"
            else
                mkvmerge -o $file.mkv --forced-track 1:no -d 1 -A -S $file.videorip.mkv --forced-track 0:no -a 0 -D -S $file.audiorip.ac3 --track-order 0:1,1:0
        fi
        echo -e '\E[37;43m'"\033[1m$(echo -e "Video file $file finished on $(date)")\033[0m"
    done
    mkdir completed
    for file in *.avi.mkv ; do mv $file completed/`echo $file | sed 's/\(.*\.\)avi\.mkv/\1mkv/'` ; done
    echo -e '\E[37;42m'"\033[1m$(echo -e "Script has completed.\nPlease confirm .mkv files were correctly muxed. You should delete originals and clean up the work files")\033[0m"
    echo -e '\E[37;43m'"\033[1m$(echo -e "Script ended on $(date)")\033[0m"
    
    Takes a video recorded with FRAPS and rips both the video and audio tracks then merges them with mkvmerge.
    Required tools: ffmpeg, x264, mkvtoolnix (includes mkvmerge)

    ## Batch BMP to JPG ##
    Code:
    #!/bin/bash
    # This script runs through sub-directories recursively twice to complete two tasks
    # First, it mogrifies (Imagemagick) every bmp to jpg at 100% quality
    # Second, it sends the original .bmp files to a directory called please_remove (this prevents any accidental deletion of files in case imagemagick goofs up or something)
    ## These commands produce error "find: missing argument to `-exec'" when this script is ran with file extention '.sh'
    find ./ -name "*.bmp" -exec /usr/bin/mogrify -verbose -format jpg -quality 100% {} \;
    mkdir please_remove
    find ./ -name "*.bmp" -exec /bin/mv -v {} please_remove/ \;
    
    Required tools: imagemagick

    ## Batch Youtube Playlist Grabber ##
    Code:
    #!/bin/bash
    ## Quick usage explanation 'youtube-playlist-grabber playlistID'
    ## An example of a playlistID is 'AE85DE8440AA6B83' and can be found on any youtube playlist URL (https://www.youtube.com/view_play_list?p=AE85DE8440AA6B83)
    ## But that doesn't appease to me, of course. So here is an extra bonus.
    ## 'https://gdata.youtube.com/feeds/api/users/thenewboston/playlists?v=2&max-results=50&start-index=1'
    ## Replace 'thenewboston' with any youtube channel name you want, and it will list all of the playlists for you to grab the playlistID of.
    ## Oh, and just because I know someone is going to wonder about it, I don't actually know why gdata.youtube.com API limits the maximum results to 50 (thats the highest value you can use for max-results, which otherwise defaults to 10 [a"suggested" value for any developers {hur hurr}]). And if it occurs to you, yes, thats the only reason I had to go through the trouble of a while script (extra burden, made it fun though).
    
    playlist=$*
    sIndex=1
    vidCount=50
    
    title=$(curl "https://gdata.youtube.com/feeds/api/playlists/$playlist?v=2&max-results=0" | grep -o "<title>.*</title>" | cut -d\> -f2 | cut -d\< -f1)
    mkdir "$title"
    
    while [ $vidCount == 50 ]
    do
        vidList=$(curl "https://gdata.youtube.com/feeds/api/playlists/$playlist?v=2&max-results=50&start-index=$sIndex" | grep -o "href='https://www.youtube.com/watch?v=...........&" | cut -d\= -f1,2 --complement | cut -d\& -f1)
        vidCount=$(printf '%s\n' "$vidList" | wc -l)
        sIndex=$(($sIndex + $vidCount))
    
        printf '%s\n' "$vidList" >> "$title/ytgetlist.txt"
    done
    
    echo "$title contains $(($sIndex - 1)) videos"
    
    exit
    
    This will grab all of the video IDs for everything listed in the playlist and place them in a text file inside of a directory named after the playlist.
    The intended use is that after you have ran this script, you change to the directory just made and run youtube-dl with the batch file option. Example 'youtube-dl -ta ytgetlist.txt'
    Required tools: curl, grep, cut (yadda yadda)
    youtube-dl requires python.

    I did not make these for anyone but myself, so if they seem un-professional or un-organized or in-efficient, it's probably because I'm still a novice at scripting and had not originally intended to share these.

    Feedback or ask for more details if you wish.

Share This Page