Commit e4f14c32 authored by Nicolas George's avatar Nicolas George

examples/muxing: improve error messages.

Illustrate the use of return values, av_err2str and
avcodec_get_name.
parent b99bef17
...@@ -63,7 +63,8 @@ static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec, ...@@ -63,7 +63,8 @@ static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
/* find the encoder */ /* find the encoder */
*codec = avcodec_find_encoder(codec_id); *codec = avcodec_find_encoder(codec_id);
if (!(*codec)) { if (!(*codec)) {
fprintf(stderr, "Could not find codec\n"); fprintf(stderr, "Could not find encoder for '%s'\n",
avcodec_get_name(codec_id));
exit(1); exit(1);
} }
...@@ -133,12 +134,14 @@ static int audio_input_frame_size; ...@@ -133,12 +134,14 @@ static int audio_input_frame_size;
static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st) static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
{ {
AVCodecContext *c; AVCodecContext *c;
int ret;
c = st->codec; c = st->codec;
/* open it */ /* open it */
if (avcodec_open2(c, codec, NULL) < 0) { ret = avcodec_open2(c, codec, NULL);
fprintf(stderr, "Could not open audio codec\n"); if (ret < 0) {
fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
exit(1); exit(1);
} }
...@@ -198,7 +201,7 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st) ...@@ -198,7 +201,7 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet); ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Error encoding audio frame\n"); fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
exit(1); exit(1);
} }
...@@ -208,8 +211,10 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st) ...@@ -208,8 +211,10 @@ static void write_audio_frame(AVFormatContext *oc, AVStream *st)
pkt.stream_index = st->index; pkt.stream_index = st->index;
/* Write the compressed frame to the media file. */ /* Write the compressed frame to the media file. */
if (av_interleaved_write_frame(oc, &pkt) != 0) { ret = av_interleaved_write_frame(oc, &pkt);
fprintf(stderr, "Error while writing audio frame\n"); if (ret != 0) {
fprintf(stderr, "Error while writing audio frame: %s\n",
av_err2str(ret));
exit(1); exit(1);
} }
avcodec_free_frame(&frame); avcodec_free_frame(&frame);
...@@ -235,8 +240,9 @@ static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st) ...@@ -235,8 +240,9 @@ static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
AVCodecContext *c = st->codec; AVCodecContext *c = st->codec;
/* open the codec */ /* open the codec */
if (avcodec_open2(c, codec, NULL) < 0) { ret = avcodec_open2(c, codec, NULL);
fprintf(stderr, "Could not open video codec\n"); if (ret < 0) {
fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
exit(1); exit(1);
} }
...@@ -250,7 +256,7 @@ static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st) ...@@ -250,7 +256,7 @@ static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
/* Allocate the encoded raw picture. */ /* Allocate the encoded raw picture. */
ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height); ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Could not allocate picture\n"); fprintf(stderr, "Could not allocate picture: %s\n", av_err2str(ret));
exit(1); exit(1);
} }
...@@ -260,7 +266,8 @@ static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st) ...@@ -260,7 +266,8 @@ static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
if (c->pix_fmt != AV_PIX_FMT_YUV420P) { if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
ret = avpicture_alloc(&src_picture, AV_PIX_FMT_YUV420P, c->width, c->height); ret = avpicture_alloc(&src_picture, AV_PIX_FMT_YUV420P, c->width, c->height);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Could not allocate temporary picture\n"); fprintf(stderr, "Could not allocate temporary picture: %s\n",
av_err2str(ret));
exit(1); exit(1);
} }
} }
...@@ -346,7 +353,7 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st) ...@@ -346,7 +353,7 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
ret = avcodec_encode_video2(c, &pkt, frame, &got_output); ret = avcodec_encode_video2(c, &pkt, frame, &got_output);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "Error encoding video frame\n"); fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
exit(1); exit(1);
} }
...@@ -364,7 +371,7 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st) ...@@ -364,7 +371,7 @@ static void write_video_frame(AVFormatContext *oc, AVStream *st)
} }
} }
if (ret != 0) { if (ret != 0) {
fprintf(stderr, "Error while writing video frame\n"); fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
exit(1); exit(1);
} }
frame_count++; frame_count++;
...@@ -389,7 +396,7 @@ int main(int argc, char **argv) ...@@ -389,7 +396,7 @@ int main(int argc, char **argv)
AVStream *audio_st, *video_st; AVStream *audio_st, *video_st;
AVCodec *audio_codec, *video_codec; AVCodec *audio_codec, *video_codec;
double audio_pts, video_pts; double audio_pts, video_pts;
int i; int ret, i;
/* Initialize libavcodec, and register all codecs and formats. */ /* Initialize libavcodec, and register all codecs and formats. */
av_register_all(); av_register_all();
...@@ -441,15 +448,19 @@ int main(int argc, char **argv) ...@@ -441,15 +448,19 @@ int main(int argc, char **argv)
/* open the output file, if needed */ /* open the output file, if needed */
if (!(fmt->flags & AVFMT_NOFILE)) { if (!(fmt->flags & AVFMT_NOFILE)) {
if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) { ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
fprintf(stderr, "Could not open '%s'\n", filename); if (ret < 0) {
fprintf(stderr, "Could not open '%s': %s\n", filename,
av_err2str(ret));
return 1; return 1;
} }
} }
/* Write the stream header, if any. */ /* Write the stream header, if any. */
if (avformat_write_header(oc, NULL) < 0) { ret = avformat_write_header(oc, NULL);
fprintf(stderr, "Error occurred when opening output file\n"); if (ret < 0) {
fprintf(stderr, "Error occurred when opening output file: %s\n",
av_err2str(ret));
return 1; return 1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment