Skip to content

Instantly share code, notes, and snippets.

@ShravanJ

ShravanJ/gist.cs Secret

Last active February 6, 2019 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShravanJ/47eae4f1d4ab36c74f014ccd6c9dbf04 to your computer and use it in GitHub Desktop.
Save ShravanJ/47eae4f1d4ab36c74f014ccd6c9dbf04 to your computer and use it in GitHub Desktop.
public async Task<Stream> CompressVideo(MediaFile file)
{
string exportPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string exportFilePath = Path.Combine(exportPath, "compressed_video.mp4");
System.Diagnostics.Debug.WriteLine("Export path: " + exportPath);
System.Diagnostics.Debug.WriteLine("Export file path: " + exportFilePath);
var asset = AVAsset.FromUrl(NSUrl.FromFilename(file.Path));
AVAssetExportSession export = new AVAssetExportSession(asset, AVAssetExportSessionPreset.MediumQuality);
System.Diagnostics.Debug.WriteLine("Created export object");
export.OutputUrl = NSUrl.FromFilename(exportFilePath);
export.OutputFileType = AVFileType.Mpeg4;
export.ShouldOptimizeForNetworkUse = true;
System.Diagnostics.Debug.WriteLine("Preparing to export");
await runExportAsync(export);
Stream exportStream = File.OpenRead(exportFilePath);
System.Diagnostics.Debug.WriteLine("Stream read from file");
return exportStream;
}
private async Task RunExportAsync(AVAssetExportSession exp)
{
await exp.ExportTaskAsync();
if (exp.Status == AVAssetExportSessionStatus.Completed)
{
System.Diagnostics.Debug.WriteLine("Finished export");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment