コード例 #1
0
ファイル: VideoConverterX.cs プロジェクト: xuan2261/Minista
        async Task <StorageUploadItem> ConvertVideoAsync(StorageUploadItem uploadItem)
        {
            try
            {
                var MediaProfile = MediaEncodingProfile.CreateMp4(uploadItem.VideoEncodingQuality);
                var outputFile   = await GenerateRandomOutputFile();

                if (uploadItem != null && outputFile != null)
                {
                    var inputFile = uploadItem.VideoToUpload;

                    //MediaProfile = await MediaEncodingProfile.CreateFromFileAsync(inputFile);
                    Transcoder.TrimStartTime = uploadItem.StartTime;
                    Transcoder.TrimStopTime  = uploadItem.EndTime;

                    MediaProfile.Video.Height = (uint)uploadItem.Size.Height;
                    MediaProfile.Video.Width  = (uint)uploadItem.Size.Width;


                    var transform = new VideoTransformEffectDefinition
                    {
                        Rotation      = MediaRotation.None,
                        OutputSize    = uploadItem.Size,
                        Mirror        = MediaMirroringOptions.None,
                        CropRectangle = uploadItem.Rect
                    };

                    Transcoder.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties);

                    var preparedTranscodeResult = await Transcoder
                                                  .PrepareFileTranscodeAsync(inputFile,
                                                                             outputFile,
                                                                             //.PrepareMediaStreamSourceTranscodeAsync(Mss,
                                                                             //await outputFile.OpenAsync(FileAccessMode.ReadWrite),
                                                                             MediaProfile);

                    if (preparedTranscodeResult.CanTranscode)
                    {
                        var progress = new Progress <double>(ConvertProgress);
                        await preparedTranscodeResult.TranscodeAsync().AsTask(new CancellationTokenSource().Token, progress);

                        ConvertComplete(outputFile);
                        uploadItem.VideoToUpload = outputFile;
                        return(uploadItem);
                    }
                    else
                    {
                        preparedTranscodeResult.FailureReason.ToString().ShowMsg();
                    }
                }
            }
            catch (Exception ex) { ex.PrintException().ShowMsg("ConvertVideo"); }
            return(uploadItem);
        }
コード例 #2
0
            private async void Send(DialogViewModel viewModel, ChatRecordMode mode, StorageFile file, bool mirroring, int duration)
            {
                if (mode == ChatRecordMode.Video)
                {
                    var props = await file.Properties.GetVideoPropertiesAsync();

                    var width  = props.GetWidth();
                    var height = props.GetHeight();
                    var x      = 0d;
                    var y      = 0d;

                    if (width > height)
                    {
                        x     = (width - height) / 2;
                        width = height;
                    }
                    else if (height > width)
                    {
                        y      = (height - width) / 2;
                        height = width;
                    }

                    var transform = new VideoTransformEffectDefinition();
                    transform.CropRectangle = new Rect(x, y, width, height);
                    transform.OutputSize    = new Size(240, 240);
                    transform.Mirror        = mirroring ? MediaMirroringOptions.Horizontal : MediaMirroringOptions.None;

                    var profile = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
                    profile.Video.Width   = 240;
                    profile.Video.Height  = 240;
                    profile.Video.Bitrate = 300000;

                    try
                    {
                        viewModel.Dispatcher.Dispatch(async() =>
                        {
                            await viewModel.SendVideoNoteAsync(file, profile, transform);
                        });
                    }
                    catch { }
                }
                else
                {
                    try
                    {
                        viewModel.Dispatcher.Dispatch(async() =>
                        {
                            await viewModel.SendVoiceNoteAsync(file, duration, null);
                        });
                    }
                    catch { }
                }
            }
コード例 #3
0
        public VideoTransformEffectDefinition GetTransform()
        {
            var crop = CropRectangle;

            if (crop.HasValue)
            {
                var transform = new VideoTransformEffectDefinition();
                transform.CropRectangle = crop.Value;

                return(transform);
            }

            return(null);
        }
コード例 #4
0
        public VideoTransformEffectDefinition GetTransform()
        {
            var crop = _editState?.Rectangle ?? Rect.Empty;

            if (!(crop.IsEmpty || (crop.X == 0 && crop.Y == 0 && crop.Width == 1 && crop.Height == 1)))
            {
                var transform = new VideoTransformEffectDefinition();
                transform.CropRectangle = crop;

                return(transform);
            }

            return(null);
        }
コード例 #5
0
        private async void EffectButton_Tapped(object sender, TappedRoutedEventArgs e)
        {
            //VideoStabilizationEffectDefinition vsed = new VideoStabilizationEffectDefinition();
            //VideoEffectDefinition ved = new VideoEffectDefinition(vsed.ActivatableClassId, vsed.Properties);

            // 创建一个属性集,并添加一个属性/值对
            PropertySet echoProperties = new PropertySet();

            echoProperties.Add("Mix", 0.5f);

            PropertySet properties = new PropertySet();

            properties.Add("FadeValue", 5);

            VideoTransformEffectDefinition van    = new VideoTransformEffectDefinition();
            VideoEffectDefinition          effect = new VideoEffectDefinition(van.ActivatableClassId, van.Properties);

            //   VideoEffectDefinition effect = new VideoEffectDefinition(typeof(ExampleVideoEffect).FullName);

            if (App.Model.Current != null)
            {
                try
                {
                    App.Model.Current.VideoEffectDefinitions.Add(effect);
                }
                catch (Exception exception)
                {
                    await new MessageDialog(exception.Message).ShowAsync();
                }
            }
            else if (App.Model.OverlayCurrent != null)
            {
                try
                {
                    App.Model.OverlayCurrent.Clip.VideoEffectDefinitions.Add(new VideoEffectDefinition(typeof(ExampleVideoEffect).FullName));
                }
                catch (Exception exception)
                {
                    await new MessageDialog(exception.Message).ShowAsync();
                }
            }
        }
コード例 #6
0
ファイル: UploadUc.xaml.cs プロジェクト: xuan2261/Minista
        void ShowVideo()
        {
            try
            {
                var MediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(FileToUpload));
                //FileToUpload
                ME.SetPlaybackSource(MediaPlaybackItem);
                ME.RemoveAllEffects();
                var transform = new VideoTransformEffectDefinition
                {
                    Rotation      = MediaRotation.None,
                    OutputSize    = new Size(BitmapDecoder.PixelWidth, BitmapDecoder.PixelHeight),
                    Mirror        = MediaMirroringOptions.None,
                    CropRectangle = CurrentCroppedRectForVideo
                };

                ME.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties);
            }
            catch { }
        }
コード例 #7
0
        void ShowVideo()
        {
            try
            {
                var MediaPlaybackItem = new MediaPlaybackItem(MediaSource.CreateFromStorageFile(FileToUpload));
                //FileToUpload
                ME.SetPlaybackSource(MediaPlaybackItem);
                ME.RemoveAllEffects();
                var transform = new VideoTransformEffectDefinition
                {
                    Rotation      = MediaRotation.None,
                    OutputSize    = Editor.ScaledSize,
                    Mirror        = MediaMirroringOptions.None,
                    CropRectangle = Editor.ScaledCropRect
                };

                ME.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties);
            }
            catch { }
        }
コード例 #8
0
        public async Task SendVideoNoteAsync(StorageFile file, MediaEncodingProfile profile = null, VideoTransformEffectDefinition transform = null)
        {
            var basicProps = await file.GetBasicPropertiesAsync();

            var videoProps = await file.Properties.GetVideoPropertiesAsync();

            //var thumbnail = await ImageHelper.GetVideoThumbnailAsync(file, videoProps, transform);

            var videoWidth  = (int)videoProps.GetWidth();
            var videoHeight = (int)videoProps.GetHeight();

            if (profile != null)
            {
                videoWidth  = videoProps.Orientation == VideoOrientation.Rotate180 || videoProps.Orientation == VideoOrientation.Normal ? (int)profile.Video.Width : (int)profile.Video.Height;
                videoHeight = videoProps.Orientation == VideoOrientation.Rotate180 || videoProps.Orientation == VideoOrientation.Normal ? (int)profile.Video.Height : (int)profile.Video.Width;
            }

            var conversion = new VideoConversion();

            if (profile != null)
            {
                conversion.Transcode = true;
                conversion.Width     = profile.Video.Width;
                conversion.Height    = profile.Video.Height;
                conversion.Bitrate   = profile.Video.Bitrate;

                if (transform != null)
                {
                    conversion.Transform     = true;
                    conversion.Rotation      = transform.Rotation;
                    conversion.OutputSize    = transform.OutputSize;
                    conversion.Mirror        = transform.Mirror;
                    conversion.CropRectangle = transform.CropRectangle;
                }
            }

            var generated = await file.ToGeneratedAsync("transcode#" + JsonConvert.SerializeObject(conversion));

            var thumbnail = await file.ToThumbnailAsync(conversion, "thumbnail_transcode#" + JsonConvert.SerializeObject(conversion));

            var reply = GetReply(true);
            var input = new InputMessageVideoNote(generated, thumbnail, (int)videoProps.Duration.TotalSeconds, Math.Min(videoWidth, videoHeight));

            await SendMessageAsync(reply, input);
        }
コード例 #9
0
        public async Task SendVideoAsync(StorageFile file, string caption, bool animated, bool asFile, int?ttl = null, MediaEncodingProfile profile = null, VideoTransformEffectDefinition transform = null)
        {
            var basicProps = await file.GetBasicPropertiesAsync();

            var videoProps = await file.Properties.GetVideoPropertiesAsync();

            //var thumbnail = await ImageHelper.GetVideoThumbnailAsync(file, videoProps, transform);

            var videoWidth  = (int)videoProps.GetWidth();
            var videoHeight = (int)videoProps.GetHeight();

            if (profile != null)
            {
                videoWidth  = videoProps.Orientation == VideoOrientation.Rotate180 || videoProps.Orientation == VideoOrientation.Normal ? (int)profile.Video.Width : (int)profile.Video.Height;
                videoHeight = videoProps.Orientation == VideoOrientation.Rotate180 || videoProps.Orientation == VideoOrientation.Normal ? (int)profile.Video.Height : (int)profile.Video.Width;
            }

            var conversion = new VideoConversion();

            if (profile != null)
            {
                conversion.Transcode = true;
                conversion.Mute      = profile.Audio == null;
                conversion.Width     = profile.Video.Width;
                conversion.Height    = profile.Video.Height;
                conversion.Bitrate   = profile.Video.Bitrate;

                if (transform != null)
                {
                    conversion.Transform     = true;
                    conversion.Rotation      = transform.Rotation;
                    conversion.OutputSize    = transform.OutputSize;
                    conversion.Mirror        = transform.Mirror;
                    conversion.CropRectangle = transform.CropRectangle;
                }
            }

            var generated = await file.ToGeneratedAsync("transcode#" + JsonConvert.SerializeObject(conversion));

            var thumbnail = await file.ToThumbnailAsync(conversion, "thumbnail_transcode#" + JsonConvert.SerializeObject(conversion));

            if (asFile)
            {
                var reply = GetReply(true);
                var input = new InputMessageDocument(generated, thumbnail, GetFormattedText(caption));

                await SendMessageAsync(reply, input);
            }
            else
            {
                if (profile != null && profile.Audio == null)
                {
                    var reply = GetReply(true);
                    var input = new InputMessageAnimation(generated, thumbnail, (int)videoProps.Duration.TotalSeconds, videoWidth, videoHeight, GetFormattedText(caption));

                    await SendMessageAsync(reply, input);
                }
                else
                {
                    var reply = GetReply(true);
                    var input = new InputMessageVideo(generated, thumbnail, new int[0], (int)videoProps.Duration.TotalSeconds, videoWidth, videoHeight, true, GetFormattedText(caption), ttl ?? 0);

                    await SendMessageAsync(reply, input);
                }
            }
        }
コード例 #10
0
ファイル: ImageHelper.cs プロジェクト: nazzi88ua/Unigram
        public static async Task <TLPhotoSizeBase> GetVideoThumbnailAsync(StorageFile file, VideoProperties props, VideoTransformEffectDefinition effect)
        {
            double originalWidth  = props.GetWidth();
            double originalHeight = props.GetHeight();

            if (effect != null && !effect.CropRectangle.IsEmpty)
            {
                file = await CropAsync(file, effect.CropRectangle);

                originalWidth  = effect.CropRectangle.Width;
                originalHeight = effect.CropRectangle.Height;
            }

            TLPhotoSizeBase result;
            var             fileLocation = new TLFileLocation
            {
                VolumeId = TLLong.Random(),
                LocalId  = TLInt.Random(),
                Secret   = TLLong.Random(),
                DCId     = 0
            };

            var desiredName = string.Format("{0}_{1}_{2}.jpg", fileLocation.VolumeId, fileLocation.LocalId, fileLocation.Secret);
            var desiredFile = await FileUtils.CreateTempFileAsync(desiredName);

            using (var fileStream = await OpenReadAsync(file))
                using (var outputStream = await desiredFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var decoder = await BitmapDecoder.CreateAsync(fileStream);

                    double ratioX = (double)90 / originalWidth;
                    double ratioY = (double)90 / originalHeight;
                    double ratio  = Math.Min(ratioX, ratioY);

                    uint width  = (uint)(originalWidth * ratio);
                    uint height = (uint)(originalHeight * ratio);

                    var transform = new BitmapTransform();
                    transform.ScaledWidth       = width;
                    transform.ScaledHeight      = height;
                    transform.InterpolationMode = BitmapInterpolationMode.Linear;

                    if (effect != null)
                    {
                        transform.Flip = effect.Mirror == MediaMirroringOptions.Horizontal ? BitmapFlip.Horizontal : BitmapFlip.None;
                    }

                    var pixelData = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

                    var propertySet  = new BitmapPropertySet();
                    var qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);
                    propertySet.Add("ImageQuality", qualityValue);

                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);

                    encoder.SetSoftwareBitmap(pixelData);
                    await encoder.FlushAsync();

                    result = new TLPhotoSize
                    {
                        W        = (int)width,
                        H        = (int)height,
                        Size     = (int)outputStream.Size,
                        Type     = string.Empty,
                        Location = fileLocation
                    };
                }

            return(result);
        }
コード例 #11
0
ファイル: MessageFactory.cs プロジェクト: nitanmarcel/Unigram
        public async Task <InputMessageFactory> CreateVideoAsync(StorageFile file, bool animated, bool asFile, int ttl = 0, MediaEncodingProfile profile = null, VideoTransformEffectDefinition transform = null)
        {
            var basicProps = await file.GetBasicPropertiesAsync();

            var videoProps = await file.Properties.GetVideoPropertiesAsync();

            //var thumbnail = await ImageHelper.GetVideoThumbnailAsync(file, videoProps, transform);

            var duration    = (int)videoProps.Duration.TotalSeconds;
            var videoWidth  = (int)videoProps.GetWidth();
            var videoHeight = (int)videoProps.GetHeight();

            if (profile != null)
            {
                videoWidth  = videoProps.Orientation is VideoOrientation.Rotate180 or VideoOrientation.Normal ? (int)profile.Video.Width : (int)profile.Video.Height;
                videoHeight = videoProps.Orientation is VideoOrientation.Rotate180 or VideoOrientation.Normal ? (int)profile.Video.Height : (int)profile.Video.Width;
            }

            var conversion = new VideoConversion();

            if (profile != null)
            {
                //conversion.Transcode = true;
                conversion.Mute = animated;
                //conversion.Width = profile.Video.Width;
                //conversion.Height = profile.Video.Height;
                //conversion.Bitrate = profile.Video.Bitrate;

                if (transform != null)
                {
                    conversion.Transcode     = true;
                    conversion.Transform     = true;
                    conversion.Rotation      = transform.Rotation;
                    conversion.OutputSize    = transform.OutputSize;
                    conversion.Mirror        = transform.Mirror;
                    conversion.CropRectangle = transform.CropRectangle;
                }
            }

            var generated = await file.ToGeneratedAsync(ConversionType.Transcode, JsonConvert.SerializeObject(conversion));

            var thumbnail = await file.ToThumbnailAsync(conversion, ConversionType.TranscodeThumbnail, JsonConvert.SerializeObject(conversion));

            if (asFile && ttl == 0)
            {
                return(new InputMessageFactory
                {
                    InputFile = generated,
                    Type = new FileTypeDocument(),
                    Delegate = (inputFile, caption) => new InputMessageDocument(inputFile, thumbnail, true, caption)
                });
            }
            else if (animated && ttl == 0)
            {
                return(new InputMessageFactory
                {
                    InputFile = generated,
                    Type = new FileTypeAnimation(),
                    Delegate = (inputFile, caption) => new InputMessageAnimation(inputFile, thumbnail, new int[0], duration, videoWidth, videoHeight, caption)
                });
            }

            return(new InputMessageFactory
            {
                InputFile = generated,
                Type = new FileTypeVideo(),
                Delegate = (inputFile, caption) => new InputMessageVideo(inputFile, thumbnail, new int[0], duration, videoWidth, videoHeight, true, caption, ttl)
            });
        }
コード例 #12
0
ファイル: MessageFactory.cs プロジェクト: murka1611/Unigram
        public async Task <InputMessageFactory> CreateVideoNoteAsync(StorageFile file, MediaEncodingProfile profile = null, VideoTransformEffectDefinition transform = null)
        {
            var basicProps = await file.GetBasicPropertiesAsync();

            var videoProps = await file.Properties.GetVideoPropertiesAsync();

            //var thumbnail = await ImageHelper.GetVideoThumbnailAsync(file, videoProps, transform);

            var duration    = (int)videoProps.Duration.TotalSeconds;
            var videoWidth  = (int)videoProps.GetWidth();
            var videoHeight = (int)videoProps.GetHeight();

            if (profile != null)
            {
                videoWidth  = videoProps.Orientation == VideoOrientation.Rotate180 || videoProps.Orientation == VideoOrientation.Normal ? (int)profile.Video.Width : (int)profile.Video.Height;
                videoHeight = videoProps.Orientation == VideoOrientation.Rotate180 || videoProps.Orientation == VideoOrientation.Normal ? (int)profile.Video.Height : (int)profile.Video.Width;
            }

            var conversion = new VideoConversion();

            if (profile != null)
            {
                conversion.Transcode = true;
                conversion.Width     = profile.Video.Width;
                conversion.Height    = profile.Video.Height;
                conversion.Bitrate   = profile.Video.Bitrate;

                if (transform != null)
                {
                    conversion.Transform     = true;
                    conversion.Rotation      = transform.Rotation;
                    conversion.OutputSize    = transform.OutputSize;
                    conversion.Mirror        = transform.Mirror;
                    conversion.CropRectangle = transform.CropRectangle;
                }
            }

            var generated = await file.ToGeneratedAsync(ConversionType.Transcode, JsonConvert.SerializeObject(conversion));

            var thumbnail = await file.ToThumbnailAsync(conversion, ConversionType.TranscodeThumbnail, JsonConvert.SerializeObject(conversion));

            return(new InputMessageFactory
            {
                InputFile = generated,
                Type = new FileTypeVideoNote(),
                Delegate = (inputFile, caption) => new InputMessageVideoNote(inputFile, thumbnail, duration, Math.Min(videoWidth, videoHeight))
            });
        }
コード例 #13
0
        private async Task TranscodeAsync(UpdateFileGenerationStart update, string[] args)
        {
            try
            {
                var conversion = JsonConvert.DeserializeObject <VideoConversion>(args[2]);
                if (conversion.Transcode) // <==> conversion.Mute (currently, see: MessageFactory)
                {
                    var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(args[0]);

                    var temp = await StorageFile.GetFileFromPathAsync(update.DestinationPath);

                    var profile = await MediaEncodingProfile.CreateFromFileAsync(file);

                    if ((profile.Video.Width != conversion.Width || profile.Video.Height != conversion.Height) &&
                        conversion.Width > 0 && conversion.Height > 0 && conversion.Bitrate > 0) // All zero for video profile
                    {
                        profile.Video.Width   = conversion.Width;                                //Note: OutputSize tells the video effect how to crop the video, and encoding profile tells the encoder how to encode the video
                        profile.Video.Height  = conversion.Height;
                        profile.Video.Bitrate = conversion.Bitrate;
                    }
                    else if (profile.Video.Width == conversion.Width && profile.Video.Height == conversion.Height &&
                             Math.Abs(profile.Video.Bitrate - conversion.Bitrate) < 10000)
                    { // Do not transcode if bitrate is very similar
                        await CopyAsync(update, args);

                        return;
                    }
                    else
                    if (profile.Audio == null && conversion.Mute && conversion.TrimStartTime == null && conversion.TrimStopTime == null)
                    {
                        await CopyAsync(update, args);

                        return;
                    }
                    //profile.Video.Width = conversion.Width;
                    //profile.Video.Height = conversion.Height;
                    //profile.Video.Bitrate = conversion.Bitrate;

                    if (conversion.Mute)
                    {
                        profile.Audio = null;
                    }

                    var transcoder = new MediaTranscoder();

                    if (conversion.TrimStartTime is TimeSpan trimStart)
                    {
                        transcoder.TrimStartTime = trimStart;
                    }
                    if (conversion.TrimStopTime is TimeSpan trimStop)
                    {
                        transcoder.TrimStopTime = trimStop;
                    }

                    if (conversion.Transform)
                    {
                        var transform = new VideoTransformEffectDefinition();
                        transform.Rotation      = conversion.Rotation;
                        transform.OutputSize    = conversion.OutputSize;
                        transform.Mirror        = conversion.Mirror;
                        transform.CropRectangle = conversion.CropRectangle.IsEmpty() ? Rect.Empty : conversion.CropRectangle;

                        profile.Video.Width  = (uint)conversion.OutputSize.Width;
                        profile.Video.Height = (uint)conversion.OutputSize.Height;

                        transcoder.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties);
                    }

                    var prepare = await transcoder.PrepareFileTranscodeAsync(file, temp, profile);

                    if (prepare.CanTranscode)
                    {
                        var progress = prepare.TranscodeAsync();
                        progress.Progress = (result, delta) =>
                        {
                            _protoService.Send(new SetFileGenerationProgress(update.GenerationId, 100, (int)delta));
                        };
                        progress.Completed = (result, delta) =>
                        {
                            _protoService.Send(new FinishFileGeneration(update.GenerationId, prepare.FailureReason == TranscodeFailureReason.None ? null : new Error(500, prepare.FailureReason.ToString())));
                        };
                    }
                    else
                    {
                        _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, prepare.FailureReason.ToString())));
                    }
                }
                else
                {
                    await CopyAsync(update, args);
                }
            }
            catch (Exception ex)
            {
                _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID " + ex.ToString())));
            }
        }
コード例 #14
0
ファイル: VideoConverterX.cs プロジェクト: ramtinak/Minista
        async Task <StorageUploadItem> ConvertVideoAsync(StorageUploadItem uploadItem)
        {
            try
            {
                var mediaProfile = MediaEncodingProfile.CreateMp4(uploadItem.VideoEncodingQuality);
                var outputFile   = await GenerateRandomOutputFile();

                if (uploadItem != null && outputFile != null)
                {
                    var inputFile = uploadItem.VideoToUpload;

                    var fileProfile = await 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();
                    }
                    //MediaProfile = await MediaEncodingProfile.CreateFromFileAsync(inputFile);
                    Transcoder.TrimStartTime = uploadItem.StartTime;
                    Transcoder.TrimStopTime  = uploadItem.EndTime;

                    mediaProfile.Video.Height = (uint)uploadItem.Size.Height;
                    mediaProfile.Video.Width  = (uint)uploadItem.Size.Width;


                    var transform = new VideoTransformEffectDefinition
                    {
                        Rotation      = MediaRotation.None,
                        OutputSize    = uploadItem.Size,
                        Mirror        = MediaMirroringOptions.None,
                        CropRectangle = uploadItem.Rect
                    };

                    Transcoder.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties);

                    var preparedTranscodeResult = await Transcoder
                                                  .PrepareFileTranscodeAsync(inputFile,
                                                                             outputFile,
                                                                             mediaProfile);

                    if (preparedTranscodeResult.CanTranscode)
                    {
                        var progress = new Progress <double>(ConvertProgress);
                        await preparedTranscodeResult.TranscodeAsync().AsTask(new CancellationTokenSource().Token, progress);

                        ConvertComplete(outputFile);
                        uploadItem.VideoToUpload = outputFile;
                        return(uploadItem);
                    }
                    else
                    {
                        preparedTranscodeResult.FailureReason.ToString().ShowMsg();
                    }
                }
            }
            catch (Exception ex) { ex.PrintException().ShowMsg("ConvertVideo"); }
            return(uploadItem);
        }
コード例 #15
0
ファイル: VideoConverter.cs プロジェクト: xuan2261/Minista
        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);
        }
コード例 #16
0
        public async Task SendVideoAsync(StorageFile file, string caption, bool round, VideoTransformEffectDefinition transform = null, MediaEncodingProfile profile = null)
        {
            if (_peer == null)
            {
                return;
            }

            var fileLocation = new TLFileLocation
            {
                VolumeId = TLLong.Random(),
                LocalId = TLInt.Random(),
                Secret = TLLong.Random(),
                DCId = 0
            };

            var fileName = string.Format("{0}_{1}_{2}.mp4", fileLocation.VolumeId, fileLocation.LocalId, fileLocation.Secret);
            var fileCache = await FileUtils.CreateTempFileAsync(fileName);

            await file.CopyAndReplaceAsync(fileCache);

            var basicProps = await fileCache.GetBasicPropertiesAsync();
            var videoProps = await fileCache.Properties.GetVideoPropertiesAsync();
            var thumbnailBase = await FileUtils.GetFileThumbnailAsync(file);
            var thumbnail = thumbnailBase as TLPhotoSize;
            if (thumbnail == null)
            {
                return;
            }

            var desiredName = string.Format("{0}_{1}_{2}.jpg", thumbnail.Location.VolumeId, thumbnail.Location.LocalId, thumbnail.Location.Secret);

            var date = TLUtils.DateToUniversalTimeTLInt(ProtoService.ClientTicksDelta, DateTime.Now);

            var videoWidth = (int)videoProps.Width;
            var videoHeight = (int)videoProps.Height;

            if (profile != null)
            {
                videoWidth = (int)profile.Video.Width;
                videoHeight = (int)profile.Video.Height;
            }

            var document = new TLDocument
            {
                Id = 0,
                AccessHash = 0,
                Date = date,
                Size = (int)basicProps.Size,
                MimeType = fileCache.ContentType,
                Thumb = thumbnail,
                Attributes = new TLVector<TLDocumentAttributeBase>
                {
                    new TLDocumentAttributeFilename
                    {
                        FileName = file.Name
                    },
                    new TLDocumentAttributeVideo
                    {
                        Duration = (int)videoProps.Duration.TotalSeconds,
                        W = videoWidth,
                        H = videoHeight,
                        IsRoundMessage = round
                    }
                }
            };

            var media = new TLMessageMediaDocument
            {
                Document = document,
                Caption = caption
            };

            var message = TLUtils.GetMessage(SettingsHelper.UserId, _peer.ToPeer(), TLMessageState.Sending, true, true, date, string.Empty, media, TLLong.Random(), null);

            if (Reply != null)
            {
                message.HasReplyToMsgId = true;
                message.ReplyToMsgId = Reply.Id;
                message.Reply = Reply;
                Reply = null;
            }

            var previousMessage = InsertSendingMessage(message);
            CacheService.SyncSendingMessage(message, previousMessage, async (m) =>
            {
                if (transform != null && profile != null)
                {
                    await fileCache.RenameAsync(fileName + ".temp.mp4");
                    var fileResult = await FileUtils.CreateTempFileAsync(fileName);

                    var transcoder = new MediaTranscoder();
                    transcoder.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties);

                    var prepare = await transcoder.PrepareFileTranscodeAsync(fileCache, fileResult, profile);
                    await prepare.TranscodeAsync().AsTask(Upload(media.Document as TLDocument, progress => new TLSendMessageUploadDocumentAction { Progress = progress }, 0, 200.0));

                    //await fileCache.DeleteAsync();
                    fileCache = fileResult;

                    thumbnailBase = await FileUtils.GetFileThumbnailAsync(fileCache);
                    thumbnail = thumbnailBase as TLPhotoSize;

                    desiredName = string.Format("{0}_{1}_{2}.jpg", thumbnail.Location.VolumeId, thumbnail.Location.LocalId, thumbnail.Location.Secret);
                    document.Thumb = thumbnail;
                }

                var fileId = TLLong.Random();
                var upload = await _uploadVideoManager.UploadFileAsync(fileId, fileCache.Name, false).AsTask(Upload(media.Document as TLDocument, progress => new TLSendMessageUploadDocumentAction { Progress = progress }, 0.5, 2.0));
                if (upload != null)
                {
                    var thumbFileId = TLLong.Random();
                    var thumbUpload = await _uploadDocumentManager.UploadFileAsync(thumbFileId, desiredName);
                    if (thumbUpload != null)
                    {
                        var inputMedia = new TLInputMediaUploadedThumbDocument
                        {
                            File = upload.ToInputFile(),
                            Thumb = thumbUpload.ToInputFile(),
                            MimeType = document.MimeType,
                            Caption = media.Caption,
                            Attributes = document.Attributes
                        };

                        var result = await ProtoService.SendMediaAsync(_peer, inputMedia, message);
                    }
                    //if (result.IsSucceeded)
                    //{
                    //    var update = result.Result as TLUpdates;
                    //    if (update != null)
                    //    {
                    //        var newMessage = update.Updates.OfType<TLUpdateNewMessage>().FirstOrDefault();
                    //        if (newMessage != null)
                    //        {
                    //            var newM = newMessage.Message as TLMessage;
                    //            if (newM != null)
                    //            {
                    //                message.Media = newM.Media;
                    //                message.RaisePropertyChanged(() => message.Media);
                    //            }
                    //        }
                    //    }
                    //}
                }
            });
        }
コード例 #17
0
ファイル: GenerationService.cs プロジェクト: DJ-ZX/Unigram
        private async Task TranscodeAsync(UpdateFileGenerationStart update)
        {
            try
            {
                var args = update.Conversion.Substring("transcode#".Length);
                args = args.Substring(0, args.LastIndexOf('#'));

                var conversion = JsonConvert.DeserializeObject <VideoConversion>(args);
                if (conversion.Transcode)
                {
                    var file = await StorageFile.GetFileFromPathAsync(update.OriginalPath);

                    var temp = await StorageFile.GetFileFromPathAsync(update.DestinationPath);

                    var transcoder = new MediaTranscoder();

                    var profile = await MediaEncodingProfile.CreateFromFileAsync(file);

                    profile.Video.Width   = conversion.Width;
                    profile.Video.Height  = conversion.Height;
                    profile.Video.Bitrate = conversion.Bitrate;

                    if (conversion.Mute)
                    {
                        profile.Audio = null;
                    }

                    if (conversion.Transform)
                    {
                        var transform = new VideoTransformEffectDefinition();
                        transform.Rotation      = conversion.Rotation;
                        transform.OutputSize    = conversion.OutputSize;
                        transform.Mirror        = conversion.Mirror;
                        transform.CropRectangle = conversion.CropRectangle.IsEmpty() ? Rect.Empty : conversion.CropRectangle;

                        transcoder.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties);
                    }

                    var prepare = await transcoder.PrepareFileTranscodeAsync(file, temp, profile);

                    if (prepare.CanTranscode)
                    {
                        var progress = prepare.TranscodeAsync();
                        progress.Progress = (result, delta) =>
                        {
                            _protoService.Send(new SetFileGenerationProgress(update.GenerationId, (int)delta, 100));
                        };
                        progress.Completed = (result, delta) =>
                        {
                            _protoService.Send(new FinishFileGeneration(update.GenerationId, prepare.FailureReason == TranscodeFailureReason.None ? null : new Error(406, prepare.FailureReason.ToString())));
                        };
                    }
                    else
                    {
                        _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, prepare.FailureReason.ToString())));
                    }
                }
                else
                {
                    await CopyAsync(update);
                }
            }
            catch (Exception ex)
            {
                _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, ex.ToString())));
            }
        }
コード例 #18
0
        private async Task TranscodeAsync(UpdateFileGenerationStart update, string[] args)
        {
            try
            {
                var conversion = JsonConvert.DeserializeObject <VideoConversion>(args[2]);
                if (conversion.Mute)
                {
                    var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(args[0]);

                    var temp = await StorageFile.GetFileFromPathAsync(update.DestinationPath);

                    var profile = await MediaEncodingProfile.CreateFromFileAsync(file);

                    if (profile.Audio == null && conversion.Mute)
                    {
                        await CopyAsync(update, args);

                        return;
                    }
                    //profile.Video.Width = conversion.Width;
                    //profile.Video.Height = conversion.Height;
                    //profile.Video.Bitrate = conversion.Bitrate;

                    if (conversion.Mute)
                    {
                        profile.Audio = null;
                    }

                    var transcoder = new MediaTranscoder();

                    if (conversion.Transform)
                    {
                        var transform = new VideoTransformEffectDefinition();
                        transform.Rotation      = conversion.Rotation;
                        transform.OutputSize    = conversion.OutputSize;
                        transform.Mirror        = conversion.Mirror;
                        transform.CropRectangle = conversion.CropRectangle.IsEmpty() ? Rect.Empty : conversion.CropRectangle;

                        transcoder.AddVideoEffect(transform.ActivatableClassId, true, transform.Properties);
                    }

                    var prepare = await transcoder.PrepareFileTranscodeAsync(file, temp, profile);

                    if (prepare.CanTranscode)
                    {
                        var progress = prepare.TranscodeAsync();
                        progress.Progress = (result, delta) =>
                        {
                            _protoService.Send(new SetFileGenerationProgress(update.GenerationId, 100, (int)delta));
                        };
                        progress.Completed = (result, delta) =>
                        {
                            _protoService.Send(new FinishFileGeneration(update.GenerationId, prepare.FailureReason == TranscodeFailureReason.None ? null : new Error(500, prepare.FailureReason.ToString())));
                        };
                    }
                    else
                    {
                        _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, prepare.FailureReason.ToString())));
                    }
                }
                else
                {
                    await CopyAsync(update, args);
                }
            }
            catch (Exception ex)
            {
                _protoService.Send(new FinishFileGeneration(update.GenerationId, new Error(500, "FILE_GENERATE_LOCATION_INVALID " + ex.ToString())));
            }
        }
コード例 #19
0
        private void Stop()
        {
            Task.Run(async() =>
            {
                _stopReset.WaitOne();
                _stopReset.Reset();

                _recording = false;

                Execute.BeginOnUIThread(() =>
                {
                    if (_video)
                    {
                        _roundView.IsOpen = false;
                    }

                    RecordingStopped?.Invoke(this, EventArgs.Empty);
                });

                //_startReset.Set();
                //return;

                var now     = DateTime.Now;
                var elapsed = now - _start;

                Debug.WriteLine("Stop reached");
                Debug.WriteLine("Stop: " + now);

                if (_recorder == null)
                {
                    _startReset.Set();
                    return;
                }

                if (_recorder.IsRecording)
                {
                    await _recorder.StopAsync();
                }

                if (_cancelOnRelease || elapsed < TimeSpan.FromSeconds(1))
                {
                    await _file.DeleteAsync();
                }
                else if (_file != null)
                {
                    Debug.WriteLine("Sending voice message");

                    Execute.BeginOnUIThread(async() =>
                    {
                        if (_video)
                        {
                            var props  = await _file.Properties.GetVideoPropertiesAsync();
                            var width  = props.Width;
                            var height = props.Height;
                            var x      = 0d;
                            var y      = 0d;

                            if (width > height)
                            {
                                x     = (width - height) / 2;
                                width = height;
                            }

                            if (height > width)
                            {
                                y      = (height - width) / 2;
                                height = width;
                            }

                            var transform           = new VideoTransformEffectDefinition();
                            transform.CropRectangle = new Windows.Foundation.Rect(x, y, width, height);
                            transform.OutputSize    = new Windows.Foundation.Size(240, 240);

                            var profile           = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Vga);
                            profile.Video.Width   = 240;
                            profile.Video.Height  = 240;
                            profile.Video.Bitrate = 300000;

                            await ViewModel.SendVideoAsync(_file, null, true, profile, transform);
                        }
                        else
                        {
                            await ViewModel.SendAudioAsync(_file, (int)elapsed.TotalSeconds, true, null, null, null);
                        }
                    });
                }

                _startReset.Set();
            });
        }
コード例 #20
0
        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);
        }