Skip to content

Instantly share code, notes, and snippets.

@mwilliammyers
Last active June 6, 2016 07:53
Show Gist options
  • Save mwilliammyers/bb7d820ad3784a113885 to your computer and use it in GitHub Desktop.
Save mwilliammyers/bb7d820ad3784a113885 to your computer and use it in GitHub Desktop.
filebot post processing script for use with transmission and plex
source 'https://rubygems.org'
gem 'streamio-ffmpeg'
GEM
remote: https://rubygems.org/
specs:
multi_json (1.11.2)
streamio-ffmpeg (2.0.0)
multi_json (~> 1.8)
PLATFORMS
ruby
DEPENDENCIES
streamio-ffmpeg
BUNDLED WITH
1.11.2
#!/usr/bin/env ruby
require 'pathname'
require 'ruby-progressbar'
require 'streamio-ffmpeg'
require 'omdbapi'
require 'to_name'
usage = <<-EOS
Usage: #{$PROGRAM_NAME} FILE...
EOS
class String
def namify
self.downcase.gsub(' ', '_')
end
end
# FIXME: use composition instead
class Media < FFMPEG::Movie
attr_accessor :metadata
def initialize path
info = ToName.to_name(path)
# puts info.episode
@metadata = info.series ?
OMDB.title(info.name, { :season => info.series, :episode => info.episode }) :
OMDB.title(info.name, year: info.year)
@metadata[:name] = info.name
@desired_video_codec = desired_video_codec
@desired_audio_codec = desired_audio_codec
@desired_extension = '.m4v'
super(path)
end
def transcode output_file = Pathname(@path).sub_ext(@desired_extension)
options = { video_codec: @desired_video_codec, audio_codec: @desired_audio_codec }
progressbar = ProgressBar.create(:total => 1.01, :title => 'transcode')
super(output_file, options) { |progress| progressbar.progress = progress }
end
def compatible? container = @container
container.include?('mov') || container.include?('mp4')
end
def new_path meta = @metadata
# FIXME: are these the pre or post transcode codecs?
p = meta.type == 'episode' ?
File.join(meta.name, "#{meta.title}.#{meta.year}.s#{meta.season}e#{meta.episode}.#{@audio_codec}.#{@video_codec}.#{@height}p#{@desired_extension}").namify :
"#{meta.title}.#{meta.year}.#{@audio_codec}.#{@video_codec}.#{@height}p#{@desired_extension}".namify
type_dir = meta.type == 'movie' ? 'movies' : 'tv'
File.join('/Volumes', 'Media', 'plex', "#{type_dir}", p)
end
private
def desired_video_codec video_codec = @video_codec
(video_codec == 'h264' || video_codec == 'h265' || video_codec == 'hevc') ? 'copy' : 'h264'
end
def desired_audio_codec audio_codec = @audio_codec
(audio_codec == 'aac' || audio_codec == 'ac3') ? 'copy' : 'aac'
end
end
def parse_args
# TODO allow processing of multiple files at once...
if ARGV[0]
File.realpath(ARGV[0])
elsif ENV['TR_TORRENT_DIR'] && ENV['TR_TORRENT_NAME']
# Transmission sets these
dir = ENV['TR_TORRENT_DIR']
file_name = ENV['TR_TORRENT_NAME']
File.realpath(File.join(dir, file_name))
else
abort(usage)
end
end
##### MAIN #####
media = Media.new(parse_args)
media.transcode unless media.compatible?
require 'pp'
# puts PP.pp(media.metadata)
File.rename(media.path, media.new_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment