ReadAllStreams() public static method

Uses ffprobe/avprobe to query all audio, video and subtitle streams in a container file.
public static ReadAllStreams ( String filename ) : List
filename String
return List
コード例 #1
0
        /// <summary>
        /// This function searches a container file by streamType. Let's say you want to extract an audio stream from
        /// a file with two audio streams. To identify the right one, the key "stream" is searched in "properties". The
        /// value for this key should be an index (this index refers to an entry of an array created by "StreamInfo.ReadAllStreams()",
        /// but not to the actual in-file stream for ffmpeg).
        ///
        /// In case there is no property given, the first stream of this type is selected (in array from "StreamInfo.ReadAllStreams()").
        ///
        /// </summary>
        /// <returns>The stream info describing the requested stream</returns>
        /// <param name="filename">Filename.</param>
        /// <param name="properties">Properties.</param>
        /// <param name="streamType">Stream type.</param>
        public static StreamInfo ChooseStreamInfo(String filename, Dictionary <String, String> properties, StreamInfo.StreamType streamType)
        {
            List <StreamInfo> allStreams = StreamInfo.ReadAllStreams(filename);

            // find first stream of given type in file (after that we also know whether the file has any of these streams)
            int firstSuitableStream = -1;

            for (int i = 0; i < allStreams.Count; i++)
            {
                if (allStreams [i].StreamTypeValue == streamType)
                {
                    firstSuitableStream = i;
                    break;
                }
            }

            if (firstSuitableStream == -1)
            {
                throw new Exception("Container file \"" + filename + "\" does not contain any " + streamType.GetPlainString() + " streams");
            }


            int    streamIndex = 0;
            String streamIndexDictionaryString = "";

            if (properties.TryGetValue("stream", out streamIndexDictionaryString))
            {
                try {
                    streamIndex = Int32.Parse(streamIndexDictionaryString);
                } catch (Exception) {
                    throw new Exception("Stream property is not an integer.");
                }

                if (streamIndex < 0 || streamIndex >= allStreams.Count || allStreams [streamIndex].StreamTypeValue != streamType)
                {
                    throw new Exception("Stream with index " + streamIndex + " is not a " + streamType.GetPlainString() + " stream (but stream at index " + firstSuitableStream + " is).");
                }
            }
            else
            {
                streamIndex = firstSuitableStream;
            }

            return(allStreams [streamIndex]);
        }
コード例 #2
0
        public void ExportData(Settings settings, InfoProgress progressInfo)
        {
            var activeCardList = GetActiveCards();

            progressInfo.AddSection("Exporting text file", 1);
            progressInfo.AddSection("Exporting snapshots", activeCardList.Count);
            progressInfo.AddSection("Exporting audio files", activeCardList.Count);
            if (settings.NormalizeAudio)
            {
                progressInfo.AddSection("Normalize audio files", activeCardList.Count);
            }
            progressInfo.Update();

            ExportTextFile(activeCardList, settings, progressInfo);

            progressInfo.ProcessedSteps(1);

            var cardSnapshotNameTupleList = new List <Tuple <CardInfo, String> >(activeCardList.Count);
            var cardAudioNameTupleList    = new List <Tuple <CardInfo, String> >(activeCardList.Count);

            foreach (var cardInfo in activeCardList)
            {
                cardSnapshotNameTupleList.Add(new Tuple <CardInfo, String>(cardInfo, GetSnapshotFileName(settings, cardInfo)));
                cardAudioNameTupleList.Add(new Tuple <CardInfo, String>(cardInfo, GetAudioFileName(settings, cardInfo)));
            }

            if (progressInfo.Cancelled)
            {
                return;
            }

            // extract images
            String snapshotsPath = settings.OutputDirectoryPath + Path.DirectorySeparatorChar + settings.DeckName + "_snapshots" + Path.DirectorySeparatorChar;

            UtilsCommon.ClearDirectory(snapshotsPath);
            WorkerSnapshot.ExtractSnaphots(settings, snapshotsPath, cardSnapshotNameTupleList, progressInfo);

            if (progressInfo.Cancelled)
            {
                return;
            }

            // extract audio
            String audioPath = settings.OutputDirectoryPath + Path.DirectorySeparatorChar + settings.DeckName + "_audio" + Path.DirectorySeparatorChar;

            UtilsCommon.ClearDirectory(audioPath);
            WorkerAudio.ExtractAudio(settings, audioPath, cardAudioNameTupleList, progressInfo);

            if (progressInfo.Cancelled)
            {
                return;
            }

            if (settings.NormalizeAudio)
            {
                // normalize all audio files
                foreach (var entry in cardAudioNameTupleList)
                {
                    if (progressInfo.Cancelled)
                    {
                        return;
                    }
                    progressInfo.ProcessedSteps(1);

                    var cardInfo = entry.Item1;
                    if (!cardInfo.HasAudio())
                    {
                        continue;
                    }

                    var filepath         = audioPath + entry.Item2;
                    var audioStreamInfos = StreamInfo.ReadAllStreams(filepath);
                    audioStreamInfos.RemoveAll(streamInfo => streamInfo.StreamTypeValue != StreamInfo.StreamType.ST_AUDIO);
                    if (audioStreamInfos.Count != 1)
                    {
                        Console.WriteLine("Skipped normalizing file \"{0}\" because it contains {1} audio streams", filepath, audioStreamInfos.Count);
                        continue;
                    }
                    try {
                        UtilsAudio.NormalizeAudio(filepath, audioStreamInfos[0]);
                    } catch (Exception e) {
                        Console.WriteLine(e.ToString());
                        continue;
                    }
                }
            }
        }