Capturing and Saving Video in Windows 10 Application

Video capturing is one of the common tool used now a days in application development. It is quite obvious a person might be needing to integrate Video capturing in his Windows 10 Application. So, here we are beginning with our Video Capturing application:

In Visual Studio 2015, Start a new project and select Blank app in Universal App category. Before starting any coding, we have to go to package.appxmanifest file of the solution, we need to select Webcam and Video Library under Capabilities.

Then in the MainPage.xaml file we will add the code for button which will be used to capture the video and a media element where video will be shown:

  1. <Button x:Name="Capture_button" Content="Start Capturing Video" HorizontalAlignment="Left" Margin="79,240,0,0" VerticalAlignment="Top" Height="78" Width="169" Click="Capture_button_Click"/>  
  2. <MediaElement x:Name="video" HorizontalAlignment="Left" Height="720" Margin="312,0,0,0" VerticalAlignment="Top" Width="968" AreTransportControlsEnabled="True"/>  
  3. <Button x:Name="Save_button" Content="Save Video" HorizontalAlignment="Left" Margin="79,391,0,0" VerticalAlignment="Top" Height="78" Width="169" Click="save_Video"/>  

After adding the above code in the xaml file, we will go to the methods of the buttons: Capture_button and Save_button and add their functionality. But before adding the functionality to the methods, we need to add the namespaces which will be required in the application:

  1. using Windows.Media.Capture;  
  2. using Windows.Media.Core;  
  3. using Windows.Media.Editing;  
  4. using Windows.Storage;  
  5. using Windows.Storage.Pickers;  
  6. using Windows.Storage.Streams;  
  7. using Windows.UI.Popups;  
  8. using Windows.UI.Xaml;  
  9. using Windows.UI.Xaml.Controls;  

Now, we add the declaration code for two objects of StorageFile class and IRandomAccessStream class which will be used throughout the application:

  1. private StorageFile sf;  
  2. private IRandomAccessStream rs;  

Now, the functionality for Capture_button method which will invoke the default video capturing tool of the system:

  1. private async void Capture_button_Click(object sender, RoutedEventArgs e)  
  2. {  
  3. // Declaring CameraCaptureUI to call the default Video capturing tool in the system  
  4. CameraCaptureUI cc = new CameraCaptureUI();  
  5. cc.VideoSettings.Format = CameraCaptureUIVideoFormat.Mp4;  
  6. cc.VideoSettings.MaxResolution = CameraCaptureUIMaxVideoResolution.HighDefinition;  
  7.   
  8. /* saving the video temporarily in the storage file object 
  9. and setting the streaming source for our media element to show the information*/  
  10. sf = await cc.CaptureFileAsync(CameraCaptureUIMode.Video);  
  11. if (sf != null)  
  12. {  
  13. rs = await sf.OpenAsync(FileAccessMode.Read);  
  14. MediaClip mc = await MediaClip.CreateFromFileAsync(sf);  
  15. MediaComposition mcomp = new MediaComposition();  
  16. mcomp.Clips.Add(mc);  
  17. MediaStreamSource mss = mcomp.GeneratePreviewMediaStreamSource((int) video.ActualWidth, (int)video.ActualHeight);  
  18. video.SetMediaStreamSource(mss);  
  19. }  
  20. }  

Now, as the save functionality we used in the last blog of Capture and save Image, same we will be using here but with minor adjustments for the Video file extensions:

  1. private async void save_Video(object sender, RoutedEventArgs e)  
  2. {  
  3. // Adding try catch block in case of occurence of an exception  
  4. try  
  5. {  
  6. // Creating object of FileSavePicker and adding few values to the properties of the object.  
  7. FileSavePicker fs = new FileSavePicker();  
  8. fs.FileTypeChoices.Add("Video"new List<string>() { ".mp4",".3gp" });  
  9. fs.DefaultFileExtension = ".mp4";  
  10. fs.SuggestedFileName = "Video" + DateTime.Today.ToString();  
  11. fs.SuggestedStartLocation = PickerLocationId.VideosLibrary;  
  12.   
  13. // Using storagefile object defined earlier in above method to save the file using filesavepicker.  
  14. fs.SuggestedSaveFile = sf;  
  15.   
  16. // Saving the file  
  17. var s = await fs.PickSaveFileAsync();  
  18. if (s != null)  
  19. {  
  20. using (var dataReader = new DataReader(rs.GetInputStreamAt(0)))  
  21. {  
  22. await dataReader.LoadAsync((uint)rs.Size);  
  23. byte[] buffer = new byte[(int)rs.Size];  
  24. dataReader.ReadBytes(buffer);  
  25. await FileIO.WriteBytesAsync(s, buffer);  
  26. }  
  27. }  
  28. }  
  29. catch (Exception ex)  
  30. {  
  31. var messageDialog = new MessageDialog("Something went wrong.");  
  32. await messageDialog.ShowAsync();  
  33. }  
  34. }  

Now, run the program and enjoy!

Ebook Download
View all
Learn
View all