Exemple #1
0
        private M3uPlaylist MapToEntity(IPlaylist playlist)
        {
            M3uPlaylist m3UPlaylist = new M3uPlaylist();

            m3UPlaylist.IsExtended = true;

            m3UPlaylist.Comments.Add($"PLAYLIST-Author:{playlist.Author}");
            m3UPlaylist.Comments.Add($"PLAYLIST-Title:{playlist.Name}");
            m3UPlaylist.Comments.Add($"PLAYLIST-CreatedAt:{playlist.CreatedAt}");

            foreach (var item in playlist.Items)
            {
                var entry = new M3uPlaylistEntry()
                {
                    Duration = item.Duration,
                    Path     = item.Path,
                    Title    = item.Title,
                    Comments = new List <string>()
                };

                m3UPlaylist.PlaylistEntries.Add(entry);
            }

            return(m3UPlaylist);
        }
Exemple #2
0
        public static string ChainAttributes(M3uPlaylistEntry entry)
        {
            var properties     = entry.GetType().GetProperties();
            var joinedProperty = new List <string>();
            var title          = string.Empty;

            foreach (var property in properties)
            {
                if (property.Name == "Uri")
                {
                    continue;
                }
                var value = property.GetValue(entry, null);
                if (property.Name == "Title")
                {
                    title = $"{value}";
                    continue;
                }
                if (string.IsNullOrWhiteSpace(value?.ToString()))
                {
                    continue;
                }
                var attribute = property.GetCustomAttribute <PlaylistEntryAttribute>();

                joinedProperty.Add(attribute != null ? $"{attribute.Tag}=\"{value}\"" : $"{value}");
            }

            var serializedProperties = string.Join(" ", joinedProperty);

            var serializedData = $"{Directives.EXTINF}:-1 {serializedProperties}, {title}";

            return(serializedData);
        }
Exemple #3
0
        private M3uPlaylist GetExtM3u(IEnumerable <string> playlistLines)
        {
            var playlist = new M3uPlaylist
            {
                IsExtended = true,
            };

            var currentEntry = new M3uPlaylistEntry {
                Album = "", AlbumArtist = "", Title = ""
            };

            foreach (var currentLine in playlistLines)
            {
                var match = Regex.Match(currentLine, @"^#EXTINF:(-?\d*),(.*)$");
                if (match.Success)
                {
                    var seconds = string.IsNullOrEmpty(match.Groups[1].Value) ? 0 : double.Parse(match.Groups[1].Value);
                    currentEntry.Duration = TimeSpan.FromSeconds(seconds);
                    currentEntry.Title    = match.Groups[2].Value;
                    continue;
                }

                match = Regex.Match(currentLine, @"^#(EXTALB):(.*)$");
                if (match.Success)
                {
                    currentEntry.Album = match.Groups[2].Value;
                    continue;
                }

                match = Regex.Match(currentLine, @"^#(EXTART):(.*)$");
                if (match.Success)
                {
                    currentEntry.AlbumArtist = match.Groups[2].Value;
                    continue;
                }

                match = Regex.Match(currentLine, @"^#(EXT.*):(.*)$");
                if (match.Success)
                {
                    currentEntry.CustomProperties.Add(match.Groups[1].Value, match.Groups[2].Value);
                    continue;
                }

                match = Regex.Match(currentLine, @"^#(?!EXT)(.*)$");
                if (match.Success)
                {
                    currentEntry.Comments.Add(match.Groups[1].Value);
                    continue;
                }

                currentEntry.Path = WebUtility.UrlDecode(currentLine);
                playlist.PlaylistEntries.Add(currentEntry);
                currentEntry             = new M3uPlaylistEntry();
                currentEntry.Album       = "";
                currentEntry.AlbumArtist = "";
                currentEntry.Title       = "";
            }

            return(playlist);
        }
Exemple #4
0
        public void ShouldChainAttributes()
        {
            var m3uPlaylistEntry = new M3uPlaylistEntry
            {
                Title = "TVOne",
                Logo  = "http://logo"
            };
            var title = Helpers.ChainAttributes(m3uPlaylistEntry);

            Assert.Equal(
                "#EXTINF:-1 tvg-logo=\"http://logo\", TVOne",
                title);
        }
Exemple #5
0
        public void ShouldChainFullAttributesList()
        {
            var m3uPlaylistEntry = new M3uPlaylistEntry
            {
                Title     = "TVOne",
                Uri       = "http://local",
                Name      = "TvOne",
                Logo      = "http://logo",
                SmallLogo = "http://local-small-logo",
                Code      = "123",
                Group     = "local",
                Id        = "12"
            };
            var title = Helpers.ChainAttributes(m3uPlaylistEntry);

            Assert.Equal(
                "#EXTINF:-1 tvg-id=\"12\" tvg-logo=\"http://logo\" tvg-name=\"TvOne\" tvg-logo-small=\"http://local-small-logo\" group-title=\"local\" parent-code=\"123\", TVOne",
                title);
        }
        private void DataAvailable(object s, DataAvailableEventArgs e)
        {
            double numBytesInTrack       = (_playlist.PlaylistEntries[_playlistIndex].Duration.TotalMilliseconds / 1000) * e.Format.BytesPerSecond;
            int    remainingBytesInTrack = (int)(numBytesInTrack - trackBytesWritten);

            bool bufferIsSilent = e.Data.All(item => item == 0);

            bool trackEndFound = started && remainingBytesInTrack < TrackEndCheckByteThreshold && bufferIsSilent;

            if (trackEndFound)
            {
                M3uPlaylistEntry entry = _playlist.PlaylistEntries[_playlistIndex];
                Task.Run(() => _converter.CreateMp3FromWav(entry, currentWavFilename));

                _writer.Dispose();
                trackBytesWritten = 0;
                _playlistIndex++;
                if (_playlistIndex >= _playlist.PlaylistEntries.Count())
                {
                    _capture.DataAvailable -= DataAvailable;
                }
                started = false;
            }

            if (!started)
            {
                if (bufferIsSilent)
                {
                    return;
                }
                else
                {
                    Log.Debug($"Started recording track: {_playlist.PlaylistEntries[_playlistIndex].Title}; expected bytes in track: {numBytesInTrack}.\n\n");
                    StartNewTrackRecording();
                    started = true;
                }
            }

            byte[] buffer = new byte[e.ByteCount];
            Buffer.BlockCopy(e.Data, 0, buffer, 0, e.ByteCount);
            _writer.Write(buffer, 0, e.ByteCount);
            trackBytesWritten += e.ByteCount;
        }
        public bool CreateMp3FromWav(M3uPlaylistEntry entry, string wavFilePath)
        {
            // eliminate invalid characters from the track title to ensure the file saves
            string sanitizedEntryTitle = Program.StripInvalidFilenameCharacters(entry.Title);

            var mp3Filename = $"{sanitizedEntryTitle}.mp3";
            var successful  = ConvertWavToMp3(wavFilePath, mp3Filename);

            if (successful)
            {
                var fileMetadata = TagLib.File.Create(mp3Filename);
                fileMetadata.Tag.Album      = entry.Album;
                fileMetadata.Tag.Performers = new string[] { entry.AlbumArtist };
                fileMetadata.Tag.Title      = entry.Title;
                fileMetadata.Save();

                File.Delete(wavFilePath);
            }

            return(successful);
        }
        public ChannelInfo(M3uPlaylistEntry channelEntry)
        {
            URL = channelEntry.Path;
            string extraInfoForParser = "";

            foreach (KeyValuePair <string, string> entry in channelEntry.CustomProperties)
            {
                extraInfoForParser += entry.Key + ":" + entry.Value;
            }
            string regexTVGName  = "tvg-name=\"(.*?)\"";
            string regexTVGLogo  = "tvg-logo=\"(.*?)\"";
            string regexTVGGroup = "group-title=\"(.*?)\"";
            string regexTVGId    = "tvg-id=\"(.*?)\"";
            string regexTitle    = "[,](?!.*[,])(.*?)$";

            TVGName  = MatchAndResult(extraInfoForParser, regexTVGName);
            Title    = Utils.DecodeToUTF8(MatchAndResult(extraInfoForParser, regexTitle));
            TVGLogo  = MatchAndResult(extraInfoForParser, regexTVGLogo);
            TVGGroup = Utils.DecodeToUTF8(MatchAndResult(extraInfoForParser, regexTVGGroup));
            TVGId    = MatchAndResult(extraInfoForParser, regexTVGId);
            CalculateType();
        }
Exemple #9
0
        public void GetFromStream_ReadVLCPlaylistExtendedAndCompareWithObject_Equal()
        {
            var entry = new M3uPlaylistEntry
            {
                Album       = null,
                AlbumArtist = null,
                Duration    = TimeSpan.FromSeconds(304),
                Path        = "Aimer/春はゆく - marie/01 - 春はゆく.flac",
                Title       = "Aimer - 春はゆく",
            };

            var content = new M3uContent();

            using (var stream = Helpers.ReadStream("PlaylistVLC.m3u"))
            {
                var file      = content.GetFromStream(stream);
                var fileEntry = file.PlaylistEntries[0];

                Assert.AreEqual(entry.Path, fileEntry.Path);
                Assert.AreEqual(entry.Title, fileEntry.Title);
            }
        }
Exemple #10
0
        public void Create_CreatePlaylistExtendedAndCompareWithFile_Equal()
        {
            M3uContent  content  = new M3uContent();
            M3uPlaylist playlist = new M3uPlaylist();

            playlist.IsExtended = true;
            playlist.PlaylistEntries.Add(new M3uPlaylistEntry()
            {
                Album       = "",
                AlbumArtist = "",
                Duration    = TimeSpan.FromSeconds(254),
                Path        = @"D:\Muzyka\Andrea Bocelli\04 Chiara.mp3",
                Title       = "Andrea Bocelli - Chiara",
            });
            var entry = new M3uPlaylistEntry()
            {
                Album       = null,
                AlbumArtist = null,
                Duration    = TimeSpan.FromSeconds(-1),
                Path        = @"D:\Muzyka\Andrea Bocelli\01 Con Te Partiro.mp3",
                Title       = "Andrea Bocelli - Con Te Partiro",
            };

            entry.CustomProperties.Add("EXTVLCOPT", "network-caching=1000");
            playlist.PlaylistEntries.Add(entry);
            playlist.PlaylistEntries.Add(new M3uPlaylistEntry()
            {
                Album       = "AndreaBocelli",
                AlbumArtist = "Andrea Bocelli",
                Duration    = TimeSpan.FromSeconds(-1),
                Path        = @"D:\Muzyka\Andrea Bocelli\04 E Chiove.mp3",
                Title       = "Andrea Bocelli - E Chiove",
            });
            string created  = content.ToText(playlist);
            string fromFile = Helpers.Read("PlaylistExt.m3u");

            Assert.AreEqual(created, fromFile);
        }
Exemple #11
0
        private void SavePlaylistFile(Playlist item)
        {
            // this is probably best done as a metadata provider
            // saving a file over itself will require some work to prevent this from happening when not needed
            var playlistPath = item.Path;
            var extension    = Path.GetExtension(playlistPath);

            if (string.Equals(".wpl", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new WplPlaylist();
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new WplPlaylistEntry()
                    {
                        Path       = NormalizeItemPath(playlistPath, child.Path),
                        TrackTitle = child.Name,
                        AlbumTitle = child.Album
                    };

                    var hasAlbumArtist = child as IHasAlbumArtist;
                    if (hasAlbumArtist != null)
                    {
                        entry.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
                    }

                    var hasArtist = child as IHasArtist;
                    if (hasArtist != null)
                    {
                        entry.TrackArtist = hasArtist.Artists.FirstOrDefault();
                    }

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }

                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new WplContent().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }

            if (string.Equals(".zpl", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new ZplPlaylist();
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new ZplPlaylistEntry()
                    {
                        Path       = NormalizeItemPath(playlistPath, child.Path),
                        TrackTitle = child.Name,
                        AlbumTitle = child.Album
                    };

                    var hasAlbumArtist = child as IHasAlbumArtist;
                    if (hasAlbumArtist != null)
                    {
                        entry.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
                    }

                    var hasArtist = child as IHasArtist;
                    if (hasArtist != null)
                    {
                        entry.TrackArtist = hasArtist.Artists.FirstOrDefault();
                    }

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }
                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new ZplContent().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }

            if (string.Equals(".m3u", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new M3uPlaylist();
                playlist.IsExtended = true;
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new M3uPlaylistEntry()
                    {
                        Path  = NormalizeItemPath(playlistPath, child.Path),
                        Title = child.Name,
                        Album = child.Album
                    };

                    var hasAlbumArtist = child as IHasAlbumArtist;
                    if (hasAlbumArtist != null)
                    {
                        entry.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
                    }

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }

                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new M3uContent().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }

            if (string.Equals(".m3u8", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new M3uPlaylist();
                playlist.IsExtended = true;
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new M3uPlaylistEntry()
                    {
                        Path  = NormalizeItemPath(playlistPath, child.Path),
                        Title = child.Name,
                        Album = child.Album
                    };

                    var hasAlbumArtist = child as IHasAlbumArtist;
                    if (hasAlbumArtist != null)
                    {
                        entry.AlbumArtist = hasAlbumArtist.AlbumArtists.FirstOrDefault();
                    }

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Duration = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }

                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new M3u8Content().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }

            if (string.Equals(".pls", extension, StringComparison.OrdinalIgnoreCase))
            {
                var playlist = new PlsPlaylist();
                foreach (var child in item.GetLinkedChildren())
                {
                    var entry = new PlsPlaylistEntry()
                    {
                        Path  = NormalizeItemPath(playlistPath, child.Path),
                        Title = child.Name
                    };

                    if (child.RunTimeTicks.HasValue)
                    {
                        entry.Length = TimeSpan.FromTicks(child.RunTimeTicks.Value);
                    }

                    playlist.PlaylistEntries.Add(entry);
                }

                string text = new PlsContent().ToText(playlist);
                File.WriteAllText(playlistPath, text);
            }
        }
Exemple #12
0
        public void GetFromStream_ReadPlaylistExtendedAndCompareWithObject_Equal()
        {
            M3uContent  content  = new M3uContent();
            M3uPlaylist playlist = new M3uPlaylist();

            playlist.IsExtended = true;
            playlist.PlaylistEntries.Add(new M3uPlaylistEntry()
            {
                Album       = "",
                AlbumArtist = "",
                Duration    = TimeSpan.FromSeconds(254),
                Path        = @"D:\Muzyka\Andrea Bocelli\04 Chiara.mp3",
                Title       = "Andrea Bocelli - Chiara",
            });
            var entry = new M3uPlaylistEntry()
            {
                Album       = null,
                AlbumArtist = null,
                Duration    = TimeSpan.FromSeconds(-1),
                Path        = @"D:\Muzyka\Andrea Bocelli\01 Con Te Partiro.mp3",
                Title       = "Andrea Bocelli - Con Te Partiro",
            };

            entry.CustomProperties.Add("EXTVLCOPT", "network-caching=1000");
            playlist.PlaylistEntries.Add(entry);
            playlist.PlaylistEntries.Add(new M3uPlaylistEntry()
            {
                Album       = "AndreaBocelli",
                AlbumArtist = "Andrea Bocelli",
                Duration    = TimeSpan.FromSeconds(-1),
                Path        = @"D:\Muzyka\Andrea Bocelli\04 E Chiove.mp3",
                Title       = "Andrea Bocelli - E Chiove",
            });

            using (var stream = Helpers.ReadStream("PlaylistExt.m3u"))
            {
                var file = content.GetFromStream(stream);

                Assert.AreEqual(playlist.IsExtended, file.IsExtended);
                Assert.AreEqual(playlist.PlaylistEntries.Count, file.PlaylistEntries.Count);

                Assert.AreEqual(playlist.PlaylistEntries[0].Path, file.PlaylistEntries[0].Path);
                Assert.AreEqual(playlist.PlaylistEntries[0].Title, file.PlaylistEntries[0].Title);
                Assert.AreEqual(playlist.PlaylistEntries[0].Album, file.PlaylistEntries[0].Album);
                Assert.AreEqual(playlist.PlaylistEntries[0].AlbumArtist, file.PlaylistEntries[0].AlbumArtist);

                Assert.AreEqual(playlist.PlaylistEntries[1].Path, file.PlaylistEntries[1].Path);
                Assert.AreEqual(playlist.PlaylistEntries[1].Title, file.PlaylistEntries[1].Title);
                Assert.AreNotEqual(playlist.PlaylistEntries[1].Album, file.PlaylistEntries[1].Album);
                Assert.AreNotEqual(playlist.PlaylistEntries[1].AlbumArtist, file.PlaylistEntries[1].AlbumArtist);
                Assert.IsNull(playlist.PlaylistEntries[1].Album);
                Assert.AreEqual("", file.PlaylistEntries[1].Album);

                Assert.AreEqual(playlist.PlaylistEntries[1].CustomProperties.Count(), file.PlaylistEntries[1].CustomProperties.Count());
                Assert.AreEqual(playlist.PlaylistEntries[1].CustomProperties.Last().Key, file.PlaylistEntries[1].CustomProperties.Last().Key);
                Assert.AreEqual(playlist.PlaylistEntries[1].CustomProperties.Last().Value, file.PlaylistEntries[1].CustomProperties.Last().Value);

                Assert.AreEqual(playlist.PlaylistEntries[2].Path, file.PlaylistEntries[2].Path);
                Assert.AreEqual(playlist.PlaylistEntries[2].Title, file.PlaylistEntries[2].Title);
                Assert.AreEqual(playlist.PlaylistEntries[2].Album, file.PlaylistEntries[2].Album);
                Assert.AreEqual(playlist.PlaylistEntries[2].AlbumArtist, file.PlaylistEntries[2].AlbumArtist);
            }
        }
Exemple #13
0
        public static M3uPlaylist GetFromStream(Stream stream)
        {
            var playlistEntries = new List <M3uPlaylistEntry>();

            var streamReader = new StreamReader(stream);

            var prevLineIsEXTINF = false;
            var title            = string.Empty;
            var id         = string.Empty;
            var name       = string.Empty;
            var logo       = string.Empty;
            var smallLogo  = string.Empty;
            var groupTitle = string.Empty;
            var parentCode = string.Empty;

            while (!streamReader.EndOfStream)
            {
                var line = streamReader.ReadLine();
                line.TrimStart();

                if (line.StartsWith(Directives.EXTINF))
                {
                    if (prevLineIsEXTINF)
                    {
                        throw new ArgumentNullException(
                                  "Every EXTINF tag in a Playlist MUST have an media URI applied to it.");
                    }

                    prevLineIsEXTINF = true;
                    title            = Helpers.GetTitle(line);
                    id         = Helpers.GetProperties(line, Properties.TvgId);
                    name       = Helpers.GetProperties(line, Properties.TvgName);
                    logo       = Helpers.GetProperties(line, Properties.TvgLogo);
                    smallLogo  = Helpers.GetProperties(line, Properties.TvgLogoSmall);
                    groupTitle = Helpers.GetProperties(line, Properties.GroupTitle);
                    parentCode = Helpers.GetProperties(line, Properties.ParentCode);

                    continue;
                }

                if (line.StartsWith("#"))
                {
                    continue;
                }
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                var playListEntry = new M3uPlaylistEntry
                {
                    Id        = id,
                    Title     = title,
                    Logo      = logo,
                    SmallLogo = smallLogo,
                    Name      = name,
                    Uri       = line,
                    Group     = groupTitle,
                    Code      = parentCode
                };

                playlistEntries.Add(playListEntry);

                prevLineIsEXTINF = false;
                title            = string.Empty;
                id         = string.Empty;
                name       = string.Empty;
                logo       = string.Empty;
                smallLogo  = string.Empty;
                groupTitle = string.Empty;
                parentCode = string.Empty;
            }

            var playlist = new M3uPlaylist
            {
                PlaylistEntries = playlistEntries
            };

            return(playlist);
        }
 public string GetFileName(M3uPlaylistEntry entry)
 {
     return($"{Program.StripInvalidFilenameCharacters(entry.Title)}.wav");
 }