randomuser1
randomuser1

Reputation: 2803

Is there a way of using ffmpeg in c# app?

I'm using the ffmpeg.org and when I run ffmpeg -y -f vfwcap -r 25 -i 0 out.mp4 in command line I can grab the video from my webcam and write it to the out.mp4 file. However, I can't see that stream anywhere. I thought about writing some simple wrapper in c# that is built on ffmpeg functionality, so far I found post mentioned on Stack before, but there's nothing about displaying the data live (instead of saving it into the file). Does anyone have any experience with it? Can I for example 'draw' the received data from webcam on a picture box or on some other component? Thanks!

Upvotes: 0

Views: 1955

Answers (2)

Ronald S. Bultje
Ronald S. Bultje

Reputation: 11184

One of the comments in your linked post says this:

How about writing a C++/CLI wrapper around ffmpeg's native interface and then calling your wrapper interface from your application?

I think this is exactly what you want to do. (Note that FFmpeg has worked fine for years in recent versions of Visual Studio, so the response in the linked post to this comment doesn't apply.)

You would basically create a camera input (this lives in libavdevice), and then you would encode this to h264 in a mp4 container (see output_example.c). To get a live display, you would take the data generated from the vfwcap source, decode it (using the "rawvideo" decoder in libavcodec). This gives you a AVFrame, which has the data pointers to display the image in any native UI element in your application, typically using direct3d or opengl. Read the documentation to learn more about all of this.

Upvotes: 2

aergistal
aergistal

Reputation: 31229

You could use a MediaElement or MediaPlayer control.

MediaElement is a UIElement that is supported by the Layout and can be consumed as the content of many controls. It is also usable in Extensible Application Markup Language (XAML) as well as code. MediaPlayer, on the other hand, is designed for Drawing objects and lacks layout support. Media loaded using a MediaPlayer can only be presented using a VideoDrawing or by directly interacting with a DrawingContext. MediaPlayer cannot be used in XAML.

  • MediaElement

Sample XAML:

<MediaElement Source="path\to\out.mp4" Name="myMediaElement" 
     Width="450" Height="250" LoadedBehavior="Manual" UnloadedBehavior="Stop" Stretch="Fill" 
     MediaOpened="Element_MediaOpened" MediaEnded="Element_MediaEnded"/>
  • MediaPlayer
    // 
    // Create a VideoDrawing. 
    //      
    MediaPlayer player = new MediaPlayer();

    player.Open(new Uri(@"sampleMedia\xbox.wmv", UriKind.Relative));

    VideoDrawing aVideoDrawing = new VideoDrawing();

    aVideoDrawing.Rect = new Rect(0, 0, 100, 100);

    aVideoDrawing.Player = player;

    // Play the video once.
    player.Play();       

Multimedia Overview on MSDN

Upvotes: 1

Related Questions