示例#1
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
            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,
            });
        }
示例#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
            });
        }
示例#3
0
        public FileStreamResult Song(string filename)
        {
            //var result = _authService.CheckToken(AccessToken);

            string contentType = "audio/mpeg";

            string audioFolderPath = _fileResolver.GetAudioFolderPath(StorageType.FileSystem);

            byte[] song       = System.IO.File.ReadAllBytes(Path.Combine(audioFolderPath, filename));
            long   fSize      = song.Length;
            long   startbyte  = 0;
            long   endbyte    = fSize - 1;
            int    statusCode = 200;

            var rangeHeader = Request.Headers["Range"];

            if (Request.Headers.ContainsKey("Range"))
            {
                //Get the actual byte range from the range header string, and set the starting byte.
                string[] range = rangeHeader.ToString().Split(new char[] { '=', '-' });
                startbyte = Convert.ToInt64(range[1]);
                if (range.Length > 2 && range[2] != "")
                {
                    endbyte = Convert.ToInt64(range[2]);
                }
                //If the start byte is not equal to zero, that means the user is requesting partial content.
                if (startbyte != 0 || endbyte != fSize - 1 || range.Length > 2 && range[2] == "")
                {
                    statusCode = 206;
                }                    //Set the status code of the response to 206 (Partial Content) and add a content range header.
            }
            long desSize = endbyte - startbyte + 1;

            //Headers
            Response.StatusCode = statusCode;

            Response.ContentType = contentType;
            Response.Headers.Add("Content-Accept", Response.ContentType);
            Response.Headers.Add("Content-Length", desSize.ToString());
            Response.Headers.Add("Content-Range", string.Format("bytes {0}-{1}/{2}", startbyte, endbyte, fSize));
            //Data

            var stream = new MemoryStream(song, (int)startbyte, (int)desSize);

            return(new FileStreamResult(stream, Response.ContentType));
        }