/// <summary> /// Used for encoding audio samples into a stream /// </summary> /// <param name="destinationStream">Output stream</param> /// <param name="channels">Input number of channels</param> /// <param name="sampleRate">Input sample rate</param> /// <param name="bitDepth">Input bits per sample</param> /// <param name="encoderOptions">Extra FFmpeg encoding options that will be passed to FFmpeg</param> /// <param name="ffmpegExecutable">Name or path to the ffmpeg executable</param> public AudioWriter(Stream destinationStream, int channels, int sampleRate, int bitDepth = 16, FFmpegAudioEncoderOptions encoderOptions = null, string ffmpegExecutable = "ffmpeg") { if (channels <= 0 || sampleRate <= 0) { throw new InvalidDataException("Channels/Sample rate have to be bigger than 0!"); } if (bitDepth != 16 && bitDepth != 24 && bitDepth != 32) { throw new InvalidOperationException("Acceptable bit depths are 16, 24 and 32"); } if (destinationStream == null) { throw new NullReferenceException("Stream can't be null!"); } UseFilename = false; ffmpeg = ffmpegExecutable; Channels = channels; BitDepth = bitDepth; SampleRate = sampleRate; DestinationStream = destinationStream; EncoderOptions = encoderOptions ?? new FFmpegAudioEncoderOptions(); }
/// <summary> /// Used for encoding audio samples into a new audio file /// </summary> /// <param name="filename">Output audio file name/path</param> /// <param name="channels">Input number of channels</param> /// <param name="sampleRate">Input sample rate</param> /// <param name="bitDepth">Input bits per sample</param> /// <param name="encoderOptions">Extra FFmpeg encoding options that will be passed to FFmpeg</param> /// <param name="ffmpegExecutable">Name or path to the ffmpeg executable</param> public AudioWriter(string filename, int channels, int sampleRate, int bitDepth = 16, FFmpegAudioEncoderOptions encoderOptions = null, string ffmpegExecutable = "ffmpeg") { if (channels <= 0 || sampleRate <= 0) { throw new InvalidDataException("Channels/Sample rate have to be bigger than 0!"); } if (bitDepth != 16 && bitDepth != 24 && bitDepth != 32) { throw new InvalidOperationException("Acceptable bit depths are 16, 24 and 32"); } if (string.IsNullOrEmpty(filename)) { throw new NullReferenceException("Filename can't be null or empty!"); } UseFilename = true; ffmpeg = ffmpegExecutable; Channels = channels; BitDepth = bitDepth; SampleRate = sampleRate; Filename = filename; EncoderOptions = encoderOptions ?? new FFmpegAudioEncoderOptions(); }
/// <summary> /// Opens output stream for writing and returns both the input and output streams. /// </summary> /// <param name="options">Output options</param> /// <param name="process">FFmpeg process</param> /// <param name="inputArguments">Input arguments (such as -f, -channels, -sample_rate,...)</param> /// <param name="ffmpegExecutable">Name or path to the ffmpeg executable</param> public static (Stream Input, Stream Output) StreamToStream(FFmpegAudioEncoderOptions options, out Process process, string inputArguments = "", string ffmpegExecutable = "ffmpeg") { var(input, output) = FFmpegWrapper.Open(ffmpegExecutable, $"{inputArguments} -i - " + $"-c:a {options.EncoderName} {options.EncoderArguments} -f {options.Format} -", out process); return(input, output); }
/// <summary> /// Uses input file and returns the output stream. /// </summary> /// <param name="inputFilename">Input audio file name/path</param> /// <param name="options">Output options</param> /// <param name="process">FFmpeg process</param> /// <param name="inputArguments">Input arguments (such as -f, -channels, -sample_rate,...)</param> /// <param name="ffmpegExecutable">Name or path to the ffmpeg executable</param> public static Stream FileToStream(string inputFilename, FFmpegAudioEncoderOptions options, out Process process, string inputArguments = "", string ffmpegExecutable = "ffmpeg") { var output = FFmpegWrapper.OpenOutput(ffmpegExecutable, $"{inputArguments} -i \"{inputFilename}\" " + $"-c:a {options.EncoderName} {options.EncoderArguments} -f {options.Format} -", out process); return(output); }
/// <summary> /// Opens output file for writing and returns the input stream. /// </summary> /// <param name="outputFilename">Output audio file name/path</param> /// <param name="options">Output options</param> /// <param name="process">FFmpeg process</param> /// <param name="inputArguments">Input arguments (such as -f, -channels, -sample_rate,...)</param> /// <param name="ffmpegExecutable">Name or path to the ffmpeg executable</param> public static Stream StreamToFile(string outputFilename, FFmpegAudioEncoderOptions options, out Process process, string inputArguments = "", bool showOutput = false, string ffmpegExecutable = "ffmpeg") { var input = FFmpegWrapper.OpenInput(ffmpegExecutable, $"{inputArguments} -i - " + $"-c:a {options.EncoderName} {options.EncoderArguments} -f {options.Format} \"{outputFilename}\"", out process, showOutput); return(input); }
/// <summary> /// Converts given input file to output file. /// </summary> /// <param name="inputFilename">Input audio file name/path</param> /// <param name="outputFilename">Input audio file name/path</param> /// <param name="options">Output options</param> /// <param name="process">FFmpeg process</param> /// <param name="inputArguments">Input arguments (such as-f, -channels, -sample_rate,...)</param> /// <param name="ffmpegExecutable">Name or path to the ffmpeg executable</param> public static void FileToFile(string inputFilename, string outputFilename, FFmpegAudioEncoderOptions options, out Process process, string inputArguments = "", bool showOutput = false, string ffmpegExecutable = "ffmpeg") { var output = FFmpegWrapper.ExecuteCommand(ffmpegExecutable, $"{inputArguments} -i \"{inputFilename}\" " + $"-c:a {options.EncoderName} {options.EncoderArguments} -f {options.Format} \"{outputFilename}\"", showOutput); process = output; }