Code: Keyzoid.Core.MediaManager


See my Github page for the full source code described below.

A media manager program to create/format/append media files for presentation on the web.

Getting Started

This solution creates and manipulates local media files for presentation on the web. It currently supports two modes: Music and Photography.

Music mode reads local .m3u and .m3u8 playlists (exported from your favorite music player) and creates .json files based on Playlist, Track and Video Model objects. The idea is to take the basic data present in the local playlist and pull out artist and track info and add videos (with thumbmails) by searching YouTube. The output is much more rich, displays nicely on a webpage, and shares well with others.

Photography mode formats raw local images (from your phone or camera) for web viewing, pretties up the titles and file names by using image metadata, and creates .json files based on PhotoAlbum and Picture Model objects. This makes for quicker web publishing by letting you add subjects and tags to your images locally on the file system, quickly and in bulk, and then generating pretty titles and scaled images for web viewing.

Prerequisites

.m3u or .m3u8 playlists and a YouTube API key for Music Mode and a folder with pictures for Photography mode. Photos should have EXIF data tags or subjects for best results.

Installing

Update the appsettings.json file with details of the prerequisites above. You can alter the "mode" appsetting to run in one mode or the other: "Music" or "Photography".

Built With

  • .NET Core - The console application framework used.
  • YouTube API - Google API.

Acknowledgments

The following links proved useful on the journey to creating this solution.

exiv2.org - Standard Exif Tags

Sample Code

public static async Task<IEnumerable<Video>> Search(string searchText, int maxResults = 50)
        {
            try
            {
                var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                {
                    ApiKey = YouTubeApiKey,
                    ApplicationName = YouTubeApplicationName
                });

                var searchListRequest = youtubeService.Search.List("snippet");
                searchListRequest.Q = searchText;
                searchListRequest.MaxResults = maxResults;

                var searchListResponse = await searchListRequest.ExecuteAsync();
                var results = new List<Video>();
                var rand = new Random();

                foreach (var searchResult in searchListResponse.Items)
                {
                    switch (searchResult.Id.Kind)
                    {
                        case "youtube#video":

                            var thumbs = new List<Thumbnail>();
                            var item = new Video
                            {
                                id = searchResult.Id.VideoId,
                                title = searchResult.Snippet.Title,
                                description = searchResult.Snippet.Description
                            };

                            if (searchResult.Snippet.Thumbnails.Default__ != null)
                            {
                                thumbs.Add(new Thumbnail
                                {
                                    type = "default",
                                    url = searchResult.Snippet.Thumbnails.Default__.Url,
                                    height = searchResult.Snippet.Thumbnails.Default__.Height.ToString(),
                                    width = searchResult.Snippet.Thumbnails.Default__.Width.ToString()
                                });
                            }

                            if (searchResult.Snippet.Thumbnails.Medium != null)
                            {
                                thumbs.Add(new Thumbnail
                                {
                                    type = "medium",
                                    url = searchResult.Snippet.Thumbnails.Medium.Url,
                                    height = searchResult.Snippet.Thumbnails.Medium.Height.ToString(),
                                    width = searchResult.Snippet.Thumbnails.Medium.Width.ToString()
                                });
                            }

                            if (searchResult.Snippet.Thumbnails.High != null)
                            {
                                thumbs.Add(new Thumbnail
                                {
                                    type = "high",
                                    url = searchResult.Snippet.Thumbnails.High.Url,
                                    height = searchResult.Snippet.Thumbnails.High.Height.ToString(),
                                    width = searchResult.Snippet.Thumbnails.High.Width.ToString()
                                });
                            }

                            if (thumbs.Any())
                            {
                                item.thumbnails = thumbs.ToArray();
                            }

                            results.Add(item);
                            break;
                    }
                }

                return results;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERROR: Handler exception. Error: {ex.Message}. {ex.StackTrace}.");
                throw;
            }
        }
    }


Tagged: #code


Posted on Nov 05, 2018