Skip to content

Commit

Permalink
Refactor audio saving logic
Browse files Browse the repository at this point in the history
  • Loading branch information
staniel359 committed Oct 26, 2023
1 parent 2848f86 commit bbcde46
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
8 changes: 7 additions & 1 deletion app/services/muffon/utils/audio/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Muffon
module Utils
module Audio
module Link
FILE_EXTENSION = 'mp3'.freeze

def call
return if not_all_args? || no_data?

Expand Down Expand Up @@ -46,7 +48,11 @@ def audio_file

def audio_path
@audio_path ||=
"#{audio_folder}/#{@args[:track_id]}.mp3"
"#{audio_folder}/#{@args[:track_id]}.#{file_extension}"
end

def file_extension
self.class::FILE_EXTENSION
end

def audio_link
Expand Down
2 changes: 1 addition & 1 deletion app/services/spotify/utils/audio/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def audio_path
end

def test_audio_path
"files/audio/spotify/#{@args[:track_id]}.mp3"
"files/audio/spotify/#{@args[:track_id]}.ogg"
end

def link
Expand Down
12 changes: 9 additions & 3 deletions app/services/vk/utils/audio/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module VK
module Utils
module Audio
class Link < VK::Base
FILE_EXTENSION = 'mp3'.freeze

include Muffon::Utils::Audio::Link

private
Expand All @@ -20,7 +22,7 @@ def no_data?
def write_audio_data_to_file
convert_m3u8_file_to_ts_file

convert_ts_file_to_mp3_file
convert_ts_file_to_audio_file

delete_ts_file if ts_file_present?
end
Expand All @@ -38,12 +40,16 @@ def file_path
"public/#{audio_folder}/#{@args[:track_id]}"
end

def convert_ts_file_to_mp3_file
def convert_ts_file_to_audio_file
return if test?

`ffmpeg -loglevel panic -y \
-i #{file_path}.ts \
-c copy #{file_path}.mp3`
-c copy #{audio_file_name}`
end

def audio_file_name
"#{file_path}.#{file_extension}"
end

def ts_file_present?
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/spotify/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def albums_data
end

def info_data
{:track=>{:album=>{:source=>{:id=>"3OYnManu1Nlxnw9OMng7BH", :name=>"spotify"}, :title=>"Hounds of Love (2018 Remaster)"}, :artist=>{:name=>"Kate Bush"}, :artists=>[{:name=>"Kate Bush", :source=>{:id=>"1aSxMhuvixZ8h9dK9jIDwL", :name=>"spotify"}}], :audio=>{:link=>"http://localhost:4001/files/audio/spotify/29d0nY7TzCoi22XBqDQkiP.mp3", :present=>true}, :duration=>301, :image=>{:extrasmall=>"https://i.scdn.co/image/ab67616d000048515c390e413e27798edd4d18b4", :large=>"https://i.scdn.co/image/ab67616d0000b2735c390e413e27798edd4d18b4", :medium=>"https://i.scdn.co/image/ab67616d00001e025c390e413e27798edd4d18b4", :original=>"https://i.scdn.co/image/ab67616d0000b2735c390e413e27798edd4d18b4", :small=>"https://i.scdn.co/image/ab67616d000048515c390e413e27798edd4d18b4"}, :player_id=>"1", :source=>{:id=>"29d0nY7TzCoi22XBqDQkiP", :links=>{:original=>"https://open.spotify.com/track/29d0nY7TzCoi22XBqDQkiP", :streaming=>"https://song.link/s/29d0nY7TzCoi22XBqDQkiP"}, :name=>"spotify"}, :title=>"Running Up That Hill (A Deal With God) - 2018 Remaster"}}
{:track=>{:album=>{:source=>{:id=>"3OYnManu1Nlxnw9OMng7BH", :name=>"spotify"}, :title=>"Hounds of Love (2018 Remaster)"}, :artist=>{:name=>"Kate Bush"}, :artists=>[{:name=>"Kate Bush", :source=>{:id=>"1aSxMhuvixZ8h9dK9jIDwL", :name=>"spotify"}}], :audio=>{:link=>"http://localhost:4001/files/audio/spotify/29d0nY7TzCoi22XBqDQkiP.ogg", :present=>true}, :duration=>301, :image=>{:extrasmall=>"https://i.scdn.co/image/ab67616d000048515c390e413e27798edd4d18b4", :large=>"https://i.scdn.co/image/ab67616d0000b2735c390e413e27798edd4d18b4", :medium=>"https://i.scdn.co/image/ab67616d00001e025c390e413e27798edd4d18b4", :original=>"https://i.scdn.co/image/ab67616d0000b2735c390e413e27798edd4d18b4", :small=>"https://i.scdn.co/image/ab67616d000048515c390e413e27798edd4d18b4"}, :player_id=>"1", :source=>{:id=>"29d0nY7TzCoi22XBqDQkiP", :links=>{:original=>"https://open.spotify.com/track/29d0nY7TzCoi22XBqDQkiP", :streaming=>"https://song.link/s/29d0nY7TzCoi22XBqDQkiP"}, :name=>"spotify"}, :title=>"Running Up That Hill (A Deal With God) - 2018 Remaster"}}
end

def links_data
Expand Down

0 comments on commit bbcde46

Please sign in to comment.