Sunday, May 11, 2014

How to get subtitles using mencoder on Subsonic 4.9

Forum Subsonic : http://forum.subsonic.org/forum/viewtopic.php?f=6&t=6141
darkip GitHub : https://gist.github.com/darkip/3068578#file-mencoder_hook-pl-L1
Installing and configuring mencoder
$sudo apt-get install mencoder
$sudo ln -s /usr/bin/mencoder /var/subsonic/transcode/mencoder
$sudo mkdir /var/subsonic/pipes
$vim /var/subsonic/transcode/mencoder_hook.pl
  #!/usr/bin/perl
  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",
    "-oac", "lavc",
    "-ovc", "lavc",
    "-lavcopts", "vcodec=flv:vbitrate=" . $bitrate ,
    "-oac", "mp3lame",
    "-lameopts", "abr:br=96:vol=4",
    "-srate", "44100",
    "-af", "lavcresample=44100",
    "-lavfopts", "format=flv",
    "-vf", "harddup,scale=$width:-3",
    "-utf8",
    "-o", $pipe
  );
  foreach my $ext (".ass", ".srt") {
    my $subs = $file;
    $subs =~ s/\.[^\.]*$/$ext/;
    if (-e $subs) {
      if ($ext eq ".ass") {
         push @args, (
           "-ass", $subs,
           "-subpos", "95"
         );
      } else {
         push @args, (
           "-sub", $subs,
           "-subpos", "95"
         );
      }
    }
  }
  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";
and then go to your website's "Transcoding" config in Subsonic.
Delete flv/h264 video and edit
"video > flv"
"avi mpg mpeg mp4 m4v mkv mov wmv ogv divx m2ts rm rmvb"
"flv"
"mencoder_hook.pl %o %s %bk %w %h"
Finish!

No comments:

Post a Comment