Skip to content

Instantly share code, notes, and snippets.

@lwfitzgerald
Last active October 6, 2015 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lwfitzgerald/3068578 to your computer and use it in GitHub Desktop.
Save lwfitzgerald/3068578 to your computer and use it in GitHub Desktop.
Updated mencoder hook for Subsonic
#!/usr/bin/perl
# mencoder_hook.pl
# Perl hook script for using mencoder to encode FLV with Subsonic
# Renders subtitles if they exist in the same dir and with the same name as
# the video and end in either .ass or .str
# Usage:
# Install mencoder (apt-get install mencoder)
# Paste this script into mencoder_hook.pl in /var/subsonic/transcode/
# Create a link to mencoder: ln -s /usr/bin/mencoder /var/subsonic/transcode/mencoder
# Create the folder /var/subsonic/pipes
# Go to the "Transcoding" config in Subsonic
# Edit "mp4 > flv" (and any other video->flv encoders that you want)
# Set "Step 1" to:
# mencoder_hook.pl %o %s %bk %w %h
# Enjoy!
# If you are having trouble with video/audio sync issues, try adding "-mc 1", e.g.:
# mencoder_hook.pl %o %s %bk %w %h -mc 1
# If you are getting the wrong audio track, use "-aid #", where # is the audio track number (they start at 1), e.g.:
# mencoder_hook.pl %o %s %bk %w %h -aid 2
use strict;
use POSIX qw(mkfifo);
my ($start_offset, $file, $bitrate, $width, $height, @user_args) = @ARGV;
$bitrate =~ s/k$//;
my $pipe = sprintf("/var/subsonic/pipes/%04d.mencoder", int(rand(10000)));
mkfifo( $pipe, 0770 ) or die "Couldn't make pipe";
my @args = (
$file,
"-ss", $start_offset,
"-of", "lavf",
"-ovc", "x264",
"-x264encopts", "preset=superfast:bitrate=" . $bitrate,
"-oac", "mp3lame",
"-lameopts", "abr:br=96",
"-srate", "44100",
"-af", "lavcresample=44100",
"-lavfopts", "format=flv",
"-subpos", "100",
"-subfont-autoscale", "2",
"-subfont-text-scale", "3",
"-subalign", "2",
"-spualign", "-1",
"-spuaa", "4",
"-unicode",
"-utf8",
"-o", $pipe
);
my $foundSubFiles = 0;
foreach my $ext (".ass", ".srt") {
my $subs = $file;
$subs =~ s/\.[^\.]*$/$ext/;
if (-e $subs) {
$foundSubFiles = 1;
push @args, (
"-sub", $subs,
);
}
}
if ($foundSubFiles == 0) {
push @args, (
"-slang", "eng",
);
}
open OLDOUT, ">&", \*STDOUT;
open OLDERR, ">&", \*STDERR;
my $child = fork();
if (!$child) {
# CHILD
$SIG{'TERM'} = sub { syslog("info", "TERMED CHILD"); };
print STDERR "mencoder " . join(" ", map { "\"$_\"" } @args, @user_args) . "\n";
open STDOUT, ">", "/dev/null";
open STDERR, ">", "/dev/null";
exec("mencoder", @args, @user_args);
}
# PARENT
$SIG{'TERM'} = sub {
# Kill mencoder with a SIGKILL so it actually quits
kill 'KILL', $child;
unlink $pipe;
print STDERR "killed prematurely; child was $child\n";
};
# Print the pipe to stdout
open F, "<", $pipe or print STDERR "Pipe is missing!\n";
my $buf;
my $oldout = select(OLDOUT); $| = 1; select($oldout);
while (read F, $buf, 1024) {
print OLDOUT $buf;
}
print STDERR "finished\n";
@mr-bo-jangles
Copy link

When I use this hook, I get a blank screen, but hear audio? is there any way to fix this?

@lwfitzgerald
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment