- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个程序应该对输入 mpeg-ts 进行解复用,将 mpeg2 转码为 h264,然后将音频与转码的视频一起进行复用。当我用 VLC 打开生成的混合文件时,我既没有得到音频也没有得到视频。这是相关的代码。
我的主要工作循环如下:
void
*writer_thread(void *thread_ctx) {
struct transcoder_ctx_t *ctx = (struct transcoder_ctx_t *) thread_ctx;
AVStream *video_stream = NULL, *audio_stream = NULL;
AVFormatContext *output_context = init_output_context(ctx, &video_stream, &audio_stream);
struct mux_state_t mux_state = {0};
//from omxtx
mux_state.pts_offset = av_rescale_q(ctx->input_context->start_time, AV_TIME_BASE_Q, output_context->streams[ctx->video_stream_index]->time_base);
//write stream header if any
avformat_write_header(output_context, NULL);
//do not start doing anything until we get an encoded packet
pthread_mutex_lock(&ctx->pipeline.video_encode.is_running_mutex);
while (!ctx->pipeline.video_encode.is_running) {
pthread_cond_wait(&ctx->pipeline.video_encode.is_running_cv, &ctx->pipeline.video_encode.is_running_mutex);
}
while (!ctx->pipeline.video_encode.eos || !ctx->processed_audio_queue->queue_finished) {
//FIXME a memory barrier is required here so that we don't race
//on above variables
//fill a buffer with video data
OERR(OMX_FillThisBuffer(ctx->pipeline.video_encode.h, omx_get_next_output_buffer(&ctx->pipeline.video_encode)));
write_audio_frame(output_context, audio_stream, ctx); //write full audio frame
//FIXME no guarantee that we have a full frame per packet?
write_video_frame(output_context, video_stream, ctx, &mux_state); //write full video frame
//encoded_video_queue is being filled by the previous command
}
av_write_trailer(output_context);
//free all the resources
avcodec_close(video_stream->codec);
avcodec_close(audio_stream->codec);
/* Free the streams. */
for (int i = 0; i < output_context->nb_streams; i++) {
av_freep(&output_context->streams[i]->codec);
av_freep(&output_context->streams[i]);
}
if (!(output_context->oformat->flags & AVFMT_NOFILE)) {
/* Close the output file. */
avio_close(output_context->pb);
}
/* free the stream */
av_free(output_context);
free(mux_state.pps);
free(mux_state.sps);
}
static
AVFormatContext *
init_output_context(const struct transcoder_ctx_t *ctx, AVStream **video_stream, AVStream **audio_stream) {
AVFormatContext *oc;
AVOutputFormat *fmt;
AVStream *input_stream, *output_stream;
AVCodec *c;
AVCodecContext *cc;
int audio_copied = 0; //copy just 1 stream
fmt = av_guess_format("mpegts", NULL, NULL);
if (!fmt) {
fprintf(stderr, "[DEBUG] Error guessing format, dying\n");
exit(199);
}
oc = avformat_alloc_context();
if (!oc) {
fprintf(stderr, "[DEBUG] Error allocating context, dying\n");
exit(200);
}
oc->oformat = fmt;
snprintf(oc->filename, sizeof(oc->filename), "%s", ctx->output_filename);
oc->debug = 1;
oc->start_time_realtime = ctx->input_context->start_time;
oc->start_time = ctx->input_context->start_time;
oc->duration = 0;
oc->bit_rate = 0;
for (int i = 0; i < ctx->input_context->nb_streams; i++) {
input_stream = ctx->input_context->streams[i];
output_stream = NULL;
if (input_stream->index == ctx->video_stream_index) {
//copy stuff from input video index
c = avcodec_find_encoder(CODEC_ID_H264);
output_stream = avformat_new_stream(oc, c);
*video_stream = output_stream;
cc = output_stream->codec;
cc->width = input_stream->codec->width;
cc->height = input_stream->codec->height;
cc->codec_id = CODEC_ID_H264;
cc->codec_type = AVMEDIA_TYPE_VIDEO;
cc->bit_rate = ENCODED_BITRATE;
cc->time_base = input_stream->codec->time_base;
output_stream->avg_frame_rate = input_stream->avg_frame_rate;
output_stream->r_frame_rate = input_stream->r_frame_rate;
output_stream->start_time = AV_NOPTS_VALUE;
} else if ((input_stream->codec->codec_type == AVMEDIA_TYPE_AUDIO) && !audio_copied) {
/* i care only about audio */
c = avcodec_find_encoder(input_stream->codec->codec_id);
output_stream = avformat_new_stream(oc, c);
*audio_stream = output_stream;
avcodec_copy_context(output_stream->codec, input_stream->codec);
/* Apparently fixes a crash on .mkvs with attachments: */
av_dict_copy(&output_stream->metadata, input_stream->metadata, 0);
/* Reset the codec tag so as not to cause problems with output format */
output_stream->codec->codec_tag = 0;
audio_copied = 1;
}
}
for (int i = 0; i < oc->nb_streams; i++) {
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
oc->streams[i]->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
if (oc->streams[i]->codec->sample_rate == 0)
oc->streams[i]->codec->sample_rate = 48000; /* ish */
}
if (!(fmt->flags & AVFMT_NOFILE)) {
fprintf(stderr, "[DEBUG] AVFMT_NOFILE set, allocating output container\n");
if (avio_open(&oc->pb, ctx->output_filename, AVIO_FLAG_WRITE) < 0) {
fprintf(stderr, "[DEBUG] error creating the output context\n");
exit(1);
}
}
return oc;
}
static
void
write_audio_frame(AVFormatContext *oc, AVStream *st, struct transcoder_ctx_t *ctx) {
AVPacket pkt = {0}; // data and size must be 0;
struct packet_t *source_audio;
av_init_packet(&pkt);
if (!(source_audio = packet_queue_get_next_item_asynch(ctx->processed_audio_queue))) {
return;
}
pkt.stream_index = st->index;
pkt.size = source_audio->data_length;
pkt.data = source_audio->data;
pkt.pts = source_audio->PTS;
pkt.dts = source_audio->DTS;
pkt.duration = source_audio->duration;
pkt.destruct = avpacket_destruct;
/* Write the compressed frame to the media file. */
if (av_interleaved_write_frame(oc, &pkt) != 0) {
fprintf(stderr, "[DEBUG] Error while writing audio frame\n");
}
packet_queue_free_packet(source_audio, 0);
}
http://87.120.131.41/dl/mpeg4.h264
最佳答案
当不同的容器在 libav 中产生不同的结果时,这几乎总是一个时基问题。所有容器都有一个他们喜欢的 time_base,有些会接受自定义值......有时。
在将其放入容器之前,您必须重新调整时基。通常修补 mux 状态结构不是您想要做的事情,我认为您在那里所做的并没有按照您的想法做。尝试打印出所有的时基以找出它们是什么。
每一帧你至少必须重新计算 PTS。如果您在调用 encode 之前执行此操作,则编码器将生成正确的 DTS。对音频执行相同的操作,但通常将 DTS 设置为 AV_NO_PTS,有时您也可以将音频 PTS 设置为该值。要轻松重新缩放,请使用 av_rescale(...) 函数。
假设您在 MPEG-TS 容器中有 MPEG-2 数据,请小心,这并不总是正确的。
关于ffmpeg - 与 libav 混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17384047/
来自Linux背景。我只是想知道在 OSX 上安装 avprobe 的最佳方法是什么? 我尝试安装 libav:http://libav.org/download.html但这似乎使用了avconf。
如何在FFmpeg C API中编码之前为帧计算正确的PTS值? 为了进行编码,我使用了avcodec_encode_video2函数,然后通过av_interleaved_write_frame编写
使用 libav 保存视频中的帧。 问题是,如果你多次调用 decode 函数,然后调用 2nd,然后就没有正确处理。 第一次这样的结论(一切正常): [swscaler @ 0x8b48510]No
我正在尝试制作一个简单的 av 播放器,在某些情况下,我可以正确获取如下值: checking /media/timecapsule/Music/02 Baawre.mp3 [mp3 @ 0x7f06
我在OS X Mavericks上没有安装libav的运气。我已经尝试了一切。 我正在遵循此指南:http://earthwithsun.com/questions/568464/install-la
尝试使用 ffmpeg 打开网络摄像头(ffplay -f video4linux2/dev/video0 有效 pFormatCtx = NULL; av_register_all(
我使用下面的命令来转换文件 ./avconv -i inputFile -vcodec libx264 -trellis 2 -crf 23 -tune psnr -vf Crop='trunc(iw
我试图通过使用代码将 MP2 文件转换为原始 PCM 来获得与 ffmpeg/avconv 相同的输出。我使用了 the tutorial included in the FFmpeg/Libav d
我正在关注 ffmpeg 文档中的音频编码示例:https://www.ffmpeg.org/doxygen/0.6/api-example_8c-source.html 但我想解码 .wav 文件,
我正在尝试使用 libav(ffmpeg) 创建带有 MJPEG 视频负载的 RTP 流示例代码非常简单,适用于 MPEG1我看到 MJPEG 中的编码有效,但是当我需要发送 mjpeg 时帧 RTP
有没有办法使用 libav/avconv 来复制 FFMPEG 中瓦片过滤器的效果? 我正在尝试为每十秒的视频输入创建一个从左到右的图像 strip 。 我的计划是首先生成图像,然后创建图像条。最好我
我正在尝试使用 libav 在我的应用程序中删除视频的某些部分,例如在长度为 00:08:00 的视频中,我尝试删除帧 100-250 和 400-500(仅作为示例)。 我写了这段代码来复制 AVP
我有一个能够同时生成 5 个 mpeg 传输流的程序。每个流都有自己的上下文,并在独立的线程中完成。当只有一个流处于事件状态时,一切都很好。一旦我激活另一个流,我的帧速率就会大大下降。两个流仍然可以正
我有一系列编码数据包、H.264 视频和 AAC 音频。当它们出现时,我正在使用 av_write_frame 将它们写入视频文件。 连续给定以下情况 10秒视频,然后 10 秒的视频和音频,然后 1
我看到了这个答案,Libav (ffmpeg) copying decoded video timestamps to encoder 但我仍然不明白为什么我们需要流时基和编解码器时基。目前我正在尝试
我正在尝试学习使用 libav。我遵循了 dranger.com 上的第一个教程,但有一次我有点困惑。 // Write pixel data for(y=0; ydata[0]+y*pFrame->
我有一个从我的手机以纵向模式拍摄的视频。以下是有关视频的转储信息: Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'video.MOV': Metadata: maj
我有一个带有卡住(热)像素的视频,我想修复它,比如说用相邻像素的平均值替换像素。我在 Ubuntu 上,所以我想使用 libav/ffmpeg,但我真的不知道该怎么做。谁能提供一个简单的解决方案? 最
这个问题在这里已经有了答案: FFMPEG (libx264) "height not divisible by 2" (7 个回答) 5年前关闭。 我需要批量处理一堆视频以将它们的高度缩放到 240
我目前正在使用 libav 将视频的音频流提取到原始 PCM 文件中。 此代码适用于 mp3,但当我尝试使用 mp4 视频时,Audacity 上导入的原始格式显示奇怪的 0 到 -1 之间的规则下降
我是一名优秀的程序员,十分优秀!