apple scripts handbrake-auto

Discussion in 'Mac Programming' started by bob5731, Jan 3, 2018.

  1. bob5731, Jan 3, 2018
    Last edited: Jan 3, 2018

    bob5731 macrumors regular

    Joined:
    Jun 27, 2008
    #1
    handbrake auto

    I'm ripping dvd to my NAS then win does move to the NAS
    How do i fix it?
    How I add code to move it to my NAS?

    https://github.com/rfletcher/handbrake-auto

    code is =
    tell application "/Applications/HandBrake.app"

    set scriptname to "/Users/bob/Desktop/handbrake-auto-master/handbrake.rb"

    set scriptlog to "~/Library/Logs/" & scriptname & ".log"


    do shell script "echo 'starting up...' >>" & scriptlog

    do shell script "~/usr/local/bin" & scriptname & " &>>" & scriptlog & " &"

    end tell

    Script Error
    HandBrake got an error: sh: /Users/bob/Users/bob/Library/Logs/Users/bob/Desktop/handbrake-auto-master/handbrake.rb.log: No such file or directory

    Wed Jan 3 16:23:33 CST 2018
    starting up...
     
  2. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #2
    This line...
    Is producing the bad file path in this error...
    As for the rest of the question, I try not to use Applescript for the bulk of my logic. It's a lot easier in Ruby or Python. But to do it, you are going to need to find something that lets you mount the network drive (if it isn't already mounted), find it's path, and then issue a copy command to copy the file to the destination.

    I've got my own scripts, but they don't do any sort of auto-copying for me like you are trying to do. My end goals are usually to handle embedding of chapter and iTunes metadata for me in a more automated way.
     
  3. bob5731, Jan 3, 2018
    Last edited: Jan 3, 2018

    bob5731 thread starter macrumors regular

    Joined:
    Jun 27, 2008
    #3
    I have 1000 dvd that I'm copying to my mac the to the NAS.
    Applescript or automator but I do not know how to do it.

    code is =
    tell application "/Applications/HandBrake.app"

    set scriptname to "handbrake.rb"

    set scriptlog to "~/MAC HD/Library/Logs/" & scriptname & ".log"


    do shell script "date >>" & scriptlog

    do shell script "echo 'starting up...' >>" & scriptlog


    do shell script "~/bin/" & scriptname & " &>>" & scriptlog & " &"

    do shell script "~/usr/local/bin" & scriptname & " &>>" & scriptlog & " &"

    end tell


    Script Error
    HandBrake got an error: date: illegal time format
    usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
    [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

    code is in Ruby =
    require "enumerator"
    mode = "auto" # auto | movie | tv
    # config
    $handbrake_bin = "~/bin/handbrake"
    $input_device = "/dev/disk1"
    $output_dir = "~/Movies"
    $output_format = "m4v"
    # greatest allowed pct. difference between two longest titles on a tv disc
    $max_tv_title_differential = 0.1
    $min_tv_duration = 540 # 20 minutes
    $handbrake_opts = [
    "-i " + $input_device,
    "--preset Default",
    "--native-language eng",
    ]
    def eject_dvd
    system("drutil eject")
    $?.exitstatus == 0
    end
    def get_dvd_label
    `/sbin/mount | /usr/bin/grep #{$input_device}`.gsub(/.*(\/Volumes\/.*) \(.*/, '\1').chop().split('/').last()
    end
    def get_rip_mode
    durations = get_titles.collect { |title|
    title[:duration]
    }.sort.reverse
    if durations.length > 1 && (1 - (durations[1].to_f / durations[0].to_f)) <= $max_tv_title_differential
    mode = "tv"
    else
    mode = "movie"
    end
    end
    def get_episode_titles
    get_titles().find_all { |title|
    title[:duration] > $min_tv_duration
    }
    end
    def get_output_file
    index = 0
    begin
    output_file = File.expand_path($output_dir) + "/" + get_dvd_label() + "-" + (index += 1).to_s + "." + $output_format
    end while File.exists?(output_file)
    output_file
    end
    def get_titles
    titles = []
    title = nil
    # `cat ~/tv.txt`.each_line do |line|
    rip(["--title 0"], true).each_line do |line|
    if title && line.match(/^[^\s]/)
    titles << title
    title = nil
    end
    if matches = line.match(/^\+\s+title\s+(\d+)/)
    title = { :number => matches[1].to_i }
    elsif matches = line.match(/\s+duration:\s([\d:]+)/):
    title[:duration] = parse_duration matches[1]
    end
    end
    titles
    end
    def parse_duration(str)
    values = [
    { :s => 1 },
    { :m => 60 },
    { :h => 3600 },
    { :d => 86400 }
    ]
    str.split(':').reverse.to_enum:)each_with_index).collect do |chunk, index|
    chunk.to_i * values[index].values.first
    end.inject do |sum, seconds|
    sum + seconds
    end
    end
    def rip(opts = [], return_output = false)
    command = [
    "/opt/local/bin/nice -n 20",
    File.expand_path($handbrake_bin),
    $handbrake_opts,
    opts
    ]
    command = command.flatten.join(' ')
    puts "Running: #{command}"
    if return_output
    `#{command} 2>&1`
    else
    system(command)
    end
    end
    begin
    if mode == "auto"
    mode = get_rip_mode
    end
    if mode == "movie"
    rip [
    "--longest",
    "-o \"#{get_output_file()}\""
    ]
    elsif mode == "tv"
    get_episode_titles.each do |title|
    rip [
    "--title #{title[:number]}",
    "-o \"#{get_output_file()}\""
    ]
    end
    end
    rescue
    nil
    end
    eject_dvd()
     
  4. kryten2, Jan 7, 2018
    Last edited: Jan 7, 2018

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Please post the log file. What happens when you run the Ruby script in Terminal. The Running: lines might be useful for problem solving.

    Example :
    Code:
    Johns-Mac:~ kryten$ ruby bin/handbrake.rb
    Running: /Users/kryten/bin/HandBrakeCLI -i /dev/disk3 --preset Normal --native-language eng --title 0
    Running: /Users/kryten/bin/HandBrakeCLI -i /dev/disk3 --preset Normal --native-language eng -o "/Users/kryten/Movies/Blade Runner.m4v"
    [00:06:08] hb_init: starting libhb thread
    [00:06:08] thread 700000081000 started ("libhb")
    HandBrake 1.0.7 (2017040900) - Darwin x86_64 - https://handbrake.fr
    2 CPUs detected
    Info : https://forums.macrumors.com/thread...-backup-encoding-tagging-for-mac-os-x.805573/
     
  5. bob5731, Jan 8, 2018
    Last edited: Jan 8, 2018

    bob5731 thread starter macrumors regular

    Joined:
    Jun 27, 2008
    #5
    I got the code from github.

    libdvdcss.pkg will not insatll.

    /usr/local/bin/handbrake.rb:11: syntax error, unexpected '~', expecting end-of-input

    $output_dir = "~/Users/bob/Movies/Video”
     
  6. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #6
    I've used the files from github for testing on El Capitan 10.11.6.
    Change this :
    Code:
    $output_dir = "~/Users/bob/Movies/Video”
    to this:
    Code:
    $output_dir = "~/Movies/Video”
    Try again.
     
  7. bob5731, Jan 8, 2018
    Last edited: Jan 8, 2018

    bob5731 thread starter macrumors regular

    Joined:
    Jun 27, 2008
    #7
    /Users/bob/Desktop/handbrake-auto-master/handbrake.rb:9: unknown regexp options - lcal

    /Users/bob/Desktop/handbrake-auto-master/handbrake.rb:9: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('

    /Users/bob/Desktop/handbrake-auto-master/handbrake.rb:14: syntax error, unexpected tIDENTIFIER, expecting end-of-input

    $output_format = "m4v"
     
  8. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #8
    What version of macOS are you using? You seem to be running two different handbrake.rb scripts.
    Code:
    /usr/local/bin/handbrake.rb
    and
    Code:
    /Users/bob/Desktop/handbrake-auto-master/handbrake.rb
    Stick to one. Please attach the script to your post. You can also attach a screenshot of your Terminal app when you run the script.
     
  9. bob5731, Jan 8, 2018
    Last edited: Jan 8, 2018

    bob5731 thread starter macrumors regular

    Joined:
    Jun 27, 2008
    #9
    Os x 10.11.06

    I do TI work. But I can not program a mac.

    Code:
    #!/usr/bin/ruby
    
    require "enumerator"
    
    mode = "auto" # auto | movie | tv
    
    # config
    
    $handbrake_bin = “~/usr/local/binhandbrake"
    $input_device = “/dev/disk2”
    
    
    $output_dir = “~/Movies/Video”
    $output_format = "m4v"
    # greatest allowed pct. difference between two longest titles on a tv disc
    $max_tv_title_differential = 0.1
    $min_tv_duration = 540 # 20 minutes
    
    $handbrake_opts = [
      "-i " + $input_device,
      "--preset Default",
      "--native-language eng",
    ]
    
    def eject_dvd
      system("drutil eject")
      $?.exitstatus == 0
    end
    
    def get_dvd_label
      `/sbin/mount | /usr/bin/grep #{$input_device}`.gsub(/.*(\/Volumes\/.*) \(.*/, '\1').chop().split('/').last()
    end
    
    def get_rip_mode
      durations = get_titles.collect { |title|
        title[:duration]
      }.sort.reverse
    
      if durations.length > 1 && (1 - (durations[1].to_f / durations[0].to_f)) <= $max_tv_title_differential
        mode = "tv"
      else
        mode = "movie"
      end
    end
    
    def get_episode_titles
      get_titles().find_all { |title|
        title[:duration] > $min_tv_duration
      }
    end
    
    def get_output_file
      index = 0
      begin
        output_file = File.expand_path($output_dir) + "/" + get_dvd_label() + "-" + (index += 1).to_s + "." + $output_format
      end while File.exists?(output_file)
      output_file
    end
    
    def get_titles
      titles = []
      title = nil
    
      # `cat ~/tv.txt`.each_line do |line|
      rip(["--title 0"], true).each_line do |line|
        if title && line.match(/^[^\s]/)
          titles << title
          title = nil
        end
    
        if matches = line.match(/^\+\s+title\s+(\d+)/)
          title = { :number => matches[1].to_i }
        elsif matches = line.match(/\s+duration:\s([\d:]+)/):
          title[:duration] = parse_duration matches[1]
        end
      end
    
      titles
    end
    
    def parse_duration(str)
      values = [
        { :s => 1 },
        { :m => 60 },
        { :h => 3600 },
        { :d => 86400 }
      ]
    
      str.split(':').reverse.to_enum(:each_with_index).collect do |chunk, index|
        chunk.to_i * values[index].values.first
      end.inject do |sum, seconds|
        sum + seconds
      end
    end
    
    def rip(opts = [], return_output = false)
      command = [
        "/opt/local/bin/nice -n 20",
        File.expand_path($handbrake_bin),
        $handbrake_opts,
        opts
      ]
    
      command = command.flatten.join(' ')
    
      puts "Running: #{command}"
    
      if return_output
        `#{command} 2>&1`
      else
        system(command)
      end
    end
    
    
    begin
      if mode == "auto"
        mode = get_rip_mode
      end
    
      if mode == "movie"
        rip [
          "--longest",
          "-o \"#{get_output_file()}\""
        ]
      elsif mode == "tv"
        get_episode_titles.each do |title|
          rip [
            "--title #{title[:number]}",
            "-o \"#{get_output_file()}\""
          ]
        end
      end
    rescue
      nil
    end
    
    eject_dvd()
     

    Attached Files:

  10. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
    Might be a typo but this can't be right. Do those folders really exist? I'm guessing from your previous post that you log on as user bob so this :

    Code:
    $handbrake_bin = “~/usr/local/binhandbrake"
    translates to :
    Code:
    $handbrake_bin = “/Users/bob/usr/local/binhandbrake"
    There's a missing forward slash as well. So change to :
    Code:
    $handbrake_bin = "/usr/local/bin/handbrake"
    I just noticed that there's a mixture of smart quotes and normal quotes. Everything needs to be in normal quotes eg ""
    Can you spot the difference?
    Code:
    $handbrake_bin = “~/usr/local/binhandbrake"
    $input_device = “/dev/disk2”
    $output_dir = “~/Movies/Video”
    Please post the ouput when you run this in Terminal :

    Code:
    ls -al /Users/bob/Desktop/handbrake-auto-master/handbrake.rb
    Run the Ruby script like this :
    Code:
    ruby /Users/bob/Desktop/handbrake-auto-master/handbrake.rb
    Forget about the AppleScript part for now.
     
  11. bob5731, Jan 9, 2018
    Last edited: Jan 9, 2018

    bob5731 thread starter macrumors regular

    Joined:
    Jun 27, 2008
    #11
    I'm user bob.

    I have done the code like

    $handbrake_bin = “/Users/bob/usr/local/binhandbrake"
    $input_device = “/dev/disk2”
    $output_dir = “~/Movies/Video”
    $output_format = "m4v"
    and like
    $handbrake_bin = “~/usr/local/binhandbrake"
    $input_device = “/dev/disk2”
    $output_dir = “~/Movies/Video”
    $output_format = "m4v"

    I get the some errors

    Code:
    #!/usr/bin/ruby
    
    require "enumerator"
    
    mode = "auto" # auto | movie | tv
    
    # config
    
    $handbrake_bin = “~/usr/local/binhandbrake"
    $input_device = “/dev/disk2”
    $output_dir = “~/Movies/Video”
    $output_format = "m4v"
    
    # greatest allowed pct. difference between two longest titles on a tv disc
    $max_tv_title_differential = 0.1
    $min_tv_duration = 540 # 20 minutes
    
    $handbrake_opts = [
      "-i " + $input_device,
      "--preset Default",
      "--native-language eng",
    ]
    
    def eject_dvd
      system("drutil eject")
      $?.exitstatus == 0
    end
    
    def get_dvd_label
      `/sbin/mount | /usr/bin/grep #{$input_device}`.gsub(/.*(\/Volumes\/.*) \(.*/, '\1').chop().split('/').last()
    end
    
    def get_rip_mode
      durations = get_titles.collect { |title|
        title[:duration]
      }.sort.reverse
    
      if durations.length > 1 && (1 - (durations[1].to_f / durations[0].to_f)) <= $max_tv_title_differential
        mode = "tv"
      else
        mode = "movie"
      end
    end
    
    def get_episode_titles
      get_titles().find_all { |title|
        title[:duration] > $min_tv_duration
      }
    end
    
    def get_output_file
      index = 0
      begin
        output_file = File.expand_path($output_dir) + "/" + get_dvd_label() + "-" + (index += 1).to_s + "." + $output_format
      end while File.exists?(output_file)
      output_file
    end
    
    def get_titles
      titles = []
      title = nil
    
      # `cat ~/tv.txt`.each_line do |line|
      rip(["--title 0"], true).each_line do |line|
        if title && line.match(/^[^\s]/)
          titles << title
          title = nil
        end
    
        if matches = line.match(/^\+\s+title\s+(\d+)/)
          title = { :number => matches[1].to_i }
        elsif matches = line.match(/\s+duration:\s([\d:]+)/):
          title[:duration] = parse_duration matches[1]
        end
      end
    
      titles
    end
    
    def parse_duration(str)
      values = [
        { :s => 1 },
        { :m => 60 },
        { :h => 3600 },
        { :d => 86400 }
      ]
    
      str.split(':').reverse.to_enum(:each_with_index).collect do |chunk, index|
        chunk.to_i * values[index].values.first
      end.inject do |sum, seconds|
        sum + seconds
      end
    end
    
    def rip(opts = [], return_output = false)
      command = [
        "/opt/local/bin/nice -n 20",
        File.expand_path($handbrake_bin),
        $handbrake_opts,
        opts
      ]
    
      command = command.flatten.join(' ')
    
      puts "Running: #{command}"
    
      if return_output
        `#{command} 2>&1`
      else
        system(command)
      end
    end
    
    
    begin
      if mode == "auto"
        mode = get_rip_mode
      end
    
      if mode == "movie"
        rip [
          "--longest",
          "-o \"#{get_output_file()}\""
        ]
      elsif mode == "tv"
        get_episode_titles.each do |title|
          rip [
            "--title #{title[:number]}",
            "-o \"#{get_output_file()}\""
          ]
        end
      end
    rescue
      nil
    end
    
    eject_dvd()
     

    Attached Files:

  12. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
  13. bob5731, Jan 10, 2018
    Last edited: Jan 10, 2018

    bob5731 thread starter macrumors regular

    Joined:
    Jun 27, 2008
    #13
    Can you fix it for me?

    ^[[ALast login: Wed Jan 10 22:57:39 on ttys000

    iMac:~ bob$ ls -al /Users/bob/Desktop/handbrake-auto-master/handbrake.rb

    -rwxr-xr-x@ 1 bob staff 2644 Jan 10 23:16 /Users/bob/Desktop/handbrake-auto-master/handbrake.rb

    iMac:~ bob$ ruby /Users/bob/Desktop/handbrake-auto-master/handbrake.rb

    /Users/bob/Desktop/handbrake-auto-master/handbrake.rb:10: syntax error, unexpected '~', expecting end-of-input

    $output_dir = "~/Users/bob/Movies/video“

    ^

    iMac:~ bob$
     
  14. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #14
    I've been trying to help you to fix it yourself but you keep posting the same mistakes. If you're having difficulties in understanding the help given please say so. The code snippet below has normal quotes. Please make the necessary changes between the quotes and use in the handbrake.rb script. Good luck.

    Code:
    $handbrake_bin = ""
    $input_device = ""
    $output_dir = ""
    $output_format = ""
    Please reread the posts I made.
     
  15. bob5731, Jan 11, 2018
    Last edited: Jan 11, 2018

    bob5731 thread starter macrumors regular

    Joined:
    Jun 27, 2008
    #15
    I have difficulties in understanding normal quotes and smart quotes. Are talking about
    Single and Double Quotation?

    I fix it up to Movies.
     
  16. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #16
    “ ” = smart quotes
    ¨ ¨ = normal/regular quotes
     
  17. bob5731, Jan 11, 2018
    Last edited: Jan 11, 2018

    bob5731 thread starter macrumors regular

    Joined:
    Jun 27, 2008
    #17
    How I'm get
    Win i run it through rubymine.
    it is ejecting dvd

    Code:
    #!/usr/bin/ruby
    
    require "enumerator"
    
    mode = "auto" # auto | movie | tv
    
    # config
    $handbrake_bin = "~/bin/handbrake"
    $input_device = "/dev/disk2"
    $output_dir = "~/Movies"
    $output_format = "m4v"
    
    # greatest allowed pct. difference between two longest titles on a tv disc
    $max_tv_title_differential = 0.1
    $min_tv_duration = 540 # 20 minutes
    
    $handbrake_opts = [
      "-i " + $input_device,
      "--preset Default",
      "--native-language eng",
    ]
    
    def eject_dvd
      system("drutil eject")
      $?.exitstatus == 0
    end
    
    def get_dvd_label
      `/sbin/mount | /usr/bin/grep #{$input_device}`.gsub(/.*(\/Volumes\/.*) \(.*/, '\1').chop().split('/').last()
    end
    
    def get_rip_mode
      durations = get_titles.collect { |title|
        title[:duration]
      }.sort.reverse
    
      if durations.length > 1 && (1 - (durations[1].to_f / durations[0].to_f)) <= $max_tv_title_differential
        mode = "tv"
      else
        mode = "movie"
      end
    end
    
    def get_episode_titles
      get_titles().find_all { |title|
        title[:duration] > $min_tv_duration
      }
    end
    
    def get_output_file
      index = 0
      begin
        output_file = File.expand_path($output_dir) + "/" + get_dvd_label() + "-" + (index += 1).to_s + "." + $output_format
      end while File.exists?(output_file)
      output_file
    end
    
    def get_titles
      titles = []
      title = nil
    
      # `cat ~/tv.txt`.each_line do |line|
      rip(["--title 0"], true).each_line do |line|
        if title && line.match(/^[^\s]/)
          titles << title
          title = nil
        end
    
        if matches = line.match(/^\+\s+title\s+(\d+)/)
          title = { :number => matches[1].to_i }
        elsif matches = line.match(/\s+duration:\s([\d:]+)/);
            title[:duration] = parse_duration matches[1]
        end
      end
    
      titles
    end
    
    def parse_duration(str)
      values = [
          { :s => 1 },
          { :m => 60 },
          { :h => 3600 },
          { :d => 86400 }
      ]
    
      str.split(':').reverse.to_enum(:each_with_index).collect do |chunk, index|
        chunk.to_i * values[index].values.first
      end.inject do |sum, seconds|
        sum + seconds
      end
    end
    
    def rip(opts = [], return_output = false)
      command = [
          "/opt/local/bin/nice -n 20",
          File.expand_path($handbrake_bin),
          $handbrake_opts,
          opts
      ]
    
      command = command.flatten.join(' ')
    
      puts "Running: #{command}"
    
      if return_output
        `#{command} 2>&1`
      else
        system(command)
      end
    end
    
    
    begin
      if mode == "auto"
        mode = get_rip_mode
      end
    
      if mode == "movie"
        rip [
                "--longest",
                "-o \"#{get_output_file()}\""
            ]
      elsif mode == "tv"
        get_episode_titles.each do |title|
          rip [
                  "--title #{title[:number]}",
                  "-o \"#{get_output_file()}\""
              ]
        end
      end
    rescue
      nil
    end
    
    eject_dvd()
    /usr/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/bob/Desktop/handbrake-auto-master/handbrake.rb
    Running: /opt/local/bin/nice -n 20 /Users/bob/bin/handbrake -i /dev/disk2 --preset Default --native-language eng --title 0
    Running: /opt/local/bin/nice -n 20 /Users/bob/bin/handbrake -i /dev/disk2 --preset Default --native-language eng --longest -o "/Users/bob/Movies/FLIGHT_OF_THE_NAVIGATOR-1.m4v"
    sh: /opt/local/bin/nice: No such file or directory

    Process finished with exit code 0
     

    Attached Files:

  18. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #18
    Make a fresh start with the script from github. Don't copy paste as that makes things even worse.
     
  19. bob5731, Jan 11, 2018
    Last edited: Jan 11, 2018

    bob5731 thread starter macrumors regular

    Joined:
    Jun 27, 2008
    #19
    elsif matches = line.match(/\s+duration:\s([\d:]+)/): need to be a ;

    then the code will eject dvd.

    I'm ripping dvd the
    Eject dvd then ripping dvd
    In m4v format.
     

Share This Page