gpt4 book ai didi

c# - 在 Unity 中使用 autogen ffmpeg C# 从视频中提取音频

转载 作者:行者123 更新时间:2023-12-02 23:04:44 30 4
gpt4 key购买 nike

嗨,我正在使用 ffmpeg autogen 从 Unity 中的视频中提取音频,但是当我遵循此代码时,文件写入无法写入,它是 0Kb,所以这是什么问题,或者有人有任何使用此库提取音频的示例,抱歉我的英文。这是图书馆的github:
https://github.com/Ruslan-B/FFmpeg.AutoGen

unsafe void TestExtractAudio()
{

string inFile = Application.streamingAssetsPath + "/" + strFileName;
string outFile = Application.streamingAssetsPath + "/" + strFileNameAudio;

AVOutputFormat* outFormat = null;
AVFormatContext* inFormatContext = null;
AVFormatContext* outFormatContext = null;
AVPacket packet;

ffmpeg.av_register_all();

inFormatContext = ffmpeg.avformat_alloc_context();
outFormatContext = ffmpeg.avformat_alloc_context();

if (ffmpeg.avformat_open_input(&inFormatContext, inFile, null, null) < 0)
{
throw new ApplicationException("Could not open input file.");
}

if (ffmpeg.avformat_find_stream_info(inFormatContext, null) < 0)
{
throw new ApplicationException("Failed to retrieve input stream info.");
}

ffmpeg.avformat_alloc_output_context2(&outFormatContext, null, null, outFile);
if (outFormatContext == null)
{
throw new ApplicationException("Could not create output context");
}

outFormat = outFormatContext->oformat;

AVStream* inStream = inFormatContext->streams[1];
AVStream* outStream = ffmpeg.avformat_new_stream(outFormatContext, inStream->codec->codec);
if (outStream == null)
{
throw new ApplicationException("Failed to allocate output stream.");
}

if (ffmpeg.avcodec_copy_context(outStream->codec, inStream->codec) < 0)
{
throw new ApplicationException("Couldn't copy input stream codec context to output stream codec context");
}

outFormatContext->audio_codec_id = AVCodecID.AV_CODEC_ID_MP3;

int retcode = ffmpeg.avio_open(&outFormatContext->pb, outFile, ffmpeg.AVIO_FLAG_WRITE);
if (retcode < 0)
{
throw new ApplicationException("Couldn't open output file");
}

int returnCode = ffmpeg.avformat_write_header(outFormatContext, null);

if (returnCode < 0)
{
throw new ApplicationException("Error occurred opening output file.");
}

while (true)
{
if (ffmpeg.av_read_frame(inFormatContext, &packet) < 0)
{
break;
}

if (packet.stream_index == 1)
{

inStream = inFormatContext->streams[1];
outStream = outFormatContext->streams[0];

// TODO: Replicate log packet functionality to print out what's inside the packet.

packet.pts = ffmpeg.av_rescale_q_rnd(packet.pts, inStream->time_base, outStream->time_base,
AVRounding.AV_ROUND_NEAR_INF | AVRounding.AV_ROUND_PASS_MINMAX);
packet.dts = ffmpeg.av_rescale_q_rnd(packet.dts, inStream->time_base, outStream->time_base,
AVRounding.AV_ROUND_NEAR_INF | AVRounding.AV_ROUND_PASS_MINMAX);

packet.duration = ffmpeg.av_rescale_q(packet.duration, inStream->time_base, outStream->time_base);

int returncode = ffmpeg.av_interleaved_write_frame(outFormatContext, &packet);

}

ffmpeg.av_packet_unref(&packet);
}

ffmpeg.av_write_trailer(outFormatContext);


ffmpeg.avformat_close_input(&inFormatContext);

ffmpeg.avformat_free_context(outFormatContext);

Console.WriteLine("Press any key to continue...");

Console.ReadKey();
}

值 returnCode 返回小于 0,所以有人可以解决这个问题,非常感谢

最佳答案

问题在这里:

inStream = inFormatContext->streams[1];
outStream = outFormatContext->streams[0];

这段代码:
ffmpeg.av_interleaved_write_frame

进行以下验证
static int check_packet(AVFormatContext *s, AVPacket *pkt)
{
if (pkt->stream_index < 0 || pkt->stream_index >= s->nb_streams) {
av_log(s, AV_LOG_ERROR, "Invalid packet stream index: %d\n"
}

您需要更改 packet.stream_index调用前从 1 到 0 int returncode = ffmpeg.av_interleaved_write_frame(outFormatContext, &packet);
看:
if (type == AVMediaType.AVMEDIA_TYPE_AUDIO){

inStream = inFormatContext->streams[1];
outStream = outFormatContext->streams[0];

// TODO: Replicate log packet functionality to print out what's inside the packet.

ffmpeg.av_packet_rescale_ts(&packet, inStream->time_base, outStream->time_base);

packet.stream_index = 0;

int returncode = ffmpeg.av_write_frame(outFormatContext, &packet);

}

关于c# - 在 Unity 中使用 autogen ffmpeg C# 从视频中提取音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47483722/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com