Esempio n. 1
0
        public void getStreams(out YoutubeVideoStreamedItem video, out YoutubeVideoStreamedItem audio, int maxHeight = 4096, int maxSamplesPerSecond = 48000)
        {
            video = null;
            audio = null;

            int bestHeight = 0;
            int bestSamplesPerSecond = 0;

            // get best video stream
            foreach (YoutubeVideoStreamedItem item in StreamedItem)
            {
                if (item.StreamType == StreamType.UNKNOWN) continue;

                VideoMetadata metaData = item.Metadata as VideoMetadata;

                if (metaData.Height >= bestHeight && metaData.Height <= maxHeight)
                {
                    if (metaData.Height == bestHeight)
                    {
                        // prefer mp4 over webm
                        if (item.Metadata.MimeType.Equals("video/webm")) continue;

                        // prefer adaptive over normal streams
                        if (item.StreamType == StreamType.VIDEO_AUDIO) continue;
                    }

                    video = item;

                    bestHeight = metaData.Height;
                }
            }

            if (video != null && video.StreamType == StreamType.VIDEO_AUDIO) return;

            // get best matching audio stream
            foreach (YoutubeVideoStreamedItem item in StreamedItem)
            {
                if (item.StreamType == StreamType.UNKNOWN) continue;

                VideoMetadata metaData = item.Metadata as VideoMetadata;

                if (metaData.SamplesPerSecond >= bestSamplesPerSecond && metaData.SamplesPerSecond <= maxSamplesPerSecond)
                {
                    int idx = video.Metadata.MimeType.IndexOf('/');

                    if (!metaData.MimeType.EndsWith(video.Metadata.MimeType.Substring(idx)))
                    {
                        // only use audio stream that matches with the video stream
                        continue;
                    }

                    if (metaData.SamplesPerSecond == bestSamplesPerSecond)
                    {
                        // prefer aac over ogg
                        if (item.Metadata.MimeType.Equals("audio/webm")) continue;
                    }

                    audio = item;

                    bestSamplesPerSecond = metaData.SamplesPerSecond.Value;
                }
            }

        }        
        private void downloadAndMuxStreams(string fullpath, YoutubeVideoStreamedItem videoStream, 
            YoutubeVideoStreamedItem audioStream)
        {                    
            TotalProgress = 0;
            ItemProgressMax = 100;

            Dictionary<String, Object> options = new Dictionary<string, object>();
            options.Add("videoStreamMode", StreamOptions.Copy);
            options.Add("audioStreamMode", StreamOptions.Copy);
                           
            ItemProgress = 0;
            ItemInfo = "Downloading and muxing: " + videoStream.Name;

            try
            {
                OpenVideoArgs openArgs = new OpenVideoArgs(videoStream.Location, null, audioStream.Location, null);

                videoTranscoder.transcode(openArgs, fullpath, CancellationToken, options,muxingProgressCallback);
            }
            catch (Exception e)
            {
                InfoMessages.Add("Error muxing: " + e.Message);

                try
                {
                    File.Delete(fullpath);
                }
                catch (Exception ex)
                {
                    InfoMessages.Add("Error deleting: " + fullpath + " " + ex.Message);
                }
                    
                return;
            }
                    
            ItemProgress = 100;  
        }
        void singleStreamDownload(String fullpath, YoutubeVideoStreamedItem item)
        {
            
            FileStream outFile = null;

            try
            {
                outFile = new FileStream(fullpath, FileMode.Create);
                string mimeType;

                ItemProgressMax = 1;
                ItemProgress = 0;

                ItemInfo = "Downloading: " + fullpath;
                StreamUtils.readHttpRequest(new Uri(item.Location), outFile, out mimeType, CancellationToken, downloadProgressCallback);

                ItemProgressMax = 1;
                ItemProgress = 1;
             
                outFile.Close();
            }
            catch (Exception e)
            {
                InfoMessages.Add("Error downloading: " + fullpath + " " + e.Message);

                if (outFile != null)
                {
                    outFile.Close();
                    File.Delete(fullpath);
                }
                return;
            }

        }
Esempio n. 4
0
        public void getStreams(out YoutubeVideoStreamedItem video, out YoutubeVideoStreamedItem audio, int maxHeight = 4096, int maxSamplesPerSecond = 48000)
        {
            video = null;
            audio = null;

            int bestHeight           = 0;
            int bestSamplesPerSecond = 0;

            // get best video stream
            foreach (YoutubeVideoStreamedItem item in StreamedItem)
            {
                if (item.StreamType == StreamType.UNKNOWN)
                {
                    continue;
                }

                VideoMetadata metaData = item.Metadata as VideoMetadata;

                if (metaData.Height >= bestHeight && metaData.Height <= maxHeight)
                {
                    if (metaData.Height == bestHeight)
                    {
                        // prefer mp4 over webm
                        if (item.Metadata.MimeType.Equals("video/webm"))
                        {
                            continue;
                        }

                        // prefer adaptive over normal streams
                        if (item.StreamType == StreamType.VIDEO_AUDIO)
                        {
                            continue;
                        }
                    }

                    video = item;

                    bestHeight = metaData.Height;
                }
            }

            if (video != null && video.StreamType == StreamType.VIDEO_AUDIO)
            {
                return;
            }

            // get best matching audio stream
            foreach (YoutubeVideoStreamedItem item in StreamedItem)
            {
                if (item.StreamType == StreamType.UNKNOWN)
                {
                    continue;
                }

                VideoMetadata metaData = item.Metadata as VideoMetadata;

                if (metaData.SamplesPerSecond >= bestSamplesPerSecond && metaData.SamplesPerSecond <= maxSamplesPerSecond)
                {
                    int idx = video.Metadata.MimeType.IndexOf('/');

                    if (!metaData.MimeType.EndsWith(video.Metadata.MimeType.Substring(idx)))
                    {
                        // only use audio stream that matches with the video stream
                        continue;
                    }

                    if (metaData.SamplesPerSecond == bestSamplesPerSecond)
                    {
                        // prefer aac over ogg
                        if (item.Metadata.MimeType.Equals("audio/webm"))
                        {
                            continue;
                        }
                    }

                    audio = item;

                    bestSamplesPerSecond = metaData.SamplesPerSecond.Value;
                }
            }
        }