I was looking to play some videos using WPF's MediaElement
control, so as a starting point I took some sample XAML code from WPF4 Unleashed (page 661 if you want to look it up).
<Grid>
<MediaElement Name="Video" />
<StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
<Button Name="PlayButton" Background="#55FFFFFF" Height="40">Play</Button>
<Button Name="PauseButton" Background="#55FFFFFF" Height="40">Pause</Button>
<Button Name="ResumeButton" Background="#55FFFFFF" Height="40">Resume</Button>
</StackPanel>
<Grid.Triggers>
<EventTrigger RoutedEvent="Button.Click" SourceName="PlayButton">
<EventTrigger.Actions>
<BeginStoryboard Name="BeginStoryBoard">
<Storyboard>
<MediaTimeline Storyboard.TargetName="Video" Source="Path/To/Out.mp4"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Button.Click" SourceName="PauseButton">
<EventTrigger.Actions>
<PauseStoryboard BeginStoryboardName="BeginStoryBoard" />
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="Button.Click" SourceName="ResumeButton">
<EventTrigger.Actions>
<ResumeStoryboard BeginStoryboardName="BeginStoryBoard" />
</EventTrigger.Actions>
</EventTrigger>
</Grid.Triggers>
</Grid>
I tried this snippet with 2 different videos, one I took from youtube and one I saved using livestreamer (from a Twitch.tv stream). However, while both WMP and VLC could play both videos perfectly fine, the above XAML sample could not play the video from Livestreamer properly (there were a lot of visual artifacts, see below).
What's weird is according to an answer (to this question) on MSDN:
2, If WMP12 is provided, WPF mediaElement can play mp4 without any Codec Packs.
Speaking of codecs, I looked up the codec information for both videos using VLC:
Youtube:
- Codec: H264 - MPEG-4 AVC (part 10) (avc1)
- Resolution: 1920x1080
- Display Resolution: 1920x1080
- Frame rate: 29.970029
Livestreamer:
- Codec: H264 - MPEG-4 AVC (part 10) (h264)
Now this shouldn't really matter because MediaElement
should as I understand have the exact same behavior as WMP.
tl;dr; What is the proper way to play videos using WPF's MediaElement? (if not the one above)
P.S: If you must know, my end goal is to playback streams from twitch.tv and do some visual analysis on the video frames (using OpenCV or some other library).
P.P.S: If I use ffmpeg to re-render/re-encode/re-save the video using: ffmpeg.exe -i Out.mp4 Out_fixed.mp4
, the new version will play fine. Don't know if that helps.