Beispiel #1
0
        private static string GetOutputFileName(MediaMetaData metaData)
        {
            var outputfilename = string.Format("{0} {1}{2}", metaData.Title, metaData.Saison, metaData.Episode).Trim();

            foreach (var c in Path.GetInvalidFileNameChars())
            {
                outputfilename = outputfilename.Replace(c, '_');
            }

            return(outputfilename + ".mp4");
        }
Beispiel #2
0
        public override int Run(string[] remainingArguments)
        {
            // Get media metadata
            var stringData = Encoding.UTF8.GetString(new WebClient().DownloadData(string.Format(UrlGetMediaMetadata, MediaUrl)));
            var metaData   = new MediaMetaData(JObject.Parse(stringData));

            // Get what would be the resulting output filename and check if the file already exists
            var outputFileName = GetOutputFileName(metaData);

            if (File.Exists(outputFileName))
            {
                Console.WriteLine("File \"{0}\" already exists. Skipping.");
                return(0);
            }

            // Read the access_token from the token file
            var accessToken = File.ReadAllText(".toutv_token");

            // Exhange the access_token for a user claims
            var request1 = WebRequest.CreateHttp(string.Format(UrlGetClaims, accessToken));

            request1.PreAuthenticate = true;
            request1.Headers[HttpRequestHeader.Authorization] = "Bearer " + accessToken;
            request1.Accept    = "application/json";
            request1.UserAgent = UserAgent;
            request1.Host      = "services.radio-canada.ca";

            var o     = JObject.Parse(new StreamReader(request1.GetResponse().GetResponseStream()).ReadToEnd());
            var claim = o["claims"].Value <string>();

            // Get the media URL
            var request2 = WebRequest.CreateHttp(string.Format(UrlGetMediaPlaylist, metaData.MediaId, claim));

            request2.PreAuthenticate = true;
            request2.Headers[HttpRequestHeader.Authorization] = "Bearer " + accessToken;
            request2.UserAgent = UserAgent;
            request2.Host      = "services.radio-canada.ca";

            o = JObject.Parse(new StreamReader(request2.GetResponse().GetResponseStream()).ReadToEnd());
            var url = o["url"].Value <string>();

            // Start a ffmpeg process downloading the file
            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName  = "ffmpeg",
                    Arguments = string.Format("-i \"{0}\" -movflags faststart -sn -c copy \"{1}\"", url, outputFileName)
                }
            };

            try
            {
                process.Start();
            }
            catch (Win32Exception ex)
            {
                Console.WriteLine("[ERROR] Could not find ffmpeg installed on your system.");
                return(0);
            }

            process.WaitForExit();

            return(0);
        }