Class: Mp4Renamer::Renamer

Inherits:
Object
  • Object
show all
Defined in:
lib/mp4_renamer/renamer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commit = false) ⇒ Renamer

Initialization code

Parameters:

  • commit (Boolean) (defaults to: false)

    the flag to indicate if the operation is to be executed



13
14
15
# File 'lib/mp4_renamer/renamer.rb', line 13

def initialize(commit = false)
  @commit = commit
end

Instance Attribute Details

#commitObject (readonly)

Returns the value of attribute commit.



8
9
10
# File 'lib/mp4_renamer/renamer.rb', line 8

def commit
  @commit
end

Instance Method Details

#rename(filename, sep_string = '_') ⇒ Object

Rename the input mp4 file and append the track time to the end

Examples:

If the running time for the `sample.mp4` is '06:10' minutes

rename("/path/to/sample.mp4")         #=> will rename the file to '/path/to/sample_06.10.mp4'
rename("/path/to/bad-input-file.mp4") #=> will raise error

Parameters:

  • filename (String)

    the input file

  • sep_string (String) (defaults to: '_')

    the separator string to use default to ‘_’ underscore

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mp4_renamer/renamer.rb', line 27

def rename(filename, sep_string = '_')
  raise Errors::InvalidInputFileError unless File.exist?(filename) && File.readable?(filename)
  puts "FYI: input file : #{filename}"
  info = MP4Info.open(filename)

  # e.g. Other useful information that may be used if applicable
  # * SECS - Total seconds, rounded to nearest second
  # * MM   - Minutes
  # * SS   - Leftover seconds
  # * MS   - Leftover milliseconds, rounded to nearest millisecond
  # * TIME - Time in MM:SS, rounded to nearest second
  # Note: info.TIME always available? if not we need to ignore the operation
  # Or just skip the file?
  raise Errors::MetadataNotAvailableError unless info.TIME

  running_time = info.TIME.gsub(":", sep_string)

  base_name = File.basename(filename, '.*') # 'sample01'
  ext_name  = File.extname(filename)        # '.mp4'
  dir_name  = File.dirname(filename)        # '/path/to/this/sample'

  new_name = "#{base_name}#{sep_string}#{running_time}#{ext_name}"
  output_file = [ dir_name, new_name ].join(File::SEPARATOR)

  if rename_once?(output_file) || File.exist?(output_file)
    puts "FYI: output file: #{filename} already exist, no action required!"
  else
    FileUtils.mv filename, output_file if commit
    puts "FYI: output file: #{output_file}"
  end
  output_file
end

#rename_once?(filename) ⇒ Boolean

Return true if the file has already been renamed

Examples:

rename_once?("some_file.mp4")             == false
rename_once?("some_file_12_34.mp4")       == false
rename_once?("some_file_12_34_12_34.mp4") == true

Parameters:

  • filename (String)

    the input file name

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/mp4_renamer/renamer.rb', line 66

def rename_once?(filename)
  basename = File.basename(filename, File.extname(filename))
  basename.match(/^(.*)_(\d+)_(\d+)_\2_\3$/)
end