diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2022-12-06 23:03:44 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2022-12-07 09:41:16 -0700 |
commit | 13f08e9d36de02c54283993d2bd035adf13f0411 (patch) | |
tree | 8270a1dfe4916b9e70e173f4b2957d86e56e9e12 /scripts/media | |
parent | e27b9fcc8e89e847a4c7f7f6a80cc15e991dd260 (diff) | |
download | dotfiles-13f08e9d36de02c54283993d2bd035adf13f0411.tar.gz |
bin/: some straightforward tidying up
Diffstat (limited to 'scripts/media')
-rwxr-xr-x | scripts/media/burndir | 57 | ||||
-rwxr-xr-x | scripts/media/copydvd | 55 | ||||
-rwxr-xr-x | scripts/media/encode.sh | 184 | ||||
-rwxr-xr-x | scripts/media/flac2mp3 | 134 |
4 files changed, 430 insertions, 0 deletions
diff --git a/scripts/media/burndir b/scripts/media/burndir new file mode 100755 index 00000000..786845ac --- /dev/null +++ b/scripts/media/burndir @@ -0,0 +1,57 @@ +#!/bin/bash + +#Usage: burndir <dir> <device> + +# http://www.linuxquestions.org/questions/linux-software-2/burning-dvd-and-verifying-no-iso-689438/#post3458795 + +#Make sure we are running as root (otherwise we can't burn later) +if [ `whoami` != "root" ] +then +echo "This script must be run as root." +exit 1 +fi + +DIRPATH="$1" +DEVICE="$2" + +TMPDIR=`mktemp -d` || exit 1 +ISOPIPE="$TMPDIR/pipe.iso" +MKISOERR="$TMPDIR/mkisoerr" + +mkfifo "$ISOPIPE" + +echo "Burning directory..." + +growisofs -dvd-compat -Z "$DEVICE"="$ISOPIPE" & +BURNER=$! +MD5=`mkisofs -r -J -l "$DIRPATH" 2>"$MKISOERR" | tee "$ISOPIPE" | md5sum | egrep -o -i -e "^[0-9a-f]{32}"` + +EXTENTS=`egrep -e "^[0-9]+ extents written " "$MKISOERR" | egrep -o -e "^[0-9]+"` + +wait "$BURNER" + +echo "Wrote $EXTENTS extents with a MD5 hash of $MD5." +echo "Cycling drive tray..." + +eject "$DEVICE" +eject -t "$DEVICE" + +# Arch Linux seems to have problems without a wait +# here; gives dd: opening `/dev/sr1': No medium found otherwise +sleep 15 + +echo "Verifying..." + +DISCMD5=`dd if="$DEVICE" bs=2048 count="$EXTENTS" | md5sum | egrep -o -i -e "^[0-9a-f]{32}"` + +echo "Disc has an MD5 hash of $DISCMD5." + +if [ "$MD5" == "$DISCMD5" ] +then +echo "Disc appears to have burned successfully!" +else +echo "Disc appears to have burned with errors." +fi + +rm -r "$TMPDIR" + diff --git a/scripts/media/copydvd b/scripts/media/copydvd new file mode 100755 index 00000000..6b939c68 --- /dev/null +++ b/scripts/media/copydvd @@ -0,0 +1,55 @@ +#!/bin/bash + +#Usage: copydvd <read> <write> + +#Make sure we are running as root (otherwise we can't burn later) +if [ `whoami` != "root" ] +then +echo "This script must be run as root." +exit 1 +fi + +DEVICE1="$1" +DEVICE2="$2" + +TMPDIR=`mktemp -d` || exit 1 +ISOPIPE="$TMPDIR/pipe.iso" +READERR="$TMPDIR/mkisoerr" + +mkfifo "$ISOPIPE" + +echo "Burning directory..." + +growisofs -dvd-compat -Z "$DEVICE2"="$ISOPIPE" & +BURNER=$! +MD5=`dd if="$DEVICE1" 2>"$READERR" | tee "$ISOPIPE" | md5sum | egrep -o -i -e "^[0-9a-f]{32}"` + +EXTENTS=`egrep -e "^[0-9]+ extents written " "$READERR" | egrep -o -e "^[0-9]+"` + +wait "$BURNER" + +echo "Wrote $EXTENTS extents with a MD5 hash of $MD5." +echo "Cycling drive tray..." + +eject "$DEVICE2" +eject -t "$DEVICE2" + +# Arch Linux seems to have problems without a wait +# here; gives dd: opening `/dev/sr1': No medium found otherwise +sleep 15 + +echo "Verifying..." + +DISCMD5=`dd if="$DEVICE2" bs=2048 count="$EXTENTS" | md5sum | egrep -o -i -e "^[0-9a-f]{32}"` + +echo "Disc has an MD5 hash of $DISCMD5." + +if [ "$MD5" == "$DISCMD5" ] +then +echo "Disc appears to have burned successfully!" +else +echo "Disc appears to have burned with errors." +fi + +rm -r "$TMPDIR" + diff --git a/scripts/media/encode.sh b/scripts/media/encode.sh new file mode 100755 index 00000000..4c72eff5 --- /dev/null +++ b/scripts/media/encode.sh @@ -0,0 +1,184 @@ +#!/bin/bash + +# encode.sh +# +# Copyright (c) 2013 Don Melton +# +# This version published on June 7, 2013. +# +# Re-encode video files in a format suitable for playback on Apple TV, Roku 3, +# iOS, OS X, etc. +# +# Input is assumed to be a single file readable by HandBrakeCLI and mediainfo, +# e.g. just about any .mkv, .avi, .mpg, etc. file. +# +# The script automatically calculates output video bitrate based on input. For +# Blu-ray Disc-quality input that's always 5000 Kbps. For DVD-quality input +# that's always 1800 Kbps. For other files that will vary. +# +# The script also automatically calculates video frame rates and audio channel +# configuration. +# +# If the input contains a VobSub (DVD-style) or PGS (Blu-ray Disc-style) +# subtitle, then it is burned into the video. +# +# Optional frame rate overrides and soft subtitles in .srt format are read +# from separate fixed locations in the `$frame_rates_location` and +# `$subtitles_location` variables defined below. Edit this script to redefine +# them. +# +# If your input file is named "foobar.mkv" then the optional frame rate file +# should be named "foobar.txt". And all it should contain is the frame rate +# number, e.g. "25" followed by a carriage return. +# +# If your input file is named "foobar.mkv" then the optional soft subtitle +# file should be named "foobar.srt". +# +# Output is an MP4 container with H.264 video, AAC audio and possibly AC-3 +# audio if the input has more than two channels. +# +# No scaling or cropping is performed on the output. This is a good thing. +# +# The output .mp4 file and a companion .log file are written to the current +# directory. +# +# This script depends on two separate command line tools: +# +# HandBrakeCLI http://handbrake.fr/ +# mediainfo http://mediainfo.sourceforge.net/ +# +# Make sure both are in your `$PATH` or redefine the variables below. +# +# Usage: +# +# ./encode.sh [input file] +# + +die() { + echo "$program: $1" >&2 + exit ${2:-1} +} + +escape_string() { + echo "$1" | sed "s/'/'\\\''/g;/ /s/^\(.*\)$/'\1'/" +} + +readonly program="$(basename "$0")" + +readonly input="$1" + +if [ ! "$input" ]; then + die 'too few arguments' +fi + +handbrake="HandBrakeCLI" +mediainfo="mediainfo" + +frame_rates_location="/path/to/Frame Rates" +subtitles_location="/path/to/Subtitles" + +# My advice is: do NOT change these HandBrake options. I've encoded over 300 +# Blu-ray Discs, 30 DVDs and numerous other files with these settings and +# they've never let me down. + +handbrake_options="--markers --large-file --encoder x264 --encopts vbv-maxrate=25000:vbv-bufsize=31250:ratetol=inf --crop 0:0:0:0 --strict-anamorphic" + +width="$(mediainfo --Inform='Video;%Width%' "$input")" +height="$(mediainfo --Inform='Video;%Height%' "$input")" + +if (($width > 1280)) || (($height > 720)); then + max_bitrate="5000" +elif (($width > 720)) || (($height > 576)); then + max_bitrate="4000" +else + max_bitrate="1800" +fi + +min_bitrate="$((max_bitrate / 2))" + +bitrate="$(mediainfo --Inform='Video;%BitRate%' "$input")" + +if [ ! "$bitrate" ]; then + bitrate="$(mediainfo --Inform='General;%OverallBitRate%' "$input")" + bitrate="$(((bitrate / 10) * 9))" +fi + +if [ "$bitrate" ]; then + bitrate="$(((bitrate / 5) * 4))" + bitrate="$((bitrate / 1000))" + bitrate="$(((bitrate / 100) * 100))" + + if (($bitrate > $max_bitrate)); then + bitrate="$max_bitrate" + elif (($bitrate < $min_bitrate)); then + bitrate="$min_bitrate" + fi +else + bitrate="$min_bitrate" +fi + +handbrake_options="$handbrake_options --vb $bitrate" + +frame_rate="$(mediainfo --Inform='Video;%FrameRate_Original%' "$input")" + +if [ ! "$frame_rate" ]; then + frame_rate="$(mediainfo --Inform='Video;%FrameRate%' "$input")" +fi + +frame_rate_file="$(basename "$input")" +frame_rate_file="$frame_rates_location/${frame_rate_file%\.[^.]*}.txt" + +if [ -f "$frame_rate_file" ]; then + handbrake_options="$handbrake_options --rate $(cat "$frame_rate_file")" +elif [ "$frame_rate" == '29.970' ]; then + handbrake_options="$handbrake_options --rate 23.976" +else + handbrake_options="$handbrake_options --rate 30 --pfr" +fi + +channels="$(mediainfo --Inform='Audio;%Channels%' "$input" | sed 's/[^0-9].*$//')" + +if (($channels > 2)); then + handbrake_options="$handbrake_options --aencoder ca_aac,copy:ac3" +elif [ "$(mediainfo --Inform='General;%Audio_Format_List%' "$input" | sed 's| /.*||')" == 'AAC' ]; then + handbrake_options="$handbrake_options --aencoder copy:aac" +fi + +if [ "$frame_rate" == '29.970' ]; then + handbrake_options="$handbrake_options --detelecine" +fi + +srt_file="$(basename "$input")" +srt_file="$subtitles_location/${srt_file%\.[^.]*}.srt" + +if [ -f "$srt_file" ]; then + subtitle_format="$(mediainfo --Inform='Text;%Format%' "$input" | sed q)" + + if [ "$subtitle_format" == 'VobSub' ] || [ "$subtitle_format" == 'PGS' ]; then + handbrake_options="$handbrake_options --subtitle 1 --subtitle-burned" + else + tmp="" + + trap '[ "$tmp" ] && rm -rf "$tmp"' 0 + trap '[ "$tmp" ] && rm -rf "$tmp"; exit 1' SIGHUP SIGINT SIGQUIT SIGTERM + + tmp="/tmp/${program}.$$" + mkdir -m 700 "$tmp" || exit 1 + + temporary_srt_file="$tmp/subtitle.srt" + cp "$srt_file" "$temporary_srt_file" || exit 1 + + handbrake_options="$handbrake_options --srt-file $(escape_string "$temporary_srt_file") --srt-codeset UTF-8 --srt-lang eng --srt-default 1" + fi +fi + +output="$(basename "$input")" +output="${output%\.[^.]*}.mp4" + +echo "Encoding: $input" >&2 + +time "$handbrake" \ + $handbrake_options \ + --input "$input" \ + --output "$output" \ + 2>&1 | tee -a "${output}.log" diff --git a/scripts/media/flac2mp3 b/scripts/media/flac2mp3 new file mode 100755 index 00000000..23d5a170 --- /dev/null +++ b/scripts/media/flac2mp3 @@ -0,0 +1,134 @@ +#!/usr/bin/perl -w +use strict; + +######################################################### +# Flac to Mp3 Perl Converter +# Created by: Somnorific +# Based on: Scripts by Falkano and Nick Sklaventitis +# Date: June 26, 2008 +# +# Requires: perl, zenity, flac, lame, and rsync +# Tested on: Ubuntu (Hardy) +######################################################### + +# List of default encoding options, add to this list if you want more +my %lameOptions = ( +"320" => ["-b 320 --ignore-tag-errors", ''], +"V0" => ["-V 0 --vbr-new --ignore-tag-errors", ''], +"V2" => ["-V 2 --vbr-new --ignore-tag-errors", ''] +); +my $move_other = 0; + +# Ask for the directory containing the FLAC files +my $dialog_flac = "zenity --file-selection --title 'FLAC directory' --directory"; +my $flac_dir = ''; +if (!($flac_dir = `$dialog_flac`)) { +exit 0; +} +chomp($flac_dir); + +# Ask how the files should be encoded +my $dialog_lame = "zenity --list --text 'LAME Options' --width 400 --height 300 --radiolist --column 'Choose' --column 'Option' TRUE"; +for my $lameOption ( keys %lameOptions ) { +$dialog_lame .= " '" . $lameOption . "' FALSE"; +} +$dialog_lame .= " 'All' FALSE 'Other'"; +my $lame_choice = ''; +if (!($lame_choice = `$dialog_lame`)) { +exit 0; +} +chomp($lame_choice); +if ($lame_choice eq 'Other') { # If other, present user with a text box to manually type in encoding options +my $dialog_other = "zenity --entry --title 'LAME Options' --width 200 --text 'Please enter custom LAME encoder options'"; +if (!($lame_choice = `$dialog_other`)) { +exit 0; +} +chomp($lame_choice); +%lameOptions = (); +%lameOptions = ( 'Custom' => [$lame_choice, ''] ); +} elsif ($lame_choice ne 'All') { # If a specific default encoding type is chosen, save it and remove the others +my @temp = @{$lameOptions{$lame_choice}}; +%lameOptions = (); +%lameOptions = ( $lame_choice => [$temp[0], ''] ); +} +# If neither of the above, the array remains the same as all of the encoding types will be used + +# Ask if you would like to use default folder naming +# Default folder naming places the files in a folder with this structure: "flac_dir (encoding_type)" +my $dialog_type = "zenity --question --title 'MP3 Folder Naming' --text 'Use default folder naming?'"; +`$dialog_type`; +if ($?) { # If not using default, ask the user which directory should be used for whichever encoding they chose +foreach my $lame_option (keys %lameOptions) { +my $dialog_mp3 = "zenity --file-selection --title 'MP3 directory for " . $lame_option . "' --directory"; +my $mp3_dir = ''; +if (!($mp3_dir = `$dialog_mp3`)) { +exit 0; +} +chomp($mp3_dir); +$lameOptions{ $lame_option }[1] = $mp3_dir; +} +} else { # If using default, make the directories using the default naming scheme +foreach my $lame_option (keys %lameOptions) { +my $mp3_dir = $flac_dir . " ($lame_option)"; +`mkdir "$mp3_dir"`; +$lameOptions{ $lame_option }[1] = $mp3_dir; +} +} + +# Ask to move other files (playlists, image art, etc) +my $dialog_move = "zenity --question --title 'Other Files' --text 'Move other non-FLAC files?'"; +`$dialog_move`; +if (!$?) { +$move_other = 1; +} + +# Gather all of the flac files +opendir(DIR, $flac_dir); +my @files = grep(/\.flac$/,readdir(DIR)); +closedir(DIR); + +# Loop through each of the encodings chosen +for my $lame_option ( keys %lameOptions ) { +print "\nEncoding with $lame_option started...\n"; + +# Pull out the directories and flags from the array +my @mp3_arry = @{$lameOptions{$lame_option}}; +my $mp3_dir = $mp3_arry[1]; +my $lame_flags = $mp3_arry[0]; + +# Loop through each of the FLAC files +foreach my $file (@files) { +# Create the destination mp3 filename +my $mp3_filename = `basename "$file" .flac`; +chomp($mp3_filename); +$mp3_filename = $mp3_dir."/".$mp3_filename.".mp3"; + +# Grab the tag info from the FLAC +my %flac_tags = ('TITLE','','ALBUM','','ARTIST','','TRACKNUMBER','','GENRE','','COMMENT','','DATE',''); +for my $tag ( keys %flac_tags ) { +$flac_tags{ $tag } = `metaflac --show-tag=$tag "$flac_dir/$file" | awk -F = '{ printf(\$2) }'`; +} + +# Build the conversion script and do the actual conversion +my $flac_command = "flac -dc \"$flac_dir/$file\" | lame $lame_flags " . +"--tt \"" . $flac_tags{'TITLE'} . "\" " . +"--tl \"" . $flac_tags{'ALBUM'} . "\" " . +"--ta \"" . $flac_tags{'ARTIST'} . "\" " . +"--tn \"" . $flac_tags{'TRACKNUMBER'} . "\" " . +"--tg \"" . $flac_tags{'GENRE'} . "\" " . +"--ty \"" . $flac_tags{'DATE'} . "\" " . +"--add-id3v2 - \"$mp3_filename\" 2>&1"; +`$flac_command`; +} + +print "\nEncoding with $lame_option finished...\n"; + +# Move over any other files using rsync +if ($move_other) { +my $rsync_comm = "rsync -a --exclude \"*.flac\" \"$flac_dir/\" \"$mp3_dir/\""; +`$rsync_comm`; +} +} + +print "\nAll encodings finished, bye...\n"; + |