Skip to content

Commit

Permalink
Merge pull request #313 from ffmpeginteropx/alpha-grab
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasf authored Oct 5, 2022
2 parents 25eca9a + 93be2ff commit 2d747ea
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
20 changes: 16 additions & 4 deletions Samples/MediaPlayerCPP/MainPage.xaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,27 @@ task<void> MainPage::ExtractFrame()
filePicker->SuggestedStartLocation = PickerLocationId::VideosLibrary;
filePicker->DefaultFileExtension = ".jpg";
filePicker->FileTypeChoices->Insert("Jpeg file", ref new Platform::Collections::Vector<String^>(1, ".jpg"));
filePicker->FileTypeChoices->Insert("Png file", ref new Platform::Collections::Vector<String^>(1, ".png"));
filePicker->FileTypeChoices->Insert("Bmp file", ref new Platform::Collections::Vector<String^>(1, ".bmp"));

// Show file picker so user can select a file
auto file = co_await filePicker->PickSaveFileAsync();
if (file != nullptr)
{
auto stream = co_await file->OpenAsync(FileAccessMode::ReadWrite);
// encode frame as jpeg file
co_await frame->EncodeAsJpegAsync(stream);
stream = nullptr;
auto outputStream = co_await file->OpenAsync(FileAccessMode::ReadWrite);
if (file->FileType == ".jpg")
{
co_await frame->EncodeAsJpegAsync(outputStream);
}
else if (file->FileType == ".png")
{
co_await frame->EncodeAsPngAsync(outputStream);
}
else
{
co_await frame->EncodeAsBmpAsync(outputStream);
}
outputStream = nullptr;

// launch file after creation
auto launched = co_await Windows::System::Launcher::LaunchFileAsync(file);
Expand Down
23 changes: 19 additions & 4 deletions Samples/MediaPlayerCS/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,43 @@ private async void ExtractFrame(object sender, RoutedEventArgs e)

try
{
using (frameGrabber)
using (var frame = await frameGrabber.ExtractVideoFrameAsync(mediaPlayer.PlaybackSession.Position, exactSeek))
{
var filePicker = new FileSavePicker();
filePicker.SettingsIdentifier = "VideoFrame";
filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
filePicker.DefaultFileExtension = ".jpg";
filePicker.FileTypeChoices["Jpeg file"] = new[] { ".jpg" }.ToList();
filePicker.FileTypeChoices["Png file"] = new[] { ".png" }.ToList();
filePicker.FileTypeChoices["Bmp file"] = new[] { ".bmp" }.ToList();

var file = await filePicker.PickSaveFileAsync();
if (file != null)
{
var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite);
await frame.EncodeAsJpegAsync(outputStream);
outputStream.Dispose();
using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
if (file.FileType == ".jpg")
{
await frame.EncodeAsJpegAsync(outputStream);
}
else if (file.FileType == ".png")
{
await frame.EncodeAsPngAsync(outputStream);
}
else
{
await frame.EncodeAsBmpAsync(outputStream);
}
}

bool launched = await Windows.System.Launcher.LaunchFileAsync(file, new LauncherOptions() { DisplayApplicationPicker = false });
if (!launched)
{
await DisplayErrorMessage("File has been created:\n" + file.Path);
}
}
}
frameGrabber?.Dispose();
}
catch (Exception ex)
{
Expand Down
23 changes: 19 additions & 4 deletions Samples/MediaPlayerWinUI/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ private async void ExtractFrame(object sender, RoutedEventArgs e)

try
{
using (frameGrabber)
using (var frame = await frameGrabber.ExtractVideoFrameAsync(mediaPlayer.PlaybackSession.Position, exactSeek))
{
var filePicker = new FileSavePicker();
Expand All @@ -283,21 +284,35 @@ private async void ExtractFrame(object sender, RoutedEventArgs e)
filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
filePicker.DefaultFileExtension = ".jpg";
filePicker.FileTypeChoices["Jpeg file"] = new[] { ".jpg" }.ToList();
filePicker.FileTypeChoices["Png file"] = new[] { ".png" }.ToList();
filePicker.FileTypeChoices["Bmp file"] = new[] { ".bmp" }.ToList();

var file = await filePicker.PickSaveFileAsync();
if (file != null)
{
var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite);
await frame.EncodeAsJpegAsync(outputStream);
outputStream.Dispose();
using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
if (file.FileType == ".jpg")
{
await frame.EncodeAsJpegAsync(outputStream);
}
else if (file.FileType == ".png")
{
await frame.EncodeAsPngAsync(outputStream);
}
else
{
await frame.EncodeAsBmpAsync(outputStream);
}
}

bool launched = await Windows.System.Launcher.LaunchFileAsync(file, new LauncherOptions() { DisplayApplicationPicker = false });
if (!launched)
{
DisplayErrorMessage("File has been created:\n" + file.Path);
}
}
}
frameGrabber?.Dispose();
}
catch (Exception ex)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/VideoFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace winrt::FFmpegInteropX::implementation

encoderValue.SetPixelData(
BitmapPixelFormat::Bgra8,
BitmapAlphaMode::Ignore,
BitmapAlphaMode::Straight,
pixelWidth,
pixelHeight,
72,
Expand Down

0 comments on commit 2d747ea

Please sign in to comment.