video_to_mp3 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,12 @@
1
+ Note
2
+ ------------
3
+
4
+ "VideoToMp3.new.connvert_to_mp3" to mp3 method would be same as convert method which is there in this release as I have made this a generic method which also gives you the fexibility to specify your own ffmpeg conversion options
5
+
1
6
  Video to mp3
2
7
  =========
3
8
 
4
- This is a video to mp3 convertor. iT converts your video files into mp3
9
+ This is a video to mp3 convertor. It converts your video files into mp3
5
10
 
6
11
  Requirements
7
12
  ------------
@@ -23,12 +28,44 @@ Quick Start
23
28
 
24
29
  In your code
25
30
 
26
- VideoToMp3.new.connvert_to_mp3("\path\to\video\file", "outputfile_name", "\destination\path")
31
+ You can intialize the code as you
32
+
33
+ video = VideoToMp3.new(input_filename, output_filename, {:ab => 64000, :ar => 22050, :destination_path => "destination/path"})
34
+
35
+ Then you will have method to convert into mp3 and the default location for the file will be "/home/username/mp3_files"
36
+
37
+ Just call video.convert and it will do the conversion
38
+
39
+ or
40
+
41
+ video = VideoToMp3.new
42
+
43
+ video.convert({
44
+ :input_filename => "/absolute/path/to/input/file",
45
+ :output_filename => "outputfile" #you dont need to add mp3 in the end,
46
+ :destination_path => "/absolute/destination/path" #this should be directory path
47
+ #and any other option which which ffmpeg take for the mp3 audio conversion as key value pair
48
+ #for eg :ab => 64000 --> audio bitrate '-ab 64000'
49
+ })
50
+
51
+ incase you donot provide any options for conversion then it uses the default option which is
52
+ {
53
+ :ar => 44100, #audio rate
54
+ :q => 1, #audio quality 1..20
55
+ :ab => 128000 #audio bit rate
56
+ }
57
+
58
+
59
+
60
+
61
+ convert_to_mp3 has been not changed and can be used as it was
62
+
63
+ VideoToMp3.new.connvert_to_mp3("\path\to\video\file", "outputfile_name", "\destination\path")
27
64
 
28
- if you do not provide the output filename and destination path then inputfile name is set as
29
- outputfile name and "mp3_files" directory get created if you do not provide the destination path
65
+ if you do not provide the output filename and destination path then inputfile name is set as
66
+ outputfile name and "mp3_files" directory get created if you do not provide the destination path
30
67
 
31
- you can find the converted file at destination\path\outputfile_name.mp3
68
+ you can find the converted file at destination\path\outputfile_name.mp3
32
69
 
33
70
 
34
71
  Issue Report
@@ -0,0 +1,81 @@
1
+ class Convertor
2
+
3
+ @@default_convert_options = {
4
+ :ar => 44100, #audio rate
5
+ :q => 1, #audio quality 1..20
6
+ :ab => 128000 #audio bit rate
7
+ }
8
+
9
+ ######################
10
+ # Attribute accessors
11
+ ######################
12
+ attr_accessor :in_filename, :out_filename, :duration, :options, :destination_path
13
+
14
+
15
+ ######################
16
+ # Protected methods
17
+ ######################
18
+ protected
19
+
20
+ def path
21
+ return "#{destination_path.to_s}/#{out_filename.to_s}.mp3"
22
+ end
23
+
24
+ def stringify_options
25
+ options_arr = []
26
+
27
+ options.each do |k, v|
28
+ options_arr << "-#{k.to_s.dup} #{v}"
29
+ end
30
+
31
+ return options_arr.join(" ")
32
+
33
+ end
34
+
35
+ def get_file_duration
36
+ return nil if in_filename.nil? or in_filename.empty?
37
+
38
+ command = "#{ffmpeg} -i \"#{File.expand_path(in_filename)}\" 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//"
39
+ command_output = `#{command}`
40
+
41
+
42
+ if command_output =~ /([\d][\d]):([\d][\d]):([\d][\d]).([\d]+)/
43
+ hours = $1; mins = $2; seconds = $3; mseconds = $4
44
+ time_in_seconds = (( (hours.to_i * 60 + mins.to_i) * 60 + seconds.to_i) + (mseconds.to_f / 100).ceil).to_i
45
+ end
46
+
47
+ return Time.at(time_in_seconds).gmtime.strftime('%R:%S') if !time_in_seconds.nil? or !time_in_seconds.to_s.empty?
48
+
49
+ end
50
+
51
+ def ffmpeg
52
+ @ffmpeg ||= `which ffmpeg`.chomp
53
+ end
54
+
55
+ def check_for_errors
56
+
57
+ raise VideoToMp3Error, "No input file supplied" if in_filename.nil? or in_filename.empty?
58
+
59
+ raise VideoToMp3Error, "Invalid options. It should be a hash" unless options.is_a?(Hash)
60
+
61
+ end
62
+
63
+ def set_default_attributes
64
+ if out_filename.nil? or out_filename.empty?
65
+ self.out_filename = in_filename.split("/")[-1].split(".")[0..-2].join("")
66
+ end
67
+
68
+ if destination_path.nil? or destination_path.empty?
69
+ self.destination_path = "#{ENV["HOME"]}/mp3_files"
70
+
71
+ raise VideoToMp3Error, "Unable to create directory at location - #{destination_path}" if
72
+ !Dir.exist?(destination_path) and !system("mkdir #{destination_path}")
73
+ end
74
+
75
+ if duration.nil?
76
+ self.duration = get_file_duration
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,8 @@
1
+ class Hash
2
+ def symbolize_keys!
3
+ keys.each do |key|
4
+ self[(key.to_sym rescue key) || key] = delete(key)
5
+ end
6
+ self
7
+ end
8
+ end
data/lib/video_to_mp3.rb CHANGED
@@ -1,22 +1,82 @@
1
1
  require "errors/video_to_mp3_error"
2
+ require "convertor/hash"
3
+ require "convertor/convertor"
4
+
5
+ class VideoToMp3 < Convertor
6
+
7
+ def initialize(in_filename = nil, out_filename = nil, options = {})
8
+
9
+ raise VideoToMp3Error, "Please install ffmpeg on your system. \n
10
+ Checkout the installation guide at http://ffmpeg.org/download.html
11
+ " if ffmpeg.nil? or ffmpeg.empty?
12
+
13
+ raise VideoToMp3Error, "Invalid hash options" if !options.empty? and !options.is_a?(Hash)
14
+
15
+ @in_filename = in_filename
16
+ @out_filename = out_filename
17
+ @options = options.symbolize_keys!
18
+ @duration = get_file_duration
19
+ end
20
+
21
+ ########################################################################################
22
+ # options takes the hash which will allow user to set the custom quality parameter
23
+ # for eg quality paramter q 1..20, audio rate ab 44100, 22050 etc
24
+ ########################################################################################
25
+
26
+ def convert(convert_options = {})
27
+ self.options = convert_options
28
+
29
+ #symbolize the hash keys
30
+ self.options.symbolize_keys!
31
+
32
+ #delete the input/output filename if exists in the options hash
33
+ self.in_filename = self.options.delete(:input_filename) || in_filename
34
+ self.out_filename = self.options.delete(:output_filename) || out_filename
35
+
36
+ #raises error in case it does not have the minimum required data t convert
37
+ check_for_errors
38
+
39
+ #set the destionation path if exists in options has
40
+ self.destination_path = self.options.delete(:destination_path)
41
+
42
+ #check if options are empty now
43
+ self.options = @@default_convert_options if options.empty?
44
+
45
+ #returns the ffmpeg options hash as string for eg - '-ab 128000 -ac 2'
46
+ options_string = stringify_options
47
+
48
+ #set the deafult attributes if not present for eg - output filename duration etc
49
+ set_default_attributes
50
+
51
+ #convert to mp3
52
+ ffmpeg_command = "#{ffmpeg} -i #{in_filename} #{options_string} #{path}"
53
+
54
+ #system method runs the command and return the true or false
55
+ command_status = system(ffmpeg_command)
56
+
57
+ if command_status == true
58
+ return path
59
+ else
60
+ raise VideoToMp3Error, "Wrong file name or check the ffmpeg installation on your system"
61
+ end
62
+
63
+ end
2
64
 
3
- class VideoToMp3
4
65
 
5
- #method to convert thhe video file to mp3
66
+ #method to convert the video file to mp3
6
67
  def connvert_to_mp3(input_filename, output_filename = nil, destination_path = "")
7
68
  raise VideoToMp3Error, "No input file supplied" if input_filename.nil? or input_filename.empty?
8
69
 
9
70
  #set the output filename sames as input filename if it is not supplied
10
71
  output_filename = input_filename.split("/")[-1] if output_filename.nil? or input_filename.empty?
11
- ffmpeg = `which ffmpeg`.chomp
12
72
 
13
73
  raise VideoToMp3Error, "Please install ffmpeg on your system. \n
14
- Chackout the installation guide at http://ffmpeg.org/download.html
74
+ Checkout the installation guide at http://ffmpeg.org/download.html
15
75
  " if ffmpeg.nil? or ffmpeg.empty?
16
76
 
17
77
  if destination_path.nil? or destination_path.empty?
18
78
  #create the mp3_files directory
19
- default_path = "mp3_files"
79
+ default_path = "#{ENV["HOME"]}/mp3_files"
20
80
  system("mkdir #{default_path}")
21
81
  destination_path = default_path
22
82
  end
@@ -24,10 +84,6 @@ class VideoToMp3
24
84
  path = "#{destination_path}/#{output_filename}.mp3"
25
85
  ffmpeg_command = "#{ffmpeg} -i #{input_filename} -q 1 #{path}"
26
86
 
27
- puts "*********************************"
28
- puts ffmpeg_command
29
- puts "*********************************"
30
-
31
87
  #system method runs the command and return the true or false
32
88
  command_status = system(ffmpeg_command)
33
89
 
@@ -40,4 +96,12 @@ class VideoToMp3
40
96
 
41
97
  end
42
98
 
99
+ def options
100
+ @options ||= {}
101
+ end
102
+
103
+ def duration
104
+ @duration ||= get_file_duration
105
+ end
106
+
43
107
  end
data/video_to_mp3.gemspec CHANGED
@@ -2,7 +2,7 @@ $LOAD_PATH.push File.expand_path("../lib", __FILE__)
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "video_to_mp3"
5
- s.version = "0.0.5"
5
+ s.version = "0.0.6"
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.author = "Naveen Agarwal"
8
8
  s.email = ["naveenagarwal287@gmail.com"]
@@ -22,6 +22,8 @@ Gem::Specification.new do |s|
22
22
  s.files = [
23
23
  "lib/video_to_mp3.rb",
24
24
  "lib/errors/video_to_mp3_error.rb",
25
+ "lib/convertor/hash.rb",
26
+ "lib/convertor/convertor.rb",
25
27
  "README.md",
26
28
  "video_to_mp3.gemspec"
27
29
  ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: video_to_mp3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-13 00:00:00.000000000 Z
12
+ date: 2013-01-26 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: audio/video media convertor
15
15
  email:
@@ -20,6 +20,8 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - lib/video_to_mp3.rb
22
22
  - lib/errors/video_to_mp3_error.rb
23
+ - lib/convertor/hash.rb
24
+ - lib/convertor/convertor.rb
23
25
  - README.md
24
26
  - video_to_mp3.gemspec
25
27
  homepage: https://github.com/naveenagarwal/video_to_mp3.git