Skip to content

Instantly share code, notes, and snippets.

@jmarsh24
Last active November 26, 2020 12:44
Show Gist options
  • Save jmarsh24/280a8ccfb283888d53f548eeb06aba9b to your computer and use it in GitHub Desktop.
Save jmarsh24/280a8ccfb283888d53f548eeb06aba9b to your computer and use it in GitHub Desktop.
{
"status" => {
"msg" => "Success",
"code" => 0,
"version" => "1.0"
},
"metadata" => {
"music" => [
[0] {
"external_ids" => {},
"contributors" => {
"composers" => [
[0] "Enrique Cadicamo",
[1] "Horacio Petorosi"
],
"lyricists" => [
[0] "Enrique Cadicamo",
[1] "Horacio Petorosi"
]
},
"label" => "RHI",
"duration_ms" => 208282,
"score" => 100,
"release_date" => "2015-11-16",
"play_offset_ms" => 119500,
"external_metadata" => {
"youtube" => {
"vid" => "J-S2IVtGen0"
},
"spotify" => {
"album" => {
"id" => "3Rd02aN1olr8SrLZJmqhE1"
},
"artists" => [
[0] {
"id" => "4bKxOawCFAMSMSKyOMQzsE"
},
[1] {
"id" => "6OiarJpZWTn6cj4oTlsFY7"
}
],
"track" => {
"id" => "3pR2yXPugvX5LLbvyaAa1j"
}
}
},
"genres" => [
[0] {
"name" => "Latin"
}
],
"title" => "Invierno",
"acrid" => "be2e3bc8420c0ae583a55b058b079e2f",
"album" => {
"name" => "Mi Noche Triste"
},
"result_from" => 1,
"artists" => [
[0] {
"name" => "Francisco Canaro Y Su Orquesta"
},
[1] {
"name" => "Roberto Maida"
}
]
}
],
"timestamp_utc" => "2020-11-26 12:27:57"
},
"cost_time" => 4.7649998664856,
"result_type" => 0
}
def song_match_all(count, offset)
Video.limit(count).offset(offset).each do |youtube_video|
youtube_audio_full = YoutubeDL.download(
"https://www.youtube.com/watch?v=#{youtube_video.youtube_id}",
{format: "140", output: "~/environment/data/audio/%(id)s.wav"}
)
song = FFMPEG::Movie.new(youtube_audio_full.filename.to_s)
output_file_path = youtube_audio_full.filename.gsub(/.wav/, "_15s.wav")
time_1 = youtube_audio_full.duration / 2
time_2 = time_1 + 15
song_transcoded = song.transcode(output_file_path,
{audio_codec: "pcm_s16le", audio_channels: 1, audio_bitrate: 16, audio_sample_rate: 16000, custom: %W[-ss #{time_1} -t #{time_2}]})
song_match_output = Video.acrcloud_match(output_file_path)
puts ap JSON.parse song_match_output
video = JSON.parse(song_match_output).extend Hashie::Extensions::DeepFind
if video["status"]["code"] == 0
spotify_album_id = video.deep_find("spotify")["album"]["id"] if video.deep_find("spotify")["album"]["id"].respond_to?
spotify_album_name = RSpotify::Album.find(spotify_album_id).name if video.deep_find("spotify")["album"]["id"].respond_to?
spotify_artist_id = video.deep_find("spotify")["artists"][0]["id"] if video.deep_find("spotify")["artists"][0]["id"].respond_to?
spotify_artist_name = RSpotify::Artist.find(spotify_artist_id).name if video.deep_find("spotify")["artists"][0]["id"].respond_to?
spotify_artist_id_2 = video.deep_find("spotify")["artists"][1]["id"] if video.deep_find("spotify")["artists"][1]["id"].respond_to?
spotify_artist_name_2 = RSpotify::Artist.find(spotify_artist_id_2).name if video.deep_find("spotify")["artists"][1]["id"].respond_to?
spotify_track_id = video.deep_find("spotify")["track"]["id"] if video.deep_find("spotify")["track"]["id"].respond_to?
spotify_track_name = RSpotify::Track.find(spotify_track_id).name video.deep_find("spotify")["track"]["id"].respond_to?
youtube_song_id = video.deep_find("youtube")["vid"] if video.deep_find("youtube")["vid"].respond_to?
youtube_video.update(
spotify_album_id: spotify_album_id,
spotify_album_name: spotify_album_name,
spotify_artist_id: spotify_artist_id,
spotify_artist_name: spotify_artist_name,
spotify_artist_id_2: spotify_artist_id_2,
spotify_artist_name_2: spotify_artist_name_2,
spotify_track_id: spotify_track_id,
spotify_track_name: spotify_track_name,
youtube_song_id: youtube_song_id
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment