public MediaProperties GetMusicProperties(Media media) { if (media == null) return null; if (!media.isParsed()) media.parse(); if (!media.isParsed()) return null; var mP = new MediaProperties(); mP.AlbumArtist = media.meta(MediaMeta.AlbumArtist); mP.Artist = media.meta(MediaMeta.Artist); mP.Album = media.meta(MediaMeta.Album); mP.Title = media.meta(MediaMeta.Title); var yearString = media.meta(MediaMeta.Date); var year = 0; if(int.TryParse(yearString, out year)) { mP.Year = year; } var durationLong = media.duration(); TimeSpan duration = TimeSpan.FromMilliseconds(durationLong); mP.Duration = duration; var trackNbString = media.meta(MediaMeta.TrackNumber); uint trackNbInt = 0; uint.TryParse(trackNbString, out trackNbInt); mP.Tracknumber = trackNbInt; var discNb = media.meta(MediaMeta.DiscNumber); int discNbInt = 1; int.TryParse(discNb, out discNbInt); mP.DiscNumber = discNbInt; return mP; }
private static async Task CreateDatabaseFromMusicFile(StorageFile item) { try { if (!VLCFileExtensions.AudioExtensions.Contains(item.FileType.ToLower())) return; var media = Locator.VLCService.GetMediaFromPath(item.Path); MediaProperties mP = Locator.VLCService.GetMusicProperties(media); if(mP == null) { var props = await item.Properties.GetMusicPropertiesAsync(); mP = new MediaProperties() { Album = props.Album, AlbumArtist = props.AlbumArtist, Artist = props.Artist, Title = props.Title, Tracknumber = props.TrackNumber, Genre = (props.Genre != null && props.Genre.Any()) ? props.Genre[0] : null, }; } if (mP != null) { var artistName = mP.Artist; var albumArtistName = mP.AlbumArtist; ArtistItem artist = Locator.MusicLibraryVM._artistDatabase.LoadViaArtistName(string.IsNullOrEmpty(albumArtistName) ? artistName : albumArtistName); if (artist == null) { artist = new ArtistItem(); artist.Name = string.IsNullOrEmpty(albumArtistName) ? artistName : albumArtistName; await Locator.MusicLibraryVM._artistDatabase.Add(artist); await App.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { AddArtist(artist); }); } var albumName = mP.Album; var albumYear = mP.Year; AlbumItem album = await Locator.MusicLibraryVM._albumDatabase.LoadAlbumViaName(artist.Id, albumName); if (album == null) { var albumUrl = Locator.VLCService.GetAlbumUrl(media); var hasVLCFoundCover = false; if (!string.IsNullOrEmpty(albumUrl) && albumUrl.StartsWith("file://")) { // The Uri will be like // ms-appdata:///local/vlc/art/artistalbum/30 Seconds To Mars/B-sides & Rarities/art.jpg hasVLCFoundCover = true; } album = new AlbumItem { Name = string.IsNullOrEmpty(albumName) ? string.Empty : albumName, AlbumArtist = albumArtistName, Artist = string.IsNullOrEmpty(albumArtistName) ? artistName : albumArtistName, ArtistId = artist.Id, Favorite = false, Year = albumYear, IsPictureLoaded = hasVLCFoundCover, IsVLCCover = hasVLCFoundCover, }; await Locator.MusicLibraryVM._albumDatabase.Add(album); await App.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => { var artistFromCollection = Locator.MusicLibraryVM.Artists.FirstOrDefault(x => x.Id == album.ArtistId); AddAlbum(album, artistFromCollection); Locator.MainVM.InformationText = string.Format(Strings.AlbumsFound, Locator.MusicLibraryVM.Albums.Count); }); } TrackItem track = new TrackItem { AlbumId = album.Id, AlbumName = album.Name, ArtistId = artist.Id, ArtistName = artistName, CurrentPosition = 0, Duration = mP.Duration, Favorite = false, Name = string.IsNullOrEmpty(mP.Title) ? item.DisplayName : mP.Title, Path = item.Path, Index = mP.Tracknumber, DiscNumber = mP.DiscNumber, }; await Locator.MusicLibraryVM._trackDatabase.Add(track); await App.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () => AddTrack(track)); } } catch (Exception e) { ExceptionHelper.CreateMemorizedException("MusicLibraryManagement.CreateDatabaseFromMusicFile", e); } }
public MediaProperties GetVideoProperties(Media media) { if (media == null) return null; if (!media.isParsed()) media.parse(); if (!media.isParsed()) return null; var mP = new MediaProperties(); mP.Title = media.meta(MediaMeta.Title); var showName = media.meta(MediaMeta.ShowName); if(string.IsNullOrEmpty(showName)) { showName = media.meta(MediaMeta.Artist); } if (!string.IsNullOrEmpty(showName)) { mP.ShowTitle = showName; } var episodeString = media.meta(MediaMeta.Episode); if(string.IsNullOrEmpty(episodeString)) { episodeString = media.meta(MediaMeta.TrackNumber); } var episode = 0; if(!string.IsNullOrEmpty(episodeString) && int.TryParse(episodeString, out episode)) { mP.Episode = episode; } var episodesTotal = 0; var episodesTotalString = media.meta(MediaMeta.TrackTotal); if(!string.IsNullOrEmpty(episodesTotalString) && int.TryParse(episodesTotalString, out episodesTotal)) { mP.Episodes = episodesTotal; } return mP; }
public static MediaProperties tvShowEpisodeInfoFromString(string title) { if (string.IsNullOrEmpty(title)) return null; title = title.ToLower(); bool successfulSearch = false; var mP = new MediaProperties(); int stringLength = title.Length; if (stringLength < 6) return null; try { // Search for s01e10. for (int i = 0; i < stringLength - 5; i++) { if (title.ElementAt(i) != 's' || !isDigit(title.ElementAt(i + 1)) || !isDigit(title.ElementAt(i + 2)) || title.ElementAt(i + 3) != 'e' || !isDigit(title.ElementAt(i + 4)) || !isDigit(title.ElementAt(i + 5))) { // Inverted "if" statement to reduce nesting. continue; } string seasonString = title.ElementAt(i + 1).ToString() + title.ElementAt(i + 2).ToString(); string episodeString; if (title.Length > i + 6 && isDigit(title.ElementAt(i + 6))) episodeString = title.ElementAt(i + 4).ToString() + title.ElementAt(i + 5).ToString() + title.ElementAt(i + 6).ToString(); else episodeString = title.ElementAt(i + 4).ToString() + title.ElementAt(i + 5).ToString(); string tvShowName = i > 0 ? title.Substring(0, i) : Strings.UnknownShow; if (tvShowName != null) { tvShowName = CapitalizedString(Decrapify(tvShowName)); } string episodeName = stringLength > i + 4 ? title.Substring(0, i + 6) : null; if (episodeName != null) { episodeName = Decrapify(episodeName); } // Fill the MediaProperties object var season = 0; if(int.TryParse(seasonString, out season)) { mP.Season = season; } var episode = 0; if(int.TryParse(episodeString, out episode)) { mP.Episode = episode; } if (!string.IsNullOrEmpty(tvShowName)) { mP.ShowTitle = tvShowName; } if (!string.IsNullOrEmpty(episodeName)) { mP.Title = CapitalizedString(episodeName); } successfulSearch = true; } // search for 0x00 if (!successfulSearch) { for (int i = 0; i < stringLength - 4; i++) { if (!isDigit(title.ElementAt(i)) || title.ElementAt(i + 1) != 'x' || !isDigit(title.ElementAt(i + 2)) || !isDigit(title.ElementAt(i + 3))) { // Inverted "if" statement to reduce nesting. continue; } string seasonString = title.ElementAt(i).ToString(); string episodeString = title.ElementAt(i + 2).ToString() + title.ElementAt(i + 3).ToString(); string tvShowName = i > 0 ? title.Substring(0, i) : Strings.UnknownShow; ; if (tvShowName != null) { tvShowName = CapitalizedString(Decrapify(tvShowName)); } string episodeName = stringLength > i + 4 ? title.Substring(0, i + 4) : null; if (episodeName != null) { episodeName = Decrapify(episodeName); } var season = 0; if (int.TryParse(seasonString, out season)) { mP.Season = season; } // 'episode' will never be null according to conditions above, so checking for it is not needed. var episode = 0; if(int.TryParse(episodeString, out episode)) { mP.Episode = episode; } if (!string.IsNullOrEmpty(tvShowName)) { mP.ShowTitle = tvShowName; } if (!string.IsNullOrEmpty(episodeName)) { mP.Title = CapitalizedString(episodeName); } successfulSearch = true; } } } catch { } return successfulSearch ? mP : null; }