using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Saar.FFmpeg.CSharp;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;
namespace SaarFFmpeg.VideoTest {
public class Program
{
public class VideoFormPanel : Panel
{
object locker = new object();
List<Bitmap> mFrames = new List<Bitmap>();
Bitmap mBmp;
public VideoFormPanel()
{
this.DoubleBuffered = true;
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
}
public void UpdateBitmap(Bitmap bmp)
{
try
{
if (!this.IsHandleCreated || this.IsDisposed) return;
if (this.InvokeRequired)
{
this.Invoke(new Action(() =>
{
UpdateBitmap(bmp);
}));
return;
}
BMP = bmp;
this.Invalidate();
}catch(Exception ee)
{
}
}
public Bitmap BMP
{
get
{
lock (locker)
{
return mBmp;
}
}
set
{
lock (locker)
{
mBmp = value;
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
Bitmap bmp = BMP;
if(bmp != null)
{
e.Graphics.DrawImage(bmp, 0, 0, this.Width, this.Height);
}
base.OnPaint(e);
}
}
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "MP4 File(*.mp4)|*.mp4";
openFile.FilterIndex = 1;
if(openFile.ShowDialog() != DialogResult.OK)
{
return;
}
Form form = new Form();
VideoFormPanel panel = new VideoFormPanel();
panel.Dock = DockStyle.Fill;
form.Controls.Add(panel);
Thread thread = new Thread(new ThreadStart(() => {
var media = new MediaReader(openFile.FileName);
var decoder = media.Decoders.OfType<VideoDecoder>().First();
decoder.OutFormat = new VideoFormat(decoder.InFormat.Width, decoder.InFormat.Height, AVPixelFormat.Bgr24, 4);
VideoFrame frame = new VideoFrame();
while (media.NextFrame(frame, decoder.StreamIndex))
{
Bitmap image = new Bitmap(frame.Format.Width, frame.Format.Height, frame.Format.Strides[0], PixelFormat.Format24bppRgb, frame.Scan0);
DateTime dt = DateTime.Now;
panel.UpdateBitmap(image);
double elapsed = DateTime.Now.Subtract(dt).TotalMilliseconds;
double sleepTime = (1000.0 / media.FramesPerSecond)-elapsed;
if (sleepTime > 0)
Thread.Sleep((int)sleepTime);
if (form.IsDisposed)
break;
}
}));
form.Disposed += (o, e) => { thread.Abort(); };
thread.IsBackground = true;
thread.Start();
Application.Run(form);
}
}
}
SaarFFMPEG: VideoTest as a player
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.