为什么当使用ffmpeg从图像中实时创建avi视频文件时,avi文件正在播放紫色嘈杂的颜色?

时间:2015-06-29 01:07:43

标签: c# .net winforms ffmpeg

这是我前段时间做过的Ffmpeg课程

using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.IO.Pipes;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using DannyGeneral;

namespace Manager
{
    class Ffmpeg
    {
        NamedPipeServerStream p;
        String pipename = "mytestpipe";
        System.Diagnostics.Process process;
        string ffmpegFileName = "ffmpeg.exe";
        string workingDirectory;

        public Ffmpeg()
        {
            workingDirectory = Path.GetDirectoryName(Application.ExecutablePath);
            Logger.Write("workingDirectory: " + workingDirectory);
            if (!Directory.Exists(workingDirectory))
            {
                Directory.CreateDirectory(workingDirectory);
            }
            ffmpegFileName = Path.Combine(workingDirectory, ffmpegFileName);
            Logger.Write("FfmpegFilename: " + ffmpegFileName);
        }

        public void Start(string pathFileName, int BitmapRate)
        {
            try
            {

                string outPath = pathFileName;
                p = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, PipeTransmissionMode.Byte);

                ProcessStartInfo psi = new ProcessStartInfo();
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                psi.UseShellExecute = false;
                psi.CreateNoWindow = false;
                psi.FileName = ffmpegFileName;
                psi.WorkingDirectory = workingDirectory;
                psi.Arguments = @"-f rawvideo -pix_fmt yuv420p -video_size 1920x1080 -i \\.\pipe\mytestpipe -map 0 -c:v mpeg4 -r " + BitmapRate + " " + outPath;
                process = Process.Start(psi);
                process.EnableRaisingEvents = false;
                psi.RedirectStandardError = true;
                p.WaitForConnection();
            }
            catch (Exception err)
            {
                Logger.Write("Exception Error: " + err.ToString());
            }
        }

        public void PushFrame(Bitmap bmp)
        {
            try
            {
                int length;
                // Lock the bitmap's bits.
                //bmp = new Bitmap(1920, 1080);
                Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
                //Rectangle rect = new Rectangle(0, 0, 1280, 720);
                System.Drawing.Imaging.BitmapData bmpData =
                    bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly,
                    bmp.PixelFormat);

                int absStride = Math.Abs(bmpData.Stride);
                // Get the address of the first line.
                IntPtr ptr = bmpData.Scan0;

                // Declare an array to hold the bytes of the bitmap.
                //length = 3 * bmp.Width * bmp.Height;
                length = absStride * bmpData.Height;
                byte[] rgbValues = new byte[length];

                //Marshal.Copy(ptr, rgbValues, 0, length);
                int j = bmp.Height - 1;
                for (int i = 0; i < bmp.Height; i++)
                {
                    IntPtr pointer = new IntPtr(bmpData.Scan0.ToInt32() + (bmpData.Stride * j));
                    System.Runtime.InteropServices.Marshal.Copy(pointer, rgbValues, absStride * (bmp.Height - i - 1), absStride);
                    j--;
                }
                p.Write(rgbValues, 0, length);
                bmp.UnlockBits(bmpData);
            }
            catch(Exception err)
            {
                Logger.Write("Error: " + err.ToString());
            }

        }

        public void Close()
        {
            p.Close();
        }
    }
}

我在按钮点击事件中使用form1:

private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

directroy屏幕截图是我在timer1 tick事件中每隔100ms拍摄一次屏幕截图:

    ScreenShot shot = new ScreenShot();
    public static int counter = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
        counter++;
        shot.GetScreenShot(@"e:\screenshots\", "screenshot");
        if (counter == 1200)
        {
            timer1.Stop();
        }
    }

我从ScreenShot类中调用方法PushFrame,我保存了屏幕截图。

Ffmpeg fmpeg;

然后:

fmpeg = new Ffmpeg();
fmpeg.Start(@"e:\screenshots\test.avi", 25);

public Bitmap GetScreenShot(string folder, string name)
    {
        _screenShot = new Bitmap(GetScreen());
        System.GC.Collect();
        System.GC.WaitForPendingFinalizers();
        string ingName = folder + name + Elgato_Video_Capture.counter.ToString("D6") + ".bmp";
        _screenShot.Save(ingName);
        fmpeg.PushFrame(_screenShot);
        _screenShot.Dispose();

        return _screenShot;
    }

硬盘上的所有图像都很好我可以编辑/打开它们并观察它们没有问题。 它们的大小也相同。

结果是一个大的avi文件1.08 GB大小。 但是当我玩它的时候,我看到许多窗户内部非常快速地运行并且都涂有嘈杂的紫色。

播放视频文件时的屏幕截图:

avi file playing screenshot

我认为问题出在Ffmpeg类的某处,我给ffmpeg.exe提供了参数

psi.Arguments = @"-f rawvideo -pix_fmt yuv420p -video_size 1920x1080 -i \\.\pipe\mytestpipe -map 0 -c:v mpeg4 -r " + BitmapRate + " " + outPath;

不确定是什么让这个avi文件看起来像那样。

这是我得到的结果的视频文件:https://www.youtube.com/watch?v=fdxPus-Xv1k&feature=youtu.be

0 个答案:

没有答案