Xabe.FFmpeg
Xabe.FFmpeg copied to clipboard
not able to use opus as audio codec
Hi, I am not able to use OPUS as audio encoder. Switching the audio codec on FLAC, and it starts to work again.
I get System.NullReferenceException: 'Object reference not set to an instance of an object.'
Also setting the number of channels to 2 - STEREO, has no effect. It still encodes with the number of channels from the source audio stream.
I use the same sample code from documentation. Here is the code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xabe.FFmpeg;
namespace xabe_1
{
class Program
{
static void Main(string[] args)
{
//FFmpeg.ExecutablesPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FFmpeg");
FFmpeg.SetExecutablesPath(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
testFFMPEG().Wait();
}
static async Task testFFMPEG()
{
string outputPath = @"F:\encodded\george.mkv";
IMediaInfo mediaInfo = await FFmpeg.GetMediaInfo(@"F:\encodded\Star.Wars.Episode.I.The.Phantom.Menace.1999.1080p.BluRay.DTS-HD.MA.6.1.x264-LEGi0N.sample.mkv");
foreach(var vstream in mediaInfo.VideoStreams.ToList())
{
Console.WriteLine("stream: {0}", vstream.Duration);
}
IStream videoStream = mediaInfo.VideoStreams.FirstOrDefault()?.SetCodec(VideoCodec.hevc).SetBitrate(9000000);
IStream audioStream = mediaInfo.AudioStreams.FirstOrDefault()?.SetCodec(AudioCodec.opus).SetChannels(2);
var cancellationTokenSource = new CancellationTokenSource();
var myConversion = FFmpeg.Conversions.New();
myConversion.OnProgress += (sender, args) =>
{
var percent = (int)(Math.Round(args.Duration.TotalSeconds / args.TotalLength.TotalSeconds, 2) * 100);
Console.WriteLine($"[{args.Duration} / {args.TotalLength}] {percent}%");
};
myConversion.OnDataReceived += (sender, args) =>
{
Console.WriteLine($"{args.Data}{Environment.NewLine}");
};
myConversion.UseHardwareAcceleration(HardwareAccelerator.qsv, VideoCodec.h264, VideoCodec.hevc, 0)
.SetPixelFormat(PixelFormat.yuv420p10le).UseMultiThread(4).SetPreset(ConversionPreset.UltraFast);
//.AddParameter("-hwaccel qsv", ParameterPosition.PreInput)
//.AddParameter("-c:v hevc_qsv", ParameterPosition.PostInput);
cancellationTokenSource.CancelAfter(30000);
try
{
await myConversion.AddStream(audioStream, videoStream).SetOutput(outputPath).Start(cancellationTokenSource.Token);
}
catch(Exception e)
{
Console.WriteLine(e.InnerException.Message);
}
}
}
}
hi, I have changed in AudioCodecs.cs to change opus to libopus. with the ffmpeg from gyan I get exeption but with the one from BtbN it works fine.
However, now the problem changed that the bitrate applied to audio stream is not take into consideration. Setting it manually with AddParameter works fine. What could be the cause?
Could you also include the change with the opus -> libopus in the next version release? thanks
myConversion.UseHardwareAcceleration(HardwareAccelerator.qsv, VideoCodec.h264, VideoCodec.hevc_qsv, 0).SetPixelFormat(PixelFormat.yuv420p10le).UseMultiThread(4)
.AddParameter("-ac 2", ParameterPosition.PostInput).AddParameter("-b:a 512k", ParameterPosition.PostInput);
Hello @georgnistor Start method returns IConversionResult object. There are arguments passed to ffmpeg. Can you share it?
[opus @ 00000249172da240] The encoder 'opus' is experimental but experimental codecs are not enabled, add '-strict -2' if you want to use it.
[opus @ 00000249172da240] Alternatively use the non experimental encoder 'libopus'.
Conversion failed!
adding
.AddParameter("-strict -2", ParameterPosition.PostInput)
does the job