Exemplo n.º 1
0
        public Track(string filename, string source = "None")
        {
            this.FilePath = Path.GetFullPath(filename);
            this.Source   = source;
            IFileAbstraction f = new TagLib.File.LocalFileAbstraction(filename);

            using (var file = TagLib.File.Create(f))
            {
                switch (file.Properties.Description)
                {
                case "Flac Audio":
                    this.FileType = Track.GetFlacType(file.Properties.BitsPerSample);
                    break;

                case "MPEG Version 1 Audio, Layer 3":
                case "MPEG Version 2 Audio, Layer 3":
                case "MPEG Version 2.5 Audio, Layer 3":
                    this.FileType = TrackFileType.MP3_CBR;
                    break;

                case "MPEG Version 1 Audio, Layer 3 VBR":
                case "MPEG Version 2 Audio, Layer 3 VBR":
                case "MPEG Version 2.5 Audio, Layer 3 VBR":
                    this.FileType = TrackFileType.MP3_VBR;
                    break;

                case "MPEG-4 Audio (alac)":
                    this.FileType = TrackFileType.ALAC;
                    break;

                case "MPEG-4 Audio (mp4a)":
                    this.FileType = TrackFileType.AAC;
                    break;

                default:
                    this.FileType = TrackFileType.UNKNOWN;
                    break;
                }
                this.Duration           = file.Properties.Duration;
                this.SampleRate         = file.Properties.AudioSampleRate;
                this.Bitrate            = file.Properties.AudioBitrate;
                this.Album              = file.Tag.Album;
                this.AlbumArtists       = file.Tag.AlbumArtists.ToList();
                this.Artist             = file.Tag.FirstPerformer;
                this.TrackNumber        = file.Tag.Track;
                this.Year               = file.Tag.Year;
                this.MusicBrainzTrackId = file.Tag.MusicBrainzTrackId;
                this.Title              = file.Tag.Title;
                this.DiscNumber         = file.Tag.Disc;
                var frontAlbum = from picture in file.Tag.Pictures
                                 where picture.Type == TagLib.PictureType.FrontCover
                                 select picture;
                this.HasFrontCover = frontAlbum.Any();
                if (this.HasFrontCover)
                {
                    using (var image = Image.FromStream(new MemoryStream(frontAlbum.First().Data.Data), false, false))
                    {
                        this.FrontCoverHeight = image.Height;
                        this.FrontCoverWidth  = image.Width;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async Task <bool> AddSong(IFormFile file)
        {
            await using (Stream stream = file.OpenReadStream())
            {
                using var binaryReader = new BinaryReader(stream);
                var fileContent = binaryReader.ReadBytes((int)file.Length);
                var str         = Encoding.Default.GetString(fileContent);
                //await this.UploadFile(file.ContentDisposition);
                var filePath = @"songstorage/testmp3.mp3";

                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }
                TagLibFile.IFileAbstraction iFile = new TagLibFile.LocalFileAbstraction(filePath);

                var mp3 = TagLibFile.Create(iFile);

                var artistName  = mp3.Tag.Performers[0];
                var albumName   = mp3.Tag.Album;
                var albumNumber = mp3.Tag.Track;

                //todo add album art

                //create new artist / album if it doesnt exist yet
                var artist = _artistManager.FindByName(artistName) ?? new Artist()
                {
                    Name = artistName
                };

                var album = _albumManager.FindByName(albumName) ?? new Album()
                {
                    Artist = artist,
                    Name   = albumName
                };


                //TODO fix duration
                var song = new Song(mp3.Tag.Title, artist, album, mp3.Properties.Duration.Seconds, (int)albumNumber);

                _songManager.Add(song);

                // var extension = Path.GetExtension(file.FileName);
                //   var folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MusifyStorage", artistName, albumName);
                //  var fileName = albumNumber + " - " + mp3.Tag.Title;
                // var fullPath = Path.Combine(folderPath, fileName + extension);

                var fileInfo = new FileInfo(song.FilePath);
                if (!Directory.Exists(fileInfo.DirectoryName))
                {
                    Directory.CreateDirectory(fileInfo.DirectoryName);
                }

                using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }

                Console.WriteLine(fileInfo.FullName);
            }

            return(true);
        }