public async Task <IFile> ExtractAsync(IFile videoFile)
        {
            if (videoFile == null)
            {
                throw new ArgumentNullException(nameof(videoFile));
            }

            if (videoFile.StorageType != StorageType.FileSystem)
            {
                throw new Exception("FFMpeg can work only with file on FileSystem");
            }

            //get full path for audio
            string audioFullpath = Path.Combine(
                _fileResolver.GetAudioFolderPath(StorageType.FileSystem),
                videoFile.Filename + "." + _audioOptions.Value.DefaultFormat);

            //get video
            string videofilePath = _fileResolver.VideoFilePath(videoFile.Filename, videoFile.Extension, StorageType.FileSystem);

            if (File.Exists(audioFullpath))
            {
                File.Delete(audioFullpath);
            }

            //extract audio from video
            var ffMpeg = new FFMpegConverter();

            ffMpeg.LogReceived += FfMpeg_LogReceived;

            ffMpeg.Invoke(
                string.Format(FFMpegExtractAudioFormat,
                              videofilePath,
                              _audioOptions.Value.DefaultFormat,
                              _audioOptions.Value.BitRate,
                              audioFullpath));

            var cancellationTokenSource = new CancellationTokenSource();

            //delete video
            File.Delete(videofilePath);

            try
            {
                await Task.Delay(10000, _cancellationTokenSource.Token);
            } catch (TaskCanceledException)
            {
                //do nothing cancel it okey
            }

            //return info about audio file
            return(new PCFile()
            {
                Filename = videoFile.Filename,
                Extension = "." + _audioOptions.Value.DefaultFormat,
                Duration = _duration,
                StorageType = StorageType.FileSystem,
            });
        }
Exemple #2
0
        public async Task <IFile> ExtractAsync(IFile videoFile)
        {
            if (videoFile == null)
            {
                throw new ArgumentNullException(nameof(videoFile));
            }

            if (videoFile.StorageType != StorageType.FileSystem)
            {
                throw new Exception("FFMpeg can work only with file on FileSystem");
            }

            //get full path for audio
            var audioFullpath = Path.Combine(
                _fileResolver.GetAudioFolderPath(StorageType.FileSystem),
                videoFile.Filename + "." + _audioOptions.Value.DefaultFormat);

            //get video
            var videofilePath = _fileResolver.VideoFilePath(videoFile.Filename, videoFile.Extension, StorageType.FileSystem);

            if (File.Exists(audioFullpath))
            {
                File.Delete(audioFullpath);
            }

            //extract audio from video
            var ffMpeg = new FFmpegProcess();

            ffMpeg.RunFFmpeg(string.Format(FFMpegExtractAudioFormat,
                                           videofilePath,
                                           _audioOptions.Value.DefaultFormat,
                                           _audioOptions.Value.BitRate,
                                           audioFullpath));

            //delete video
            File.Delete(videofilePath);

            var match = FindDurationRegex.Match(ffMpeg.Output);

            if (match.Success)
            {
                _duration = int.Parse(match.Groups[1].Value) * 3600 +
                            int.Parse(match.Groups[2].Value) * 60 +
                            int.Parse(match.Groups[3].Value) +
                            int.Parse(match.Groups[4].Value) / 100.0;
            }

            //return info about audio file
            return(new PCFile
            {
                Filename = videoFile.Filename,
                Extension = "." + _audioOptions.Value.DefaultFormat,
                Duration = _duration,
                StorageType = StorageType.FileSystem
            });
        }