Why after recording a video when playing the avi file everything inside is running fast ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Im recording the video as 25 frames per second .
The timer interval is set to 40ms and the ffmpeg command line set to 25 frames per second.
The video file size is 2326KB and the lnegth is 16 seconds.
But once im playing the video for example on MPC Media Player Classic i see the video running very fast like its on forward mode.
In the video file that have been created on hard disk on details i see : 1920x1080 25 frames per second.
This is the code in Form 1: private void StartRecording_Click(object sender, EventArgs e)
{
ffmp.Start("test.avi", 25);
timer1.Enabled = true;
}

Timer tick event i push frames each 40ms each frame is a screenshot of the entire screen :private void timer1_Tick(object sender, EventArgs e)
{

using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))
{
ffmp.PushFrame(bitmap);
}

}

Then in the ffmpeg class i did :
using System;
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;

namespace ScreenVideoRecorder
{
class Ffmpeg
{
NamedPipeServerStream p;
String pipename = "mytestpipe";
byte[] b;
//int i, j;
System.Diagnostics.Process process;
//IAsyncResult ar;

public Ffmpeg()
{

}

public void Start(string FileName, int BitmapRate)
{
p = new NamedPipeServerStream(pipename, PipeDirection.Out, 1, PipeTransmissionMode.Byte);
b = new byte[1920 * 1080 * 3]; // some buffer for the r g and b of pixels of an image of size 720p
process = new System.Diagnostics.Process();
//process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.FileName = @"D:pipetestpipetestffmpegx86ffmpeg.exe";
process.EnableRaisingEvents = false;
process.StartInfo.WorkingDirectory = @"D:pipetestpipetestffmpegx86";
process.StartInfo.Arguments = @"-f rawvideo -pix_fmt bgr0 -video_size 1920x1080 -i \.pipemytestpipe -map 0 -c:v libx264 -r " + BitmapRate + " " + FileName;
process.Start();
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = false;
//ar = p.BeginWaitForConnection(EndWait,null);
p.WaitForConnection();
}

public void PushFrame(Bitmap bmp)
{

int length;
// Lock the bitmaps 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);
}
}
}

In the ffmpeg arguments string i used a breakpoint and i see that BitmapRate is 25 .
So why when im playing the video everything there is running so fast like its in forward mode ?

View the full article
 
Back
Top