Class: AppleTvConverter::MediaConverterMacAdapter

Inherits:
MediaConverterAdapter show all
Defined in:
lib/apple_tv_converter/media_converter_mac_adapter.rb

Constant Summary

Constants included from AppleTvConverter

Timer, VERSION

Instance Attribute Summary

Attributes inherited from MediaConverterAdapter

#conversion_options

Instance Method Summary collapse

Methods inherited from MediaConverterAdapter

#clean_up, #download_subtitles, #extract_subtitles, #get_metadata, #initialize, #rename_to_plex_format, #search_subtitles, #transcode

Methods included from AppleTvConverter

atomic_parsley_binary, atomic_parsley_binary=, copy, data_path, get_language_name, is_macosx?, is_windows?, logger, logger=, mp4box_binary, mp4box_binary=

Constructor Details

This class inherits a constructor from AppleTvConverter::MediaConverterAdapter

Instance Method Details

#add_subtitles(media) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/apple_tv_converter/media_converter_mac_adapter.rb', line 3

def add_subtitles(media)
  puts "* Adding external subtitles"

  if has_subtitles?(media)
    list_files(media.original_filename.gsub(File.extname(media.original_filename), '*.srt')).map do |subtitle_filename|
      subtitle_filename =~ /\.(\w{3})\.srt$/i
      language_code = $1 || 'und'

      language_name = AppleTvConverter.get_language_name(language_code)

      command_line = [
        Shellwords.escape(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'SublerCLI'))),
        %Q[-source "#{subtitle_filename}" ],
        %Q[-language "#{language_name}" ],
        %Q[-dest "#{media.converted_filename}"]
      ].join(' ')

      AppleTvConverter.logger.debug "Executing:"
      AppleTvConverter.logger.debug command_line

      printf "  * Adding #{language_name} subtitles"

      if RUBY_VERSION =~ /^1\.8/
        output, error = Open3.popen3(command_line) { |stdin, stdout, stderr| [ stdout.read, stderr.read ] }
        puts error.strip == '' || error =~ /guessed encoding/i ? " [DONE]" : " [ERROR] #{error}"
      else
        output, error, exit_status = Open3.popen3(command_line) { |stdin, stdout, stderr, wait_thr| [ stdout.read, stderr.read, wait_thr.value ] }
        if exit_status.exitstatus == 0
          puts" [DONE]"
        else
          puts " [ERROR]"
          puts command_line
        end
      end
    end
  else
    puts "  * No subtitles found"
  end
end

#add_to_itunes(media) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/apple_tv_converter/media_converter_mac_adapter.rb', line 96

def add_to_itunes(media)
  printf "* Adding to iTunes"

  command_line = [
    'osascript',
    '-e',
    %Q['tell application "iTunes" to set results to (every file track of playlist "Library" whose name equals "#{media.name}")']
  ].join(' ')

  AppleTvConverter.logger.debug "Executing:"
  AppleTvConverter.logger.debug command_line
  output, exit_status = Open3.popen3(command_line) { |stdin, stdout, stderr, wait_thr| [ stdout.read, wait_thr.value ] }

  if output.strip.blank?
    # Blank result means the file isn't in the library
    command_line = [
      'osascript <<EOF',
      'tell application "iTunes"',
      %Q[add POSIX file "#{media.resulting_filename.gsub(/"/, '\\"')}"],
      'end tell',
      'EOF'
    ].join("\n")

    AppleTvConverter.logger.debug "Executing:"
    AppleTvConverter.logger.debug command_line
    output, exit_status = Open3.popen3(command_line) { |stdin, stdout, stderr, wait_thr| [ stdout.read, wait_thr.value ] }

    puts ' [DONE]'
  else
    puts ' [NOT NECESSARY]'
  end

  return true
end

#list_files(ls) ⇒ Object



131
132
133
# File 'lib/apple_tv_converter/media_converter_mac_adapter.rb', line 131

def list_files(ls)
  `ls -1 #{Shellwords.escape(ls).gsub(/\\\*/, '*')} 2>/dev/null`.split("\n")
end

#tag(media) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/apple_tv_converter/media_converter_mac_adapter.rb', line 43

def tag(media)
   = {}

  ['Name'] = media..name
  ['Genre'] = media..genre
  ['Description'] = media..description
  ['Release Date'] = media..release_date
  ['Director'] = media..director
  ['Screenwriters'] = media..screenwriters
  ['Artwork'] = media..artwork
  ['Sort Name'] = media..sort_name
  ['HD Video'] = true if media.hd?
  ['Media Kind'] = media.is_tv_show_episode? ? 'TV Show' : 'Movie'

  if media.is_tv_show_episode?
    ['TV Show'] = media..tv_show
    ['TV Season'] = media..tv_show_season
    ['TV Episode #'] = media..tv_show_episode
    ['TV Network'] = media..tv_network

    ['Sort Album'] = media..sort_album
    ['Sort Album Artist'] = media..sort_album_artist
    ['Sort Composer'] = media..sort_composer
    ['Sort TV Show'] = media..sort_show
  end


   = .map do |key, value|
    value.nil? ? nil : %Q[{#{key}: #{value.to_s.gsub(/"/, '\\"')}}]
  end.compact.join

  command_line = [
    Shellwords.escape(File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'bin', 'SublerCLI'))),
    %Q[-metadata "#{}"],
    %Q[-dest "#{media.converted_filename}"]
  ].join(' ')

  AppleTvConverter.logger.debug "Executing:"
  AppleTvConverter.logger.debug command_line

  printf "* Tagging"

  if RUBY_VERSION =~ /^1\.8/
    output, error = Open3.popen3(command_line) { |stdin, stdout, stderr| [ stdout.read, stderr.read ] }
    puts error.strip == '' ? " [DONE]" : " [ERROR]"
  else
    output, error, exit_status = Open3.popen3(command_line) { |stdin, stdout, stderr, wait_thr| [ stdout.read, stderr.read, wait_thr.value ] }
    puts exit_status.exitstatus == 0 ? " [DONE]" : " [ERROR]"
  end

  return output.strip.empty?
end