Class: Mp4Renamer::Renamer
- Inherits:
-
Object
- Object
- Mp4Renamer::Renamer
- Defined in:
- lib/mp4_renamer/renamer.rb
Instance Attribute Summary collapse
-
#commit ⇒ Object
readonly
Returns the value of attribute commit.
Instance Method Summary collapse
-
#initialize(commit = false) ⇒ Renamer
constructor
Initialization code.
-
#rename(filename, sep_string = '_') ⇒ Object
Rename the input mp4 file and append the track time to the end.
-
#rename_once?(filename) ⇒ Boolean
Return true if the file has already been renamed.
Constructor Details
#initialize(commit = false) ⇒ Renamer
Initialization code
13 14 15 |
# File 'lib/mp4_renamer/renamer.rb', line 13 def initialize(commit = false) @commit = commit end |
Instance Attribute Details
#commit ⇒ Object (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
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
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 |