Added some new video helper scripts
authorAlan J. Pippin <alan@pippins.net>
Fri, 5 Aug 2016 05:39:46 +0000 (23:39 -0600)
committerAlan J. Pippin <ajp@pippins.net>
Fri, 5 Aug 2016 05:39:46 +0000 (23:39 -0600)
make_mobile_movie [new file with mode: 0755]
organize_movies [new file with mode: 0755]
save_movie [new file with mode: 0755]

diff --git a/make_mobile_movie b/make_mobile_movie
new file mode 100755 (executable)
index 0000000..e4d2331
--- /dev/null
@@ -0,0 +1,48 @@
+#!/bin/bash
+
+DROPBOX="/naspool/dropbox/New Movies"
+MOVIEDIR="/naspool/movies"
+MOVIE="$1"
+CATEGORY="$2"
+
+if [[ -z "$MOVIE" ]]; then
+  echo "Usage: $0 <movie.mkv|Children|Collection|Holiday|Family|Church|Mobile> <Children|Collection|Holiday|Family|Church|Mobile>"
+  exit 1
+fi
+
+if [[ "$MOVIE" == *mkv ]] && [[ "$MOVIE" != /* ]]; then
+  MOVIE="$PWD/$MOVIE"
+fi
+
+if [[ "$MOVIE" == *mp4 ]] && [[ "$MOVIE" != /* ]]; then
+  MOVIE="$PWD/$MOVIE"
+fi
+
+function link_movie {
+  MOVIEFILENAME=`basename "$1"`
+  MOVIEFILENAMENOEXT=`basename "${1%.*}"`
+  CATEGORY=$2
+  if [[ ! -e "$MOVIEDIR/Mobile/$CATEGORY/$MOVIEFILENAMENOEXT.mkv" ]]; then
+    echo "-> Linking $1 to $DROPBOX/Mobile/$CATEGORY/$MOVIEFILENAME for mobile version creation"
+    if [[ ! -e "$DROPBOX/Mobile/$CATEGORY/$MOVIEFILENAME" ]]; then
+      ln -s "$1" "$DROPBOX/Mobile/$CATEGORY/$MOVIEFILENAME"
+    fi
+  else
+    echo "-> $MOVIEFILENAMENOEXT already exists under $MOVIEDIR/Mobile/$CATEGORY"
+  fi
+}
+
+if [[ -f "$MOVIE" ]]; then
+  link_movie "$MOVIE" "$CATEGORY"
+elif [[ -d "$MOVIEDIR/$MOVIE" ]]; then
+  CATEGORY=$MOVIE
+  shopt -s nullglob
+  for MOVIE in $MOVIEDIR/$CATEGORY/*.mkv $MOVIEDIR/$CATEGORY/*.avi
+  do
+    link_movie "$MOVIE" "$CATEGORY"
+  done
+else
+  echo "-E- Unable to find movie or category $MOVIE"
+  exit 1
+fi
+
diff --git a/organize_movies b/organize_movies
new file mode 100755 (executable)
index 0000000..6feee99
--- /dev/null
@@ -0,0 +1,99 @@
+#!/usr/bin/perl
+# Author: Alan J. Pippin
+# Description: Find movies from a temporary dropbox location and compress and move them into their final destination.
+
+use File::Copy;
+use File::Basename;
+use Getopt::Std;
+use File::stat;
+use Time::localtime;
+use File::Pid;
+
+####################################################################################################
+# Configuration parameters
+my $movie_src_dir = "/naspool/dropbox/New Movies";
+my $mobile_movie_src_dir = "/naspool/dropbox/New Movies/Mobile";
+my $movie_dst_dir = "/naspool/movies";
+my $mobile_movie_dst_dir = "/naspool/movies/Mobile";
+my $movie_done_dir = "/naspool/dropbox/Encoded";
+my $log = "/var/log/organize/organize_movies.log";
+my $encode_log = "/var/log/organize/organize_movies_encode.log";
+my $minage = 1; #in minutes
+my $now = time;
+my $quality = 18;
+my $mailto = "admin\@pippins.net";
+
+####################################################################################################
+# Find the movies to encode
+
+# Only one instance of this script should be running at a time
+my $pidfile = File::Pid->new({file => "$pid_file", pid => "$$"});
+exit if $pidfile->running();
+$pidfile->write();
+
+# Encode the movies
+encode_movies($mobile_movie_src_dir, $mobile_movie_dst_dir);
+encode_movies($movie_src_dir, $movie_dst_dir);
+
+sub encode_movies {
+    my ($movie_src_dir, $movie_dst_dir) = @_;
+    
+    # Search the $movie_src_dirs for movies to encode
+    $date = `date`; print "$date";
+    print "-> Searching $movie_src_dir for movies to encode to $movie_dst_dir\n";
+    opendir(SRC_DIR, $movie_src_dir) || die "-E- Unable to open $movie_src_dir\n";
+    while(my $category = readdir(SRC_DIR)) {
+       next if ! -d "$movie_src_dir/$category";
+       next if $category =~ /^\.+$/;
+       print "-> Found category $category\n";
+       opendir(CAT_DIR,"$movie_src_dir/$category") || die "-E- Unable to open $movie_src_dir/$category\n";
+       while(my $filename = readdir(CAT_DIR)) {
+           next if $filename =~ /^\.+$/;
+           next if $filename =~ /\.FAILED$/;
+           my $movie = "$movie_src_dir/$category/$filename";
+           next if ((! -f $movie) && (! -l $movie));
+           next if -e "$movie.FAILED";
+           my $stat = stat("$movie");
+           if($stat->mtime < ($now - $minage*60)) {
+               my ($base, $dir, $ext) = fileparse($movie,'\..*');
+               my $output = "$movie_dst_dir/$category/$base.mkv";
+               print "-> Encoding $movie => $output\n";
+               my $date = `date`; chomp $date;
+               open LOG, ">>$log" || die "-E- Unable to open logfile $log\n";
+               print LOG "$date: started $movie => $output\n";
+               my $cmd = "HandBrakeCLI";
+               $cmd .= " -i \"$movie\" -o \"$output\"";
+               $cmd .= " -f mkv --loose-anamorphic --denoise=\"weak\" -e x264";
+               $cmd .= " -a 1,1";
+               $cmd .= " -x b-adapt=2:rc-lookahead=50 -v 2";
+               if($category =~ /Mobile/i || $movie_src_dir =~ /Mobile/i || $movie_dst_dir =~ /Mobile/i) {
+                   $cmd .= " --preset=\"Android Tablet\" -2 -B 256";
+               } else {
+                   $cmd .= " -q $quality";
+                   $cmd .= " -6 6ch";
+                   $cmd .= " -E copy:ac3";
+                   $cmd .= " --preset=\"High Profile\"";
+               }
+               print "   $cmd\n";
+               my $errno = system("$cmd > $encode_log 2>&1");
+               if($errno == 0) {
+                   if(-l "$movie") {
+                       unlink "$movie";
+                   } else {
+                       system("mv \"$movie\" \"$movie_done_dir\"");
+                   }
+               } else {
+                   system("mailx $mailto -s \"Encoding failed: $movie\" < $encode_log");
+                   system("touch \"$movie.FAILED\"");
+               }
+               $date = `date`; chomp $date;
+               print LOG "$date: finished $movie => $output\n";
+               close LOG;
+           }
+       }
+       closedir(CAT_DIR);
+    }
+    closedir(SRC_DIR);
+}
+
+####################################################################################################
diff --git a/save_movie b/save_movie
new file mode 100755 (executable)
index 0000000..ce92e50
--- /dev/null
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+DROPBOX="/naspool/dropbox/New Movies"
+ENCODED="/naspool/dropbox/Encoded"
+MOVIE="$1"
+CATEGORY=$2
+
+if [[ -z $MOVIE ]] || [[ -z $CATEGORY ]]; then
+  echo "Usage: $0 <movie.mkv> <Children|Collection|Holiday|Family|Church|Mobile>"
+  exit 1
+fi
+
+if [[ ! -e "$MOVIE" ]]; then
+  echo "-E- Bad movie name: $MOVIE"
+  exit 1
+fi
+
+if [[ ! -d "$DROPBOX/$CATEGORY" ]] ; then
+  echo "-E- Unsupported category: $CATEGORY"
+  exit 1
+fi
+
+echo "-> Saving movie \"$MOVIE\" to our movie library: $CATEGORY"
+mv "$MOVIE" "$DROPBOX/$CATEGORY"
+