public async Task CreateFromStream_Default() { Uri uri = new Uri(Constants.DownloadUriSource); Assert.IsNotNull(uri); StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync(Constants.DownloadStreamedFileName, uri, null); Assert.IsNotNull(file); IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read); Assert.IsNotNull(readStream); // CreateFromStreamAsync should return valid FFmpegInteropMSS object which generates valid MediaStreamSource object FFmpegInteropMSS FFmpegMSS = await FFmpegInteropMSS.CreateFromStreamAsync(readStream); Assert.IsNotNull(FFmpegMSS); MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource(); Assert.IsNotNull(mss); // Based on the provided media, check if the following properties are set correctly Assert.AreEqual(true, mss.CanSeek); Assert.AreEqual(0, mss.BufferTime.TotalMilliseconds); Assert.AreEqual(Constants.DownloadUriLength, mss.Duration.TotalMilliseconds); }
private async Task OpenLocalFile(StorageFile file) { currentFile = file; mediaPlayer.Source = null; // Open StorageFile as IRandomAccessStream to be passed to FFmpegInteropMSS IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read); try { StorageApplicationPermissions.FutureAccessList.Clear(); StorageApplicationPermissions.FutureAccessList.Add(file); // Instantiate FFmpegInteropMSS using the opened local file stream FFmpegMSS = await FFmpegInteropMSS.CreateFromStreamAsync(readStream, Config); var tags = FFmpegMSS.MetadataTags.ToArray(); if (AutoCreatePlaybackItem) { CreatePlaybackItemAndStartPlaybackInternal(); } else { playbackItem = null; } } catch (Exception ex) { await DisplayErrorMessage(ex.Message); } }
private async Task <FFmpegInteropMSS> CreateMediaSourceAndPlayer(MediaPlayerElement playerElement, FFmpegInteropMSS ffmpegMss, Clip clip) { if (ffmpegMss != null) { ffmpegMss.Dispose(); } FFmpegInteropConfig conf = new FFmpegInteropConfig(); conf.StreamBufferSize = BufferSizeInBytes; MediaPlayer player = playerElement.MediaPlayer; if (player == null) { player = CreatePlayer(); } using (var stream = await clip.ClipFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { ffmpegMss = await FFmpegInteropMSS.CreateFromStreamAsync(stream, conf); } player.Source = ffmpegMss.CreateMediaPlaybackItem(); playerElement.SetMediaPlayer(player); return(ffmpegMss); }
public async Task CreateFromStream_Bad_Input() { Uri uri = new Uri("ms-appx:///test.txt"); Assert.IsNotNull(uri); StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(uri); Assert.IsNotNull(file); IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read); Assert.IsNotNull(readStream); // CreateFromStreamAsync should throw since test.txt is not a valid media file try { await FFmpegInteropMSS.CreateFromStreamAsync(readStream); Assert.Fail("Expected exception"); } catch (Exception) { } }
public async Task GetThumbnailFromMedia() { // Create a stream from the resource URI that we have var uri = new Uri("ms-appx:///silence with album art.mp3"); var file = await StorageFile.GetFileFromApplicationUriAsync(uri); var stream = await file.OpenAsync(FileAccessMode.Read); // CreateFFmpegInteropMSSFromUri should return null if uri is blank with default parameter FFmpegInteropMSS FFmpegMSS = await FFmpegInteropMSS.CreateFromStreamAsync(stream); Assert.IsNotNull(FFmpegMSS); var thumbnailData = FFmpegMSS.ExtractThumbnail(); Assert.IsNotNull(thumbnailData); // Verify that we have a valid bitmap using (IRandomAccessStream thumbnailstream = thumbnailData.Buffer.AsStream().AsRandomAccessStream()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(thumbnailstream); var bitmap = await decoder.GetFrameAsync(0); Assert.IsNotNull(bitmap); } }
public async Task CreateFromStream_Null() { // CreateFromStreamAsync should throw if stream is null with default parameter try { await FFmpegInteropMSS.CreateFromStreamAsync(null); Assert.Fail("Expected exception"); } catch (Exception) { } }
private async void OpenLocalFile(object sender, RoutedEventArgs e) { FileOpenPicker filePicker = new FileOpenPicker(); filePicker.ViewMode = PickerViewMode.Thumbnail; filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary; filePicker.FileTypeFilter.Add("*"); // Show file picker so user can select a file StorageFile file = await filePicker.PickSingleFileAsync(); if (file != null) { currentFile = file; mediaElement.Stop(); // Open StorageFile as IRandomAccessStream to be passed to FFmpegInteropMSS IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read); try { // Instantiate FFmpegInteropMSS using the opened local file stream FFmpegMSS = await FFmpegInteropMSS.CreateFromStreamAsync(readStream, Config); var source = FFmpegMSS.CreateMediaPlaybackItem(); if (source != null) { // Pass MediaStreamSource to Media Element mediaElement.SetPlaybackSource(source); // Close control panel after file open Splitter.IsPaneOpen = false; } else { DisplayErrorMessage("Cannot open media"); } } catch (Exception ex) { DisplayErrorMessage(ex.Message); } } }
private async void OpenLocalFile(object sender, RoutedEventArgs e) { FileOpenPicker filePicker = new FileOpenPicker(); filePicker.ViewMode = PickerViewMode.Thumbnail; filePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary; filePicker.FileTypeFilter.Add("*"); // Show file picker so user can select a file StorageFile file = await filePicker.PickSingleFileAsync(); if (file != null) { currentFile = file; mediaElement.Stop(); // Open StorageFile as IRandomAccessStream to be passed to FFmpegInteropMSS IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read); try { // Instantiate FFmpegInteropMSS using the opened local file stream FFmpegMSS = await FFmpegInteropMSS.CreateFromStreamAsync(readStream, Config); var tags = FFmpegMSS.MetadataTags.ToArray(); if (AutoCreatePlaybackItem) { CreatePlaybackItemAndStartPlaybackInternal(); } else { playbackItem = null; } } catch (Exception ex) { DisplayErrorMessage(ex.Message); } } }
public static async Task <VideoStreamInfo> GetVideoInfoAsync(this StorageFile file) { try { FFmpegMSS = await FFmpegInteropMSS .CreateFromStreamAsync(await file.OpenReadAsync(), Helper.FFmpegConfig); var MssTest = FFmpegMSS.GetMediaStreamSource(); return(FFmpegMSS.VideoStream); } catch { } finally { try { FFmpegMSS.Dispose(); FFmpegMSS = null; } catch { } } return(null); }
public static async Task <TimeSpan> GetDurationAsync(this StorageFile file) { try { FFmpegMSS = await FFmpegInteropMSS .CreateFromStreamAsync(await file.OpenReadAsync(), Helper.FFmpegConfig); var MssTest = FFmpegMSS.GetMediaStreamSource(); return(FFmpegMSS.Duration); } catch { } finally { try { FFmpegMSS.Dispose(); FFmpegMSS = null; } catch { } } return(TimeSpan.Zero); }
public async Task CreateFromStream_Options() { Uri uri = new Uri(Constants.DownloadUriSource); Assert.IsNotNull(uri); StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync(Constants.DownloadStreamedFileName, uri, null); Assert.IsNotNull(file); IRandomAccessStream readStream = await file.OpenAsync(FileAccessMode.Read); Assert.IsNotNull(readStream); // Setup options PropertySet to configure FFmpeg PropertySet options = new PropertySet(); options.Add("rtsp_flags", "prefer_tcp"); options.Add("stimeout", 100000); Assert.IsNotNull(options); // CreateFromStreamAsync should return valid FFmpegInteropMSS object which generates valid MediaStreamSource object FFmpegInteropMSS FFmpegMSS = await FFmpegInteropMSS.CreateFromStreamAsync(readStream, new FFmpegInteropConfig { FFmpegOptions = options }); Assert.IsNotNull(FFmpegMSS); // Validate the metadata Assert.AreEqual(FFmpegMSS.AudioStreams[0].CodecName.ToLowerInvariant(), "aac"); Assert.AreEqual(FFmpegMSS.VideoStream.CodecName.ToLowerInvariant(), "h264"); MediaStreamSource mss = FFmpegMSS.GetMediaStreamSource(); Assert.IsNotNull(mss); // Based on the provided media, check if the following properties are set correctly Assert.AreEqual(true, mss.CanSeek); Assert.AreEqual(0, mss.BufferTime.TotalMilliseconds); Assert.AreEqual(Constants.DownloadUriLength, mss.Duration.TotalMilliseconds); }
async Task <StorageFile> ConvertVideo(StorageFile inputFile, Size?imageSize, Rect?rectSize) { try { var outputFile = await GenerateRandomOutputFile(); if (inputFile != null && outputFile != null) { MediaProfile = await MediaEncodingProfile.CreateFromFileAsync(inputFile); FFmpegMSS = await FFmpegInteropMSS .CreateFromStreamAsync(await inputFile.OpenReadAsync(), Helper.FFmpegConfig); Mss = FFmpegMSS.GetMediaStreamSource(); if (!IsStoryVideo) { if (Mss.Duration.TotalSeconds > 59) { Transcoder.TrimStartTime = StartTime; Transcoder.TrimStopTime = StopTime; } var max = Math.Max(FFmpegMSS.VideoStream.PixelHeight, FFmpegMSS.VideoStream.PixelWidth); if (max > 1920) { max = 1920; } if (imageSize == null) { MediaProfile.Video.Height = (uint)max; MediaProfile.Video.Width = (uint)max; } else { MediaProfile.Video.Height = (uint)imageSize.Value.Height; MediaProfile.Video.Width = (uint)imageSize.Value.Width; } } else { if (Mss.Duration.TotalSeconds > 14.9) { Transcoder.TrimStartTime = StartTime; Transcoder.TrimStopTime = StopTime; } //var max = Math.Max(FFmpegMSS.VideoStream.PixelHeight, FFmpegMSS.VideoStream.PixelWidth); var size = Helpers.AspectRatioHelper.GetAspectRatioX(FFmpegMSS.VideoStream.PixelWidth, FFmpegMSS.VideoStream.PixelHeight); //if (max > 1920) // max = 1920; MediaProfile.Video.Height = (uint)size.Height; MediaProfile.Video.Width = (uint)size.Width; } var transform = new VideoTransformEffectDefinition { Rotation = MediaRotation.None, OutputSize = imageSize.Value, Mirror = MediaMirroringOptions.None, CropRectangle = rectSize == null ? Rect.Empty : rectSize.Value }; Transcoder.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties); var preparedTranscodeResult = await Transcoder .PrepareMediaStreamSourceTranscodeAsync(Mss, await outputFile.OpenAsync(FileAccessMode.ReadWrite), MediaProfile); Transcoder.VideoProcessingAlgorithm = MediaVideoProcessingAlgorithm.Default; if (preparedTranscodeResult.CanTranscode) { var progress = new Progress <double>(ConvertProgress); await preparedTranscodeResult.TranscodeAsync().AsTask(Cts.Token, progress); ConvertComplete(outputFile); return(outputFile); } else { preparedTranscodeResult.FailureReason.ToString().ShowMsg(); } } } catch (Exception ex) { ex.PrintException().ShowMsg("ConvertVideo"); } return(null); }
/*public*/ async void UploadSingleVideo(StorageFile File, StorageFile thumbnail, string caption) { Caption = caption; Thumbnail = thumbnail; if (!string.IsNullOrEmpty(Caption)) { Caption = Caption.Replace("\r", "\n"); } try { FFmpegMSS = await FFmpegInteropMSS .CreateFromStreamAsync(await File.OpenReadAsync(), FFmpegConfig); Mss = FFmpegMSS.GetMediaStreamSource(); Duration = Mss.Duration.TotalSeconds; await Task.Delay(250); FFmpegMSS = null; Mss = null; } catch { } UploadId = GenerateUploadId(); try { var cacheFolder = await SessionHelper.LocalFolder.GetFolderAsync("Cache"); NotifyFile = await cacheFolder.CreateFileAsync(15.GenerateRandomStringStatic() + ".jpg"); NotifyFile = await File.CopyAsync(cacheFolder); } catch { } var photoHashCode = Path.GetFileName(File.Path ?? $"C:\\{13.GenerateRandomStringStatic()}.jpg").GetHashCode(); var photoEntityName = $"{UploadId}_0_{photoHashCode}"; var instaUri = GetUploadPhotoUri(UploadId, photoHashCode); var device = InstaApi.GetCurrentDevice(); var httpRequestProcessor = InstaApi.HttpRequestProcessor; BackgroundUploader BGU = new BackgroundUploader(); var cookies = httpRequestProcessor.HttpHandler.CookieContainer.GetCookies(httpRequestProcessor.Client.BaseAddress); var strCookies = string.Empty; foreach (Cookie cook in cookies) { strCookies += $"{cook.Name}={cook.Value}; "; } // header haye asli in ha hastan faghat BGU.SetRequestHeader("Cookie", strCookies); var r = InstaApi.HttpHelper.GetDefaultRequest(HttpMethod.Post, instaUri, device); foreach (var item in r.Headers) { BGU.SetRequestHeader(item.Key, string.Join(" ", item.Value)); } var photoUploadParamsObj = new JObject { { "upload_id", UploadId }, { "media_type", "1" }, { "retry_context", GetRetryContext() }, { "image_compression", "{\"lib_name\":\"moz\",\"lib_version\":\"3.1.m\",\"quality\":\"95\"}" }, }; var photoUploadParams = JsonConvert.SerializeObject(photoUploadParamsObj); var openedFile = await File.OpenAsync(FileAccessMode.Read); BGU.SetRequestHeader("X-Entity-Type", "image/jpeg"); BGU.SetRequestHeader("Offset", "0"); BGU.SetRequestHeader("X-Instagram-Rupload-Params", photoUploadParams); BGU.SetRequestHeader("X-Entity-Name", photoEntityName); BGU.SetRequestHeader("X-Entity-Length", openedFile.AsStream().Length.ToString()); BGU.SetRequestHeader("X_FB_PHOTO_WATERFALL_ID", Guid.NewGuid().ToString()); BGU.SetRequestHeader("Content-Transfer-Encoding", "binary"); BGU.SetRequestHeader("Content-Type", "application/octet-stream"); Debug.WriteLine("----------------------------------------Start upload----------------------------------"); //var uploadX = await BGU.CreateUploadAsync(instaUri, parts, "", UploadId); var upload = BGU.CreateUpload(instaUri, File); upload.Priority = BackgroundTransferPriority.High; await HandleUploadAsync(upload, true); }
private async void TryToUseFFmpegLocal() { if (inputFile != null) { VideoBox.Stop(); // Open StorageFile as IRandomAccessStream to be passed to FFmpegInteropMSS IRandomAccessStream readStream = await inputFile.OpenAsync(FileAccessMode.Read); try { // Instantiate FFmpegInteropMSS using the opened local file stream ffmpegMss = await FFmpegInteropMSS.CreateFromStreamAsync(readStream); if (ffmpegMss != null) { playbackItem = ffmpegMss.CreateMediaPlaybackItem(); // Pass MediaStreamSource to Media Element VideoBox.SetPlaybackSource(playbackItem); } else { ShowDialog.DisplayErrorMessage(ErrorInfo.CannotOpen); } } catch (Exception ex) { ShowDialog.DisplayErrorMessage(ex.Message); } } //try //{ // IRandomAccessStream readStream = await inputFile.OpenAsync(FileAccessMode.Read); // ffmpegMss = await FFmpegInteropMSS.CreateFromStreamAsync(readStream); // if (ffmpegMss != null) // { // MediaStreamSource mss = ffmpegMss.GetMediaStreamSource(); // if (mss != null) // { // mss.BufferTime = TimeSpan.Zero; // sender.Source = MediaSource.CreateFromMediaStreamSource(mss); // sender.RealTimePlayback = true; // sender.Play(); // } // else // { // ShowDialog.DisplayErrorMessage(ErrorInfo.CannotOpen); // } // } // else // { // ShowDialog.DisplayErrorMessage(ErrorInfo.CannotOpen); // } //} //catch (Exception ex) //{ // ShowDialog.DisplayErrorMessage(ex.Message); //} }
async Task <StorageFile> ConvertVideo(StorageFile inputFile, Size?imageSize, Rect?rectSize) { try { var outputFile = await GenerateRandomOutputFile(); if (inputFile != null && outputFile != null) { var mediaProfile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); int height = 0, width = 0; double duration = 0; if (DeviceUtil.IsMobile) { var videoInfo = await inputFile.GetVideoInfoAsync(); height = (int)videoInfo.Height; width = (int)videoInfo.Width; duration = videoInfo.Duration.TotalSeconds; } else { FFmpegMSS = await FFmpegInteropMSS .CreateFromStreamAsync(await inputFile.OpenReadAsync(), Helper.FFmpegConfig); Mss = FFmpegMSS.GetMediaStreamSource(); height = FFmpegMSS.VideoStream.PixelHeight; width = FFmpegMSS.VideoStream.PixelWidth; duration = Mss.Duration.TotalSeconds; } var fileProfile = await Uploads.VideoConverterX.GetEncodingProfileFromFileAsync(inputFile); if (fileProfile != null) { mediaProfile.Video.Bitrate = fileProfile.Video.Bitrate; if (mediaProfile.Audio != null) { mediaProfile.Audio.Bitrate = fileProfile.Audio.Bitrate; mediaProfile.Audio.BitsPerSample = fileProfile.Audio.BitsPerSample; mediaProfile.Audio.ChannelCount = fileProfile.Audio.ChannelCount; mediaProfile.Audio.SampleRate = fileProfile.Audio.SampleRate; } "Media profile copied from original video".PrintDebug(); } if (!IsStoryVideo) { if (duration > 59) { Transcoder.TrimStartTime = StartTime; Transcoder.TrimStopTime = StopTime; } var max = Math.Max(height, width); if (max > 1920) { max = 1920; } if (imageSize == null) { mediaProfile.Video.Height = (uint)max; mediaProfile.Video.Width = (uint)max; } else { mediaProfile.Video.Height = (uint)imageSize.Value.Height; mediaProfile.Video.Width = (uint)imageSize.Value.Width; } } else { if (duration > 14.9) { Transcoder.TrimStartTime = StartTime; Transcoder.TrimStopTime = StopTime; } var size = Helpers.AspectRatioHelper.GetAspectRatioX(width, height); mediaProfile.Video.Height = (uint)size.Height; mediaProfile.Video.Width = (uint)size.Width; } var transform = new VideoTransformEffectDefinition { Rotation = MediaRotation.None, OutputSize = imageSize.Value, Mirror = MediaMirroringOptions.None, CropRectangle = rectSize == null ? Rect.Empty : rectSize.Value }; Transcoder.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties); PrepareTranscodeResult preparedTranscodeResult; if (DeviceUtil.IsMobile) { preparedTranscodeResult = await Transcoder.PrepareFileTranscodeAsync(inputFile, outputFile, mediaProfile); } else { preparedTranscodeResult = await Transcoder.PrepareMediaStreamSourceTranscodeAsync(Mss, await outputFile.OpenAsync(FileAccessMode.ReadWrite), mediaProfile); } Transcoder.VideoProcessingAlgorithm = MediaVideoProcessingAlgorithm.Default; if (preparedTranscodeResult.CanTranscode) { var progress = new Progress <double>(ConvertProgress); await preparedTranscodeResult.TranscodeAsync().AsTask(Cts.Token, progress); ConvertComplete(outputFile); return(outputFile); } else { preparedTranscodeResult.FailureReason.ToString().ShowMsg(); } } } catch (Exception ex) { ex.PrintException().ShowMsg("ConvertVideo"); } return(null); }