Inheritance: IMediaDataSource, IStoppable
コード例 #1
0
        // Exactly one of manifestUri and manifestPath must be set.
        public static void DownloadAndMux(Uri manifestUri, string manifestPath, string mkvPath, bool isDeterministic, TimeSpan stopAfter,
                                          SetupStop setupStop, DisplayDuration displayDuration)
        {
            string       manifestParentPath = null; // A null indicates a remote manifest file.
            ManifestInfo manifestInfo;

            if (manifestPath != null)
            {
                manifestParentPath = Path.GetDirectoryName(manifestPath);
                Console.WriteLine("Parsing local manifest file: " + manifestPath);
                using (FileStream manifestStream = new FileStream(manifestPath, FileMode.Open)) {
                    manifestInfo = ManifestInfo.ParseManifest(manifestStream, /*manifestUri:*/ new Uri(LOCAL_URL_PREFIX));
                }
            }
            else
            {
                Console.WriteLine("Downloading and parsing manifest: " + manifestUri);
                WebClient webClient = new WebClient();
                using (Stream manifestStream = webClient.OpenRead(manifestUri)) {
                    manifestInfo = ManifestInfo.ParseManifest(manifestStream, manifestUri);
                }
            }
            Console.Write(manifestInfo.GetDescription());

            IList <Track> tracks = new List <Track>();

            foreach (StreamInfo streamInfo in manifestInfo.SelectedStreams)
            {
                foreach (TrackInfo trackInfo in streamInfo.SelectedTracks)
                {
                    tracks.Add(new Track(trackInfo));
                }
            }
            IList <TrackEntry>           trackEntries = new List <TrackEntry>();
            IList <IList <MediaSample> > trackSamples = new List <IList <MediaSample> >();

            for (int i = 0; i < tracks.Count; ++i)
            {
                trackEntries.Add(tracks[i].TrackInfo.TrackEntry);
                trackEntries[i].TrackNumber = (ulong)(i + 1);
                trackSamples.Add(new List <MediaSample>());
            }
            for (int i = 0; i < tracks.Count; i++)
            {
                // TODO: Add a facility to start live streams from a later chunk (it was chunkIndex=10 previously).
                // Our design allows for an empty ChunkList, in case live streams are growing.
                tracks[i].NextStartTime = tracks[i].TrackInfo.Stream.ChunkList.Count == 0 ? 0 :
                                          tracks[i].TrackInfo.Stream.ChunkList[0].StartTime;
            }
            // TODO: Test for live streams (see the StackOverflow question).
            Console.WriteLine("Also muxing selected tracks to MKV: " + mkvPath);
            try {
                if (Directory.GetParent(mkvPath) != null &&
                    !Directory.GetParent(mkvPath).Exists)
                {
                    Directory.GetParent(mkvPath).Create();
                }
            } catch (IOException) {
                // TODO: Add nicer error reporting, without a stack trace.
                throw new Exception("Cannot not create the directory of .mkv: " + mkvPath);
            }
            ulong maxTrackEndTimeHint = manifestInfo.Duration;

            for (int i = 0; i < tracks.Count; ++i)
            {
                IList <ChunkInfo> chunkInfos = tracks[i].TrackInfo.Stream.ChunkList;
                int j = chunkInfos.Count - 1;
                if (j >= 0) // Our design allows for an empty ChunkList.
                {
                    ulong trackDuration = chunkInfos[j].StartTime + chunkInfos[j].Duration;
                    if (maxTrackEndTimeHint < trackDuration)
                    {
                        maxTrackEndTimeHint = trackDuration;
                    }
                }
            }
            // The .muxstate file is approximately 1/5441.43 of the size of the .mkv.
            // The .muxstate file is around 28.088 bytes per second. TODO: Update this after n.
            // Sometimes totalDuration of video is 1156420602, audio is 1156818141 (larger), so we just take the maximum.
            string muxStatePath    = Path.ChangeExtension(mkvPath, "muxstate");
            string muxStateOldPath = muxStatePath + ".old";

            byte[] oldMuxState = null;
            if (File.Exists(muxStatePath)) // False for directories.
            {
                using (FileStream fileStream = new FileStream(muxStatePath, FileMode.Open)) {
                    oldMuxState = ReadFileStream(fileStream);
                }
                if (oldMuxState.Length > 0)
                {
                    // File.Move fails with IOException if the destination already exists.
                    // C# and .NET SUXX: There is no atomic overwrite-move.
                    try {
                        File.Move(muxStatePath, muxStateOldPath);
                    } catch (IOException) {
                        File.Replace(muxStatePath, muxStateOldPath, null, true);
                    }
                }
            }
            DownloadingMediaDataSource source = new DownloadingMediaDataSource(
                tracks, manifestParentPath, manifestInfo.TimeScale,
                manifestInfo.IsLive, (ulong)stopAfter.Ticks, manifestInfo.TotalTicks, displayDuration);

            setupStop(manifestInfo.IsLive, source);
            MuxStateWriter muxStateWriter = new MuxStateWriter(new FileStream(muxStatePath, FileMode.Create));

            try {
                MkvUtils.WriteMkv(mkvPath, trackEntries, source, maxTrackEndTimeHint, manifestInfo.TimeScale, isDeterministic,
                                  oldMuxState, muxStateWriter);
            } finally {
                muxStateWriter.Close();
            }
            File.Delete(muxStatePath);
            if (File.Exists(muxStateOldPath))
            {
                File.Delete(muxStateOldPath);
            }
        }
コード例 #2
0
ファイル: Download.cs プロジェクト: pinglossy/smoothget
        // Exactly one of manifestUri and manifestPath must be set.
        public static void DownloadAndMux(Uri manifestUri, string manifestPath, string mkvPath, bool isDeterministic, TimeSpan stopAfter,
            SetupStop setupStop, DisplayDuration displayDuration)
        {
            string manifestParentPath = null;  // A null indicates a remote manifest file.
              ManifestInfo manifestInfo;
              if (manifestPath != null) {
            manifestParentPath = Path.GetDirectoryName(manifestPath);
            Console.WriteLine("Parsing local manifest file: " + manifestPath);
            using (FileStream manifestStream = new FileStream(manifestPath, FileMode.Open)) {
              manifestInfo = ManifestInfo.ParseManifest(manifestStream, /*manifestUri:*/new Uri(LOCAL_URL_PREFIX));
            }
                        } else {
            Console.WriteLine("Downloading and parsing manifest: " + manifestUri);
            WebClient webClient = new WebClient();
            using (Stream manifestStream = webClient.OpenRead(manifestUri)) {
              manifestInfo = ManifestInfo.ParseManifest(manifestStream, manifestUri);
            }
              }
              Console.Write(manifestInfo.GetDescription());

              IList<Track> tracks = new List<Track>();
              foreach (StreamInfo streamInfo in manifestInfo.SelectedStreams) {
            foreach (TrackInfo trackInfo in streamInfo.SelectedTracks) {
              tracks.Add(new Track(trackInfo));
            }
              }
              IList<TrackEntry> trackEntries = new List<TrackEntry>();
              IList<IList<MediaSample>> trackSamples = new List<IList<MediaSample>>();
              for (int i = 0; i < tracks.Count; ++i) {
            trackEntries.Add(tracks[i].TrackInfo.TrackEntry);
            trackEntries[i].TrackNumber = (ulong)(i + 1);
            trackSamples.Add(new List<MediaSample>());
              }
              for (int i = 0; i < tracks.Count; i++) {
            // TODO: Add a facility to start live streams from a later chunk (it was chunkIndex=10 previously).
            // Our design allows for an empty ChunkList, in case live streams are growing.
            tracks[i].NextStartTime = tracks[i].TrackInfo.Stream.ChunkList.Count == 0 ? 0 :
                                  tracks[i].TrackInfo.Stream.ChunkList[0].StartTime;
              }
              // TODO: Test for live streams (see the StackOverflow question).
              Console.WriteLine("Also muxing selected tracks to MKV: " + mkvPath);
              try {
            if (Directory.GetParent(mkvPath) != null &&
            !Directory.GetParent(mkvPath).Exists)
              Directory.GetParent(mkvPath).Create();
              } catch (IOException) {
            // TODO: Add nicer error reporting, without a stack trace.
            throw new Exception("Cannot not create the directory of .mkv: " + mkvPath);
              }
              ulong maxTrackEndTimeHint = manifestInfo.Duration;
              for (int i = 0; i < tracks.Count; ++i) {
            IList<ChunkInfo> chunkInfos = tracks[i].TrackInfo.Stream.ChunkList;
            int j = chunkInfos.Count - 1;
            if (j >= 0) {  // Our design allows for an empty ChunkList.
              ulong trackDuration = chunkInfos[j].StartTime + chunkInfos[j].Duration;
              if (maxTrackEndTimeHint < trackDuration) maxTrackEndTimeHint = trackDuration;
            }
              }
              // The .muxstate file is approximately 1/5441.43 of the size of the .mkv.
              // The .muxstate file is around 28.088 bytes per second. TODO: Update this after n.
              // Sometimes totalDuration of video is 1156420602, audio is 1156818141 (larger), so we just take the maximum.
              string muxStatePath = Path.ChangeExtension(mkvPath, "muxstate");
              string muxStateOldPath = muxStatePath + ".old";
              byte[] oldMuxState = null;
              if (File.Exists(muxStatePath)) {  // False for directories.
            using (FileStream fileStream = new FileStream(muxStatePath, FileMode.Open)) {
              oldMuxState = ReadFileStream(fileStream);
            }
            if (oldMuxState.Length > 0) {
              // File.Move fails with IOException if the destination already exists.
              // C# and .NET SUXX: There is no atomic overwrite-move.
              try {
            File.Move(muxStatePath, muxStateOldPath);
              } catch (IOException) {
            File.Replace(muxStatePath, muxStateOldPath, null, true);
              }
            }
              }
              DownloadingMediaDataSource source = new DownloadingMediaDataSource(
              tracks, manifestParentPath, manifestInfo.TimeScale,
                            manifestInfo.IsLive, (ulong)stopAfter.Ticks, manifestInfo.TotalTicks, displayDuration);
              setupStop(manifestInfo.IsLive, source);
              MuxStateWriter muxStateWriter = new MuxStateWriter(new FileStream(muxStatePath, FileMode.Create));
              try {
            MkvUtils.WriteMkv(mkvPath, trackEntries, source, maxTrackEndTimeHint, manifestInfo.TimeScale, isDeterministic,
                          oldMuxState, muxStateWriter);
              } finally {
            muxStateWriter.Close();
              }
              File.Delete(muxStatePath);
              if (File.Exists(muxStateOldPath)) {
            File.Delete(muxStateOldPath);
              }
        }