/// <summary> /// Prepare the watcher to convert files /// </summary> public static void RunWatcher(string Path, string Filter, InFormat Format) { // Log Console.WriteLine("Watcher mode started..."); Console.WriteLine(" - Path: \"" + Path + "\""); Console.WriteLine(" - Watching for: \"" + Filter + "\" files"); // Set WatcherMode = true; InputMode = Format; // File watcher var Watcher = new FileSystemWatcher(); Watcher.Path = Path; Watcher.NotifyFilter = NotifyFilters.FileName; Watcher.Filter = "*.*"; Watcher.Created += Watcher_Created; Watcher.EnableRaisingEvents = true; Watcher.IncludeSubdirectories = true; // Split it FileExtensions = Filter.Split('|').ToList(); // Start conversion thread var Converter = Task.Run((Action)ConversionWatcher); // Wait until end new System.Threading.AutoResetEvent(false).WaitOne(); // Stop WatcherMode = false; Converter.Dispose(); }
public Task <HPacket> ReceivePacketAsync() { if (InFormat == null) { throw new NullReferenceException("Incoming format cannot be null."); } return(InFormat.ReceivePacketAsync(this)); }
/// <summary> /// recording audio. /// <para> /// first set inputDeviceName = null, you will get inputDeviceName list in vs output, /// </para> /// <para> /// then set inputDeviceName to your real device name and run again,you will get a audio output. /// </para> /// <para> /// if you want stop record, exit console; /// </para> /// <para>ffmpeg </para> /// </summary> /// <param name="outputFile"></param> public RecordingAudio(string outputFile, string inputDeviceName = null) { // console output FFmpegHelper.SetupLogging(logWrite: _ => Console.Write(_)); // register all device FFmpegHelper.RegisterDevice(); var dshowInput = new InFormat("dshow"); // list all "dshow" device at console output, ffmpeg does not support direct reading of device names MediaDevice.PrintDeviceInfos(dshowInput, "list", MediaDevice.ListDevicesOptions); if (string.IsNullOrWhiteSpace(inputDeviceName)) { return; } // get your audio input device name from console output // NOTE: DO NOT delete "audio=" using (MediaReader reader = new MediaReader($"audio={inputDeviceName}", dshowInput)) using (MediaWriter writer = new MediaWriter(outputFile)) { var stream = reader.Where(_ => _.Codec.Type == AVMediaType.AVMEDIA_TYPE_AUDIO).First(); writer.AddStream(MediaEncode.CreateAudioEncode(writer.Format, stream.Codec.AVCodecContext.channels, stream.Codec.AVCodecContext.sample_rate)); writer.Initialize(); AudioFrame dstFrame = AudioFrame.CreateFrameByCodec(writer[0].Codec); SampleConverter converter = new SampleConverter(dstFrame); long pts = 0; foreach (var packet in reader.ReadPacket()) { foreach (var frame in stream.ReadFrame(packet)) { foreach (var dstframe in converter.Convert(frame)) { pts += dstFrame.AVFrame.nb_samples; dstFrame.Pts = pts; foreach (var dstpacket in writer[0].WriteFrame(dstFrame)) { writer.WritePacket(dstpacket); } } } } writer.FlushMuxer(); } }