getting mulitple images from a single stream piped

2019-07-22 20:33发布

问题:

I start a process to retrieve a few frames from a video file with ffmpeg,

ffmpeg -i "<videofile>.mp4" -frames:v 10 -f image2pipe pipe:1

and pipe the images to stdout -

var cmd = Process.Start(p);
var stream = cmd.StandardOutput.BaseStream;
var img = Image.FromStream(stream);

Getting the first image this way works, but how do I get all of them?

回答1:

OK this was gobspackingly easy, kind of embarrassed I asked here. I'll post the answer in case it will help anyone else.

The first few bytes in the stream will be repeated every time there is a new image. I guessed the first 8 would do and voila.

    static IEnumerable<Image> GetThumbnails(Stream stream)
    {
        byte[] allImages;
        using (var ms = new MemoryStream())
        {
            stream.CopyTo(ms);
            allImages = ms.ToArray();
        }
        var bof = allImages.Take(8).ToArray(); //??
        var prevOffset = -1;
        foreach (var offset in GetBytePatternPositions(allImages, bof))
        {
            if (prevOffset > -1)
                yield return GetImageAt(allImages, prevOffset, offset);
            prevOffset = offset;
        }
        if (prevOffset > -1)
            yield return GetImageAt(allImages, prevOffset, allImages.Length);
    }

    static Image GetImageAt(byte[] data, int start, int end)
    {
        using (var ms = new MemoryStream(end - start))
        {
            ms.Write(data, start, end - start);
            return Image.FromStream(ms);
        }
    }

    static IEnumerable<int> GetBytePatternPositions(byte[] data, byte[] pattern)
    {
        var dataLen = data.Length;
        var patternLen = pattern.Length - 1;
        int scanData = 0;
        int scanPattern = 0;
        while (scanData < dataLen)
        {
            if (pattern[0] == data[scanData])
            {
                scanPattern = 1;
                scanData++;
                while (pattern[scanPattern] == data[scanData])
                {
                    if (scanPattern == patternLen)
                    {
                        yield return scanData - patternLen;
                        break;
                    }
                    scanPattern++;
                    scanData++;
                }
            }
            scanData++;
        }
    }