static bool EncodeImageToMpeg2Video(string imageFile, string outputFile, string encodingPreset) { Transcoder transcoder = new Transcoder(); // In order to use the OEM release for testing (without a valid license) the transcoder demo mode must be enabled. transcoder.AllowDemoMode = true; try { File.Delete(outputFile); // Configure Input { MediaInfo info = new MediaInfo(); info.InputFile = imageFile; if (!info.Load()) { PrintError("MediaInfo load", info.Error); return(false); } MediaSocket socket = MediaSocket.FromMediaInfo(info); transcoder.Inputs.Add(socket); } // Configure Output { MediaSocket socket = MediaSocket.FromPreset(encodingPreset); socket.File = outputFile; transcoder.Outputs.Add(socket); } // Encode Images if (!transcoder.Open()) { PrintError("Transcoder open", transcoder.Error); return(false); } if (!transcoder.Run()) { PrintError("Transcoder run", transcoder.Error); return(false); } transcoder.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return(false); } finally { transcoder.Dispose(); } return(true); }
public void Close() { if (null != _transcoder) { _transcoder.Dispose(); _transcoder = null; } _decoderThread = null; _videoStreamInfo = null; _audioStreamInfo = null; _videoStreamIndex = -1; _audioStreamIndex = -1; _decoderEOS = false; _cancellationPending = false; }
static void EncodeDirectShowInput(string inputFile, string outputFile, string preset) { if (File.Exists(outputFile)) { File.Delete(outputFile); } DSGraph dsGraph = new DSGraph(); Transcoder transcoder = new Transcoder(); // In order to use the OEM release for testing (without a valid license) the transcoder demo mode must be enabled. transcoder.AllowDemoMode = true; try { Console.WriteLine("Initializing DirectShow graph."); /* * If the source is a DirectShow filter instead of a file then: * 1) Create an instance of the source filter * 2) Configure source filter * 3) Call dsGraph.Init(sourceFilter); * * For example dsGraph.Init(inputFile) can be replaced with the following code: * * 1) Create an instance of the source filter * * // FileSourceAsync filter * IBaseFilter sourceFilter = Util.CreateFilter(new Guid("e436ebb5-524f-11ce-9f53-0020af0ba770")); * * // or WM ASF Reader filter * IBaseFilter sourceFilter = Util.CreateFilter(new Guid("187463A0-5BB7-11D3-ACBE-0080C75E246E")); * * 2) Configure source filter * IFileSourceFilter fileSourceFilter = sourceFilter as IFileSourceFilter; * fileSourceFilter.Load(inputFile, null); * * 3) * dsGraph.Init(sourceFilter); */ dsGraph.Init(inputFile); if (dsGraph.videoGrabber != null) { ConfigureVideoInput(dsGraph, transcoder); } if (dsGraph.audioGrabber != null) { ConfigureAudioInput(dsGraph, transcoder); } if ((dsGraph.videoGrabber == null) && (dsGraph.audioGrabber == null)) { Console.WriteLine("No audio or video can be read from the DirectShow graph."); return; } // Configure output { MediaSocket outSocket = MediaSocket.FromPreset(preset); outSocket.File = outputFile; transcoder.Outputs.Add(outSocket); } bool res = transcoder.Open(); PrintError("Open Transcoder", transcoder.Error); if (!res) { return; } //DBG //var rot = new DsROTEntry(dsGraph.graph); Console.WriteLine("Running DirectShow graph."); int hr = dsGraph.mediaControl.Run(); DsError.ThrowExceptionForHR(hr); while (true) { FilterState fs; dsGraph.mediaControl.GetState(-1, out fs); if (fs != FilterState.Running) { break; } EventCode ev; dsGraph.mediaEvent.WaitForCompletion(1000, out ev); if (EventCode.Complete == ev) { break; } } Console.WriteLine("DirectShow graph is stopped."); if ((dsGraph.videoGrabberCB != null) && (dsGraph.videoGrabberCB.TranscoderError != null)) { PrintError("Transcoder Error", transcoder.Error); } if ((dsGraph.audioGrabberCB != null) && (dsGraph.audioGrabberCB.TranscoderError != null)) { PrintError("Transcoder Error", transcoder.Error); } Console.WriteLine("Closing transcoder."); if (!transcoder.Flush()) { PrintError("Flush Transcoder", transcoder.Error); } transcoder.Close(); } finally { dsGraph.Reset(); transcoder.Dispose(); } }
public void Dispose() { t.Dispose(); }
static bool SplitFile(string inputFile) { string outputFileExt = ".mpg"; string encodingPreset = Preset.Video.DVD.NTSC_4x3_PCM; const double splitPartDuration = 10; // seconds int audioStreamIndex = -1; int videoStreamIndex = -1; int audioFrameSize = 0; int audioSampleRate = 0; using (var transcoder1 = new Transcoder()) { // In order to use the OEM release for testing (without a valid license) the transcoder demo mode must be enabled. transcoder1.AllowDemoMode = true; using (var inputInfo = new MediaInfo()) { inputInfo.Inputs[0].File = inputFile; if (!inputInfo.Open()) { PrintError("Open MediaInfo", inputInfo.Error); return(false); } // Configure transcoder1 input and output var inputSocket = MediaSocket.FromMediaInfo(inputInfo); transcoder1.Inputs.Add(inputSocket); for (int i = 0; i < inputSocket.Pins.Count; i++) { StreamInfo inputStreamInfo = inputSocket.Pins[i].StreamInfo; if ((inputStreamInfo.MediaType == MediaType.Video) && videoStreamIndex < 0) { var streamInfo = new VideoStreamInfo(); VideoStreamInfo inputVideoStreamInfo = inputStreamInfo as VideoStreamInfo; streamInfo.ColorFormat = ColorFormat.YUV420; streamInfo.StreamType = StreamType.UncompressedVideo; streamInfo.ScanType = inputVideoStreamInfo.ScanType; streamInfo.FrameWidth = inputVideoStreamInfo.FrameWidth; streamInfo.FrameHeight = inputVideoStreamInfo.FrameHeight; streamInfo.DisplayRatioWidth = inputVideoStreamInfo.DisplayRatioWidth; streamInfo.DisplayRatioHeight = inputVideoStreamInfo.DisplayRatioHeight; var outputPin = new MediaPin(); outputPin.StreamInfo = streamInfo; var outputSocket = new MediaSocket(); outputSocket.Pins.Add(outputPin); outputSocket.StreamType = streamInfo.StreamType; videoStreamIndex = transcoder1.Outputs.Count; transcoder1.Outputs.Add(outputSocket); } if ((inputStreamInfo.MediaType == MediaType.Audio) && audioStreamIndex < 0) { var streamInfo = new AudioStreamInfo(); AudioStreamInfo inputAudioStreamInfo = inputStreamInfo as AudioStreamInfo; streamInfo.StreamType = StreamType.LPCM; streamInfo.PcmFlags = inputAudioStreamInfo.PcmFlags; streamInfo.Channels = inputAudioStreamInfo.Channels; streamInfo.SampleRate = inputAudioStreamInfo.SampleRate; streamInfo.BitsPerSample = inputAudioStreamInfo.BitsPerSample; var outputPin = new MediaPin(); outputPin.StreamInfo = streamInfo; var outputSocket = new MediaSocket(); outputSocket.Pins.Add(outputPin); outputSocket.StreamType = streamInfo.StreamType; audioStreamIndex = transcoder1.Outputs.Count; transcoder1.Outputs.Add(outputSocket); audioFrameSize = inputAudioStreamInfo.Channels * inputAudioStreamInfo.BitsPerSample / 8; audioSampleRate = inputAudioStreamInfo.SampleRate; } } } bool res = transcoder1.Open(); PrintError("Open Transcoder1", transcoder1.Error); if (!res) { return(false); } var sample = new MediaSample(); int outputIndex; int splitPartNum = 0; double splitTime = splitPartDuration; double partStartTime = 0; Transcoder transcoder2 = null; List <SplitRecord> splitStats = new List <SplitRecord>(); List <MediaSample> audioSamplesQueue = new List <MediaSample>(); try { for (; ;) { if ((audioSamplesQueue.Count > 0) && (audioSamplesQueue[0].StartTime < splitTime)) { outputIndex = audioStreamIndex; sample = audioSamplesQueue[0]; audioSamplesQueue.RemoveAt(0); } else { if (!transcoder1.Pull(out outputIndex, sample)) { break; } if ((outputIndex != audioStreamIndex) && (outputIndex != videoStreamIndex)) { continue; } } if (outputIndex == audioStreamIndex) { double sampleDuration = (double)(sample.Buffer.DataSize) / (double)(audioFrameSize * audioSampleRate); if (sample.StartTime >= splitTime) { audioSamplesQueue.Add(sample); sample = new MediaSample(); continue; } else if ((sample.StartTime + sampleDuration) > splitTime) { double sample1Duration = splitTime - sample.StartTime; int sample1BufferSize = (int)(sample1Duration * audioSampleRate) * audioFrameSize; if (sample1BufferSize < sample.Buffer.DataSize) { int buffer2Size = sample.Buffer.DataSize - sample1BufferSize; var buffer2 = new MediaBuffer(new byte[buffer2Size]); buffer2.SetData(0, buffer2Size); Array.Copy(sample.Buffer.Start, sample1BufferSize, buffer2.Start, 0, buffer2Size); var sample2 = new MediaSample(); sample2.StartTime = sample.StartTime + sample1Duration; sample2.Buffer = buffer2; if (sample1BufferSize > 0) { sample.Buffer.SetData(sample.Buffer.DataOffset, sample1BufferSize); } else { sample.Buffer.SetData(0, 0); } audioSamplesQueue.Add(sample2); } } } if ((transcoder2 == null) || ((sample.StartTime + 0.0001 >= splitTime) && (outputIndex == videoStreamIndex))) { if (transcoder2 != null) { transcoder2.Flush(); transcoder2.Close(); transcoder2.Dispose(); } SplitRecord splitStat = new SplitRecord(); splitStat.StartTime = splitTime; splitStat.StartTimeActual = sample.StartTime; splitPartNum += 1; splitTime = splitPartNum * splitPartDuration; partStartTime = sample.StartTime; transcoder2 = new Transcoder(); transcoder2.AllowDemoMode = true; // Configure transcoder2 input and output { for (int i = 0; i < transcoder1.Outputs.Count; i++) { var streamInfo = transcoder1.Outputs[i].Pins[0].StreamInfo.Clone() as StreamInfo; var pin = new MediaPin(); pin.StreamInfo = streamInfo; var socket = new MediaSocket(); socket.Pins.Add(pin); socket.StreamType = streamInfo.StreamType; transcoder2.Inputs.Add(socket); } var outputSocket = MediaSocket.FromPreset(encodingPreset); string fileName = GenerateOutputFileName(inputFile, splitPartNum) + outputFileExt; string filePath = Path.Combine(GetExeDir(), fileName); try { File.Delete(filePath); } catch { } outputSocket.File = filePath; transcoder2.Outputs.Add(outputSocket); splitStat.FileName = fileName; } if (splitStats.Count > 0) { SplitRecord lastRecord = splitStats[splitStats.Count - 1]; lastRecord.EndTime = splitStat.StartTime; lastRecord.EndTimeActual = splitStat.StartTimeActual; } splitStats.Add(splitStat); res = transcoder2.Open(); PrintError("Open Transcoder2", transcoder2.Error); if (!res) { return(false); } } if ((splitStats.Count > 0)) { SplitRecord lastRecord = splitStats[splitStats.Count - 1]; lastRecord.EndTime = sample.StartTime; lastRecord.EndTimeActual = lastRecord.EndTime; } if (sample.StartTime >= 0) { sample.StartTime = sample.StartTime - partStartTime; } res = transcoder2.Push(outputIndex, sample); if (!res) { PrintError("Push Transcoder2", transcoder2.Error); return(false); } } } finally { if (transcoder2 != null) { transcoder2.Flush(); transcoder2.Close(); transcoder2.Dispose(); transcoder2 = null; } } if ((transcoder1.Error.Facility != ErrorFacility.Codec) || (transcoder1.Error.Code != (int)CodecError.EOS)) { PrintError("Pull Transcoder1", transcoder1.Error); return(false); } transcoder1.Close(); // print split stats Console.WriteLine(); foreach (var record in splitStats) { Console.WriteLine("{0} start: {1} end: {2} act. start: {3} act. end: {4}", record.FileName, FormatTime(record.StartTime), FormatTime(record.EndTime), FormatTime(record.StartTimeActual), FormatTime(record.EndTimeActual)); } Console.WriteLine(); } return(true); }
public override void Dispose() { _audioEncoder.Dispose(); _videoEncoder.Dispose(); _mux.Dispose(); }
static bool EncodeImagesToMpeg2Video(string imagesFolder, string outputFile, int imagesCount, int videoFramesPerImage, int step, string encodingPreset) { double frameRate = -1; switch (encodingPreset) { case Preset.Video.DVD.PAL_4x3_MP2: case Preset.Video.DVD.PAL_16x9_MP2: frameRate = 25.0; break; case Preset.Video.DVD.NTSC_4x3_MP2: case Preset.Video.DVD.NTSC_16x9_MP2: frameRate = 30000.0 / 1001; // 29.97 break; default: return(false); } Transcoder transcoder = new Transcoder(); // In order to use the OEM release for testing (without a valid license) the transcoder demo mode must be enabled. transcoder.AllowDemoMode = true; try { File.Delete(outputFile); // Configure Input { MediaInfo info = new MediaInfo(); info.InputFile = GetImagePath(imagesFolder, 0); if (!info.Load()) { PrintError("MediaInfo load", info.Error); return(false); } MediaPin pin = new MediaPin(); VideoStreamInfo vInfo = (VideoStreamInfo)info.Streams[0]; vInfo.FrameRate = frameRate; pin.StreamInfo = vInfo; MediaSocket socket = new MediaSocket(); socket.Pins.Add(pin); transcoder.Inputs.Add(socket); } // Configure Output { MediaSocket socket = MediaSocket.FromPreset(encodingPreset); socket.File = outputFile; transcoder.Outputs.Add(socket); } // Encode Images if (!transcoder.Open()) { PrintError("Transcoder open", transcoder.Error); return(false); } int totalVideoFrames = imagesCount * videoFramesPerImage; for (int i = 0; i < totalVideoFrames; i++) { string imagePath = GetImagePath(imagesFolder, i / videoFramesPerImage * step); MediaBuffer mediaBuffer = new MediaBuffer(File.ReadAllBytes(imagePath)); MediaSample mediaSample = new MediaSample(); mediaSample.Buffer = mediaBuffer; mediaSample.StartTime = i / frameRate; if (!transcoder.Push(0, mediaSample)) { PrintError("Transcoder write", transcoder.Error); return(false); } } if (!transcoder.Flush()) { PrintError("Transcoder flush", transcoder.Error); return(false); } transcoder.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); return(false); } finally { transcoder.Dispose(); } return(true); }