private void AudioEncoding() { //skip start_time if IsExplicitTimeRange=true int calc_start_time = 0, calc_end_time = (int)MusicPlayerManager.ActivityPlayer.Length; if (option.IsExplicitTimeRange) { calc_end_time = option.EndTime; calc_start_time = option.StartTime; while (audio_reader.NextFrame(audio_frame, audio_decoder.StreamIndex)) { pos = audio_reader.Position.TotalMilliseconds; if (pos >= option.StartTime) { break; } } } while (writer != null) { if (pos >= MusicPlayerManager.ActivityPlayer.CurrentTime) { continue; } if (!audio_reader.NextFrame(audio_frame, audio_decoder.StreamIndex)) { break; } pos = audio_reader.Position.TotalMilliseconds; if (option.IsExplicitTimeRange && (pos < option.StartTime || pos > option.EndTime)) { break; } lock (this) { writer.Write(audio_frame); } Log.Debug($"{audio_encoder.FullName} ---> ({audio_encoder.InputFrames}) {audio_encoder.InputTimestamp}"); } Log.User($"Finish audio encoding..."); }
static void Main(string[] args) { var media = new MediaReader(@"Z:\【神之超赛】来自深渊+01话.mp4"); var decoder = media.Decoders.OfType <VideoDecoder>().First(); decoder.OutFormat = new VideoFormat(decoder.InFormat.Width, decoder.InFormat.Height, AVPixelFormat.Bgr24, 4); VideoFrame frame = new VideoFrame(); for (int i = 0; i < 10; i++) { if (media.NextFrame(frame, decoder.StreamIndex)) { Bitmap image = new Bitmap(frame.Format.Width, frame.Format.Height, frame.Format.Strides[0], PixelFormat.Format24bppRgb, frame.Scan0); image.Save($@"Z:\{i}.png"); } } }
public override void Execute(byte[] bytes) { if (bytes.Length == 0) { return; } var aa = Thread.CurrentThread.ManagedThreadId.ToString(); Console.WriteLine($"DecodeDataImpl {aa} ==> {bytes.Length}"); if (null == stream) { stream = new MemoryStream(); } else { try { stream.Seek(0, SeekOrigin.Begin); } catch (Exception ex) { stream = new MemoryStream(); Console.WriteLine($"Execute {ex.ToString()}"); } } stream.Write(bytes, 0, bytes.Length); stream.Seek(0, SeekOrigin.Begin); if (null == media) { media = new MediaReader(stream); decoder = media.Decoders.OfType <VideoDecoder>().First(); decoder.OutFormat = new VideoFormat(decoder.InFormat.Width, decoder.InFormat.Height, AVPixelFormat.Bgr24, 4); } if (null == frame) { frame = new VideoFrame(); } if (media.NextFrame(frame, decoder.StreamIndex)) { Bitmap image = new Bitmap(frame.Format.Width, frame.Format.Height, frame.Format.Strides[0], PixelFormat.Format24bppRgb, frame.Scan0); ImgMgr.Get().SetImg("live.png", image); } }
unsafe static void Main(string[] args) { using (var reader = new MediaReader(@"D:\MyDocuments\Music\HEALTH\04 21世紀難民 feat. れをる(from REOL).m4a")) { var decoder = reader.Decoders.OfType <AudioDecoder>().First(); var frame = new AudioFrame(); using (var writer = new MediaWriter(@"D:\test.flac").AddAudio(decoder.OutFormat, BitRate.Zero).Initialize()) { var enc = writer.Encoders[0] as AudioEncoder; while (reader.NextFrame(frame, decoder.StreamIndex)) { var pos = reader.Position; writer.Write(frame); Console.Write($"\rframes: {enc.InputFrames}, time: {enc.InputTimestamp}"); } writer.Flush(); Console.WriteLine($"\rframes: {enc.InputFrames}, time: {enc.InputTimestamp}"); } } }
unsafe static void Main(string[] args) { using (var reader = new MediaReader(@"D:\MyDocuments\Music\夕立のりぼん+inst(NoMastering)_island__201411091428.mp3")) { var decoder = reader.Decoders.OfType <AudioDecoder>().First(); var frame = new AudioFrame(); using (var writer = new MediaWriter(@"D:\MyDocuments\Music\夕立のりぼん+inst(NoMastering)_island__201411091428-output.mp3").AddAudio(decoder.OutFormat, BitRate._320Kbps).Initialize()) { var enc = writer.Encoders[0] as AudioEncoder; while (reader.NextFrame(frame, decoder.StreamIndex)) { var pos = reader.Position; writer.Write(frame); Console.Write($"\rframes: {enc.InputFrames}, time: {enc.InputTimestamp}"); } writer.Flush(); Console.WriteLine($"\rframes: {enc.InputFrames}, time: {enc.InputTimestamp}"); } } }
public int Read(byte[] buffer, int start, int length) { if (over == 0) { if (!media.NextFrame(frame, decoder.StreamIndex)) { Position = TimeSpan.Zero; return(0); } frame.ToPacked(); over = frame.TotalBytes; } int writeLength = Math.Min(over, length); Marshal.Copy(frame.Data[0] + frame.TotalBytes - over, buffer, start, writeLength); over -= writeLength; return(writeLength); }
static void Main(string[] args) { var httpRequest = WebRequest.CreateHttp("https://txy.live-play.acgvideo.com/live-txy/148029/live_16159326_5264177.flv?wsSecret=f120730be328d6c1bdbe50dc42f1fc65&wsTime=1510802458"); httpRequest.Method = "GET"; var httpResponse = httpRequest.GetResponse() as HttpWebResponse; var reader = new MediaReader(httpResponse.GetResponseStream()); var decoder = reader.Decoders.OfType <VideoDecoder>().First(); decoder.OutFormat = new VideoFormat(decoder.InFormat.Width, decoder.InFormat.Height, AVPixelFormat.Bgr24, 4); var frame = new VideoFrame(); for (int i = 0; i < 100; i++) { reader.NextFrame(frame, decoder.StreamIndex); var bitmap = new Bitmap(frame.Format.Width, frame.Format.Height, frame.Format.Strides[0], System.Drawing.Imaging.PixelFormat.Format24bppRgb, frame.Scan0); bitmap.Save($@"Z:\{i}.png"); } }
static void Main(string[] args) { var reader = new MediaReader(@"G:\请问您今天来点兔子吗\[Hakugetsu&VCB-Studio]Gochuumon wa Usagi Desuka[1080p]\[Hakugetsu&VCB-Studio]Gochuumon wa Usagi Desuka[01][Hi10p_1080p][x264_2flac].mkv"); var decoder = reader.Decoders.OfType <VideoDecoder>().First(); decoder.OutFormat = new VideoFormat( decoder.InFormat.Width, decoder.InFormat.Height, AVPixelFormat.Yuv420p); VideoFrame frame = new VideoFrame(); for (int i = 0; i < 10; i++) { if (reader.NextFrame(frame, decoder.StreamIndex)) { var writer = File.OpenWrite($@"Z:\{i}.yuv"); Write(writer, frame.Data[0], frame.Format.Strides[0] * frame.Format.Height); Write(writer, frame.Data[1], frame.Format.Strides[1] * frame.Format.Height / 2); Write(writer, frame.Data[2], frame.Format.Strides[2] * frame.Format.Height / 2); writer.Close(); } } }
unsafe static void Main(string[] args) { const double MinimumFrequency = 10; const double MaximumFrequency = 20000; const double MaxDB = 65; const int fftSize = 4192 * 6; var reader = new MediaReader(@"D:\CloudMusic\MAN WITH A MISSION - My Hero.mp3"); var decoder = reader.Decoders.OfType <AudioDecoder>().First(); var videoFormat = new VideoFormat(1280, 720, AVPixelFormat.Rgb0); var writer = new MediaWriter(@"D:\CloudMusic\MAN WITH A MISSION - My Hero-fft.mkv") .AddEncoder(new VideoEncoder(AVCodecID.H264, videoFormat, new VideoEncoderParameters { FrameRate = new Fraction(30), GopSize = 10 })) .AddEncoder(new AudioEncoder(AVCodecID.Mp3, decoder.InFormat)) //.AddVideo(videoFormat, new VideoEncoderParameters { FrameRate = new Fraction(30), GopSize = 10 }) //.AddAudio(decoder.InFormat) .Initialize(); int sampleRate = decoder.InFormat.SampleRate; int channels = decoder.InFormat.Channels; var resampler = new AudioResampler(decoder.InFormat, new AudioFormat(sampleRate, channels, 64)); var inFrame = new AudioFrame(); var outFrame = new AudioFrame(); var image = new VideoFrame(videoFormat); var viewHeight = videoFormat.Height / 2; var observer = new StreamObserver <double>(fftSize * 2, fftSize / 6, 2); var fft = DoubleFFT.Create(fftSize); var inFFT = fft.AllocInput(); var outFFT = fft.AllocOutput(); var cutLength = FFTTools.CutFrequencyLength(fftSize, MinimumFrequency, MaximumFrequency, sampleRate, fft.FFTComplexCount); var cutFFT = Marshal.AllocHGlobal(cutLength * sizeof(double)); var outFFT2 = Marshal.AllocHGlobal(fft.FFTComplexCount * sizeof(double)); var outFFTFinal = Marshal.AllocHGlobal(viewHeight * sizeof(double)); var window = new BlackmanHarrisWindow(fftSize); var log = new Spectrum3DLog(); void FFT() { window.Apply((double *)inFFT, (double *)inFFT); fft.Execute(inFFT, outFFT); FFTTools.Abs(fftSize, (double *)outFFT, (double *)outFFT2); FFTTools.CutFrequency(fftSize, (double *)outFFT2, fft.FFTComplexCount, MinimumFrequency, MaximumFrequency, sampleRate, (double *)cutFFT, cutLength); FFTTools.Logarithm((double *)cutFFT, cutLength, MinimumFrequency, MaximumFrequency, (double *)outFFTFinal, viewHeight, log); FFTTools.ToDB((double *)outFFTFinal, viewHeight, MaxDB); FFTTools.Scale((double *)outFFTFinal, viewHeight, 1 / MaxDB); } void LeftShiftImage() { int w = image.Format.Width - 1; int h = image.Format.Height; for (int y = 0; y < h; y++) { var p = (uint *)(image.Data[0] + image.Format.Strides[0] * y); for (int x = 0; x < w; x++) { p[x] = p[x + 1]; } } } observer.Completed += data => { LeftShiftImage(); int w = image.Format.Width - 1; int h = image.Format.Height; var p = (byte *)((uint *)image.Data[0] + w); var stride = image.Format.Strides[0]; for (int i = 0; i < fftSize; i++) { ((double *)inFFT)[i] = ((double *)data)[2 * i]; } FFT(); for (int y = 0; y < viewHeight; y++, p += stride) { var val = ((double *)outFFTFinal)[viewHeight - y - 1] * 256; if (val < 0) { val = 0; } else if (val > 255) { val = 255; } p[0] = p[1] = p[2] = (byte)val; } for (int i = 0; i < fftSize; i++) { ((double *)inFFT)[i] = ((double *)data)[2 * i + 1]; } FFT(); for (int y = 0; y < viewHeight; y++, p += stride) { var val = ((double *)outFFTFinal)[viewHeight - y - 1] * 256; if (val < 0) { val = 0; } else if (val > 255) { val = 255; } p[0] = p[1] = p[2] = (byte)val; } }; bool end = false, run = true; while (run) { writer.Write(encoder => { switch (encoder) { case AudioEncoder audioEncoder: Console.Write($"\r{audioEncoder.InputTimestamp}"); if (reader.NextFrame(inFrame, decoder.StreamIndex)) { resampler.Resample(inFrame, outFrame); observer.Write(outFrame.Data[0], outFrame.SampleCount * channels); return(inFrame); } else { resampler.ResampleFinal(outFrame); observer.Write(outFrame.Data[0], outFrame.SampleCount * channels); end = true; Console.WriteLine($"\r{audioEncoder.InputTimestamp}"); return(null); } case VideoEncoder videoEncoder: if (end) { run = false; } return(image); default: throw new NotImplementedException(); } }); } writer.Dispose(); }
static void Main(string[] args) { var media = new MediaReader(@"D:\MyDocuments\Videos\bandicam 2018-05-07 20-38-22-722.mp4"); var decoder = media.Decoders.OfType <VideoDecoder>().First(); decoder.OutFormat = new VideoFormat(decoder.InFormat.Width, decoder.InFormat.Height, AVPixelFormat.Bgr24, 4); VideoFrame frame = new VideoFrame(); for (int i = 0; i < 100; i++) { if (media.NextFrame(frame, decoder.StreamIndex)) { Bitmap image = new Bitmap(frame.Format.Width, frame.Format.Height, frame.Format.Strides[0], PixelFormat.Format24bppRgb, frame.Scan0); image.Save($@"D:\MyDocuments\Videos\{i}.png"); } } //AVFormatContext* formatCtx = null; //const string filename = @"D:\MyDocuments\Music\虾米音乐\Cyua-Blumenkranz.mp3"; //FF.avformat_open_input(&formatCtx, filename, null, null).CheckFFmpegCode(); //FF.avformat_find_stream_info(formatCtx, null); //AVCodec* codec; //int index = FF.av_find_best_stream(formatCtx, AVMediaType.Audio, -1, -1, &codec, 0).CheckFFmpegCode(); //AVStream* stream = formatCtx->Streams[index]; //// var codec = FF.avcodec_find_decoder(stream->Codecpar->CodecId); //var parser = FF.av_parser_init(codec->Id); //var codecCtx = FF.avcodec_alloc_context3(codec); //FF.avcodec_parameters_to_context(codecCtx, stream->Codecpar); //Debug.Print(*stream->Codecpar); //FF.avcodec_open2(codecCtx, codec, null).CheckFFmpegCode(); //// FF.av_dump_format(formatCtx, 0, filename, false); //var frame = FF.av_frame_alloc(); //AVPacket packet; //FF.av_init_packet(&packet); //packet.Data = null; //packet.Size = 0; //void decode_packet(AVPacket* _packet) { // int r = FF.avcodec_send_packet(codecCtx, _packet).CheckFFmpegCode(); // AVRational rational = codecCtx->PktTimebase; // while (r >= 0) { // r = FF.avcodec_receive_frame(codecCtx, frame); // switch (r) { // case var _ when r == Error.EAGAIN.AVError(): // Console.Write('.'); // return; // case (int)AVError.Eof: // Console.WriteLine("EOF"); // return; // case var _ when r < 0: // throw new FFmpegException(r); // } // Console.Write('!'); // } //} //while (FF.av_read_frame(formatCtx, &packet) >= 0) { // if (packet.StreamIndex == index) { // decode_packet(&packet); // } // FF.av_packet_unref(&packet); //} //decode_packet(null); }