public static bool ConvertAllTheThings(byte[] strangefile, ref Track song, string extension) { string directory = Path.GetDirectoryName(song.LocalPath); byte[] mp3bytes = null; if (extension == ".wav") { mp3bytes = ConvertWavToMp3(strangefile, directory); if (mp3bytes != null) { song.LocalPath += ".mp3"; //conversion wil result in an mp3 File.WriteAllBytes(song.LocalPath, mp3bytes); return true; } else { return false; } } if (extension == ".aiff" || extension == ".aif") { mp3bytes = ConvertAiffToMp3(strangefile, directory); if (mp3bytes != null) { song.LocalPath += ".mp3"; //conversion wil result in an mp3 File.WriteAllBytes(song.LocalPath, mp3bytes); return true; } else { return false; } } if((extension == ".m4a" || extension == ".aac") && isWindows8OrHigher()) { return ConvertM4aToMp3(strangefile, directory, ref song); } else { return false; } }
public static void setup() { song = new Track(); }
//requires windows 8 or higher public static bool ConvertM4aToMp3(byte[] m4aFile, string directory, ref Track song) { var tempFile = Path.Combine(directory, "tempdata" + uniqueTempFileCounter + ".m4a"); // try { uniqueTempFileCounter += 1; System.IO.File.WriteAllBytes(tempFile, m4aFile); song.LocalPath += ".mp3"; //conversion wil result in an mp3 using (var reader = new MediaFoundationReader(tempFile)) //this reader supports: MP3, AAC and WAV { Guid AACtype = AudioSubtypes.MFAudioFormat_AAC; int[] bitrates = MediaFoundationEncoder.GetEncodeBitrates(AACtype, reader.WaveFormat.SampleRate, reader.WaveFormat.Channels); MediaFoundationEncoder.EncodeToMp3(reader, song.LocalPath, bitrates[bitrates.GetUpperBound(0)]); } File.Delete(tempFile); return true; } catch (Exception e) { Debug.WriteLine(e); if(File.Exists(tempFile)) { File.Delete(tempFile); } return false; } }
private bool DownloadTrack(Track song, string apiKey) { bool downloaded = false; if (IsActive) { using (WebClient client = new WebClient()) { Directory.CreateDirectory(Path.GetDirectoryName(song.LocalPath)); if (song.IsHD) { string extension = null; try { WebRequest request = WebRequest.Create(song.EffectiveDownloadUrl + string.Format("?client_id={0}",apiKey)); request.Method = "HEAD"; using (WebResponse response = request.GetResponse()) { extension = Path.GetExtension(response.Headers["Content-Disposition"] .Replace("attachment;filename=", "").Replace("\"", "")); } } catch (Exception) { // the download link might be invalid WebRequest request = WebRequest.Create(song.stream_url + string.Format("?client_id={0}", apiKey)); request.Method = "HEAD"; using (WebResponse response = request.GetResponse()) { extension = Path.GetExtension(response.Headers["Content-Disposition"] .Replace("attachment;filename=", "").Replace("\"", "")); } } song.LocalPath += extension; } else { song.LocalPath += ".mp3"; } client.DownloadFile(song.EffectiveDownloadUrl+string.Format("?client_id={0}",apiKey), song.LocalPath); // metadata tagging TagLib.File tagFile = TagLib.File.Create(song.LocalPath); tagFile.Tag.Title = song.Title; string artworkFilepath = null; if (!String.IsNullOrEmpty(song.Username)) { tagFile.Tag.AlbumArtists = new string[] { song.Username }; tagFile.Tag.Performers = new string[] { song.Username }; } if (!String.IsNullOrEmpty(song.genre)) { tagFile.Tag.Genres = new string[] { song.genre }; } if (!String.IsNullOrEmpty(song.description)) { tagFile.Tag.Comment = song.description; } if (!String.IsNullOrEmpty(song.artwork_url)) { // download artwork artworkFilepath = Path.GetTempFileName(); using (WebClient web = new WebClient()) { web.DownloadFile(song.artwork_url, artworkFilepath); } tagFile.Tag.Pictures = new[] { new TagLib.Picture(artworkFilepath) }; } tagFile.Save(); if (artworkFilepath != null && File.Exists(artworkFilepath)) { File.Delete(artworkFilepath); } lock (SongsDownloadedLock) { SongsDownloaded.Add(song); downloaded = true; } } } return downloaded; }