Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
libavformat/libavcodec providing invalid container header
14 décembre 2017, par seanr8I'm using libavcodec to encode a stream to h264 and libavformat to store it in an mp4. The resulting container has an invalid header that can be played in VLC, but not any other player.
I've found that using the mp4 container and the "mpeg4" codec produces a valid mp4 file, but using libx265 (HEVC) or the libx264 codec produces invalid mp4s.
I can use
ffmpeg -i invalid.mp4 -vcodec copy valid.mp4
and I get a file of almost the exact same size, but in a valid container.Examples of these files are here: Broken file and Repaied file [use the download links in the upper right to examine]
I used a hex editor to see the differences in the headers of the two files and the invalid one is 1 byte smaller than the valid one.
The code I'm using to open the container and codec and to write the header is here:
AVOutputFormat *container_format; AVFormatContext *container_format_context; AVStream *video_stream; int ret; /* allocate the output media context */ avformat_alloc_output_context2(&container_format_context, NULL, NULL, out_file); if (!container_format_context) { log(INFO, "Unable to determine container format from filename, exiting\n"); exit(1); } else { log(INFO, "Using container %s\n", container_format_context->oformat->name); } if (!container_format_context) { log(ERROR, "Could not build container format context. Encoding failed."); exit(1); } container_format = container_format_context->oformat; /* Pull codec based on name */ AVCodec* codec = avcodec_find_encoder_by_name(codec_name); if (codec == NULL) { log(ERROR, "Failed to locate codec \"%s\".", codec_name); exit(1); } /* create stream */ video_stream = NULL; video_stream = avformat_new_stream(container_format_context, codec); if (!video_stream) { log(ERROR, "Could not allocate encoder stream. Cannot continue.\n"); exit(1); } video_stream->id = container_format_context->nb_streams - 1; video_stream->time_base = video_stream->codec->time_base = (AVRational) { 1, 25}; av_dump_format(container_format_context, 0, out_file, 1); /* Retrieve encoding context */ AVCodecContext* avcodec_context = video_stream->codec; if (avcodec_context == NULL) { log(ERROR, "Failed to allocate context for " "codec \"%s\".", codec_name); exit(1); } /* Init context with encoding parameters */ avcodec_context->bit_rate = bitrate; avcodec_context->width = width; avcodec_context->height = height; avcodec_context->gop_size = 10; avcodec_context->max_b_frames = 1; avcodec_context->qmax = 31; avcodec_context->qmin = 2; avcodec_context->pix_fmt = AV_PIX_FMT_YUV420P; av_dump_format(container_format_context, 0, out_file, 1); /* Open codec for use */ if (avcodec_open2(avcodec_context, codec, NULL) < 0) { log(ERROR, "Failed to open codec \"%s\".", codec_name); exit(1); } /* Allocate corresponding frame */ AVFrame* frame = av_frame_alloc(); if (frame == NULL) { exit(1); } /* Copy necessary data for frame from avcodec_context */ frame->format = avcodec_context->pix_fmt; frame->width = avcodec_context->width; frame->height = avcodec_context->height; /* Allocate actual backing data for frame */ if (av_image_alloc(frame->data, frame->linesize, frame->width, frame->height, frame->format, 32) < 0) { exit(1); } /* open the output file, if the container needs it */ if (!(container_format->flags & AVFMT_NOFILE)) { ret = avio_open(&container_format_context->pb, out_file, AVIO_FLAG_WRITE); if (ret < 0) { log(ERROR, "Error occurred while opening output file: %s\n", av_err2str(ret)); exit(1); } } /* write the stream header, if needed */ ret = avformat_write_header(container_format_context, NULL); if (ret < 0) { log(ERROR, "Error occurred while writing output file header: %s\n", av_err2str(ret)); }
The code to encode a frame is here:
/* Init video packet */ AVPacket packet; av_init_packet(&packet); /* Request that encoder allocate data for packet */ packet.data = NULL; packet.size = 0; /* Write frame to video */ int got_data; if (avcodec_encode_video2(avcontext, &packet, frame, &got_data) < 0) { log(WARNING, "Error encoding frame #%" PRId64, video_struct->next_pts); return -1; } /* Write corresponding data to file */ if (got_data) { if (packet.pts != AV_NOPTS_VALUE) { packet.pts = av_rescale_q(packet.pts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base); } if (packet.dts != AV_NOPTS_VALUE) { packet.dts = av_rescale_q(packet.dts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base); } write_packet(video_struct, &packet, packet.size); av_packet_unref(&packet); }
And the code to write the packet to the video stream:
static int write_packet(video_struct* video, void* data, int size) { int ret; /* use AVStream is not null, otherwise write to output fd */ AVPacket *pkt = (AVPacket*) data; pkt->stream_index = video->output_stream->index; ret = av_interleaved_write_frame(video->container_format_context, pkt); if (ret != 0) { return -1; } /* Data was written successfully */ return ret; }
-
Lambda node.js function with videothumbnails
14 décembre 2017, par KaterinaI'm relatively new to AWS Lambda, and I need to develop a Lambda function that uses ffmpeg for creating thumbnails from video. I have uses few packages, but with every one I get errors, usually with starting the process for ffmpeg.
Package I have used so far: link
Could someone provide me with some useful tutorial on how to develop this kind of function?
I'd appreciate any type of help.
Regards, Katerina
-
How can I merge .webm (Audio) file and a .mp4 (Video) file using java ?
14 décembre 2017, par user2136160I have two files one is a .webm audio file and the other one is a .mp4 video file Is there a way to combine these two files together using java?
Thanks in advance.
-
QCamera switch input pin number
14 décembre 2017, par PreonixI have chinese video grabber which should work with my program. It has 2 connection variants: S-VIDEO and COMPOSITE.
In native program bundled with this hardware I can switch input source from s-video to composite and vice versa. But I need to do it in my programm and I've founded solution with the help of FFMPEG. Just switch pin_number and that's it. No need to know driver API of hardware.
Another problem is that I can not attach this library to my CMAKE project, and I just thought that Qt could has some "switch pin_number" aswell, but I cant find anything useful in qt-docs.
Thanks for any advices and sorry for my english, cheers!
-
.mp4 file won't play in the browser, is some weird codec being used ?
14 décembre 2017, par AntoineI'm trying to investigate why a certain video is not playing properly in the browser, you can see here the video compared with a video that works properly, also you can see that on my desktop (OS X), it shows a preview the second video file but not a preview of the first (implicating to me that there is something going on with a codec)
Codec as per VLC of the first video (doesn't work in browser):
Codec as per VLC of the second video (works fine in browser):
Comparison of the videos on my desktop: