Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
Mux raw H264 into MP4 using ffmpeg API
2 décembre 2016, par user1886318I try to mux raw H264 frames into MP4 container using ffmpeg in order to play the video with Media Source Extension in a web browser.
The following command works like a charm:
ffmpeg -i video.h264 -movflags frag_keyframe+empty_moov fragmented.mp4
Now, I would like to do the same thing using the FFmpeg C++ API.
I wrote the following source code:
#ifdef _WIN32 #define snprintf _snprintf #endif #include #include #include #include #include extern "C" { #include
avassert.h> #include channel_layout.h> #include opt.h> #include mathematics.h> #include avformat.h> #include swscale.h> #include swresample.h> } #include #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) #define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 #else #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL #endif struct timezone { int tz_minuteswest; /* minutes W of Greenwich */ int tz_dsttime; /* type of dst correction */ }; #ifdef WIN32 #include #include timeb.h> int gettimeofday(struct timeval *tp, void *tz) { struct _timeb timebuffer; _ftime(&timebuffer); tp->tv_sec = timebuffer.time; tp->tv_usec = timebuffer.millitm * 1000; return 0; } #endif // a wrapper around a single output AVStream typedef struct OutputStream { AVStream *st; } OutputStream; static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt) { return av_interleaved_write_frame(fmt_ctx, pkt); } /* Add an output stream. */ static void add_stream(OutputStream *ost, AVFormatContext *oc, AVCodec **codec, enum AVCodecID codec_id) { AVCodecContext *c; *codec = avcodec_find_encoder(codec_id); printf("avformat_new_stream\n"); ost->st = avformat_new_stream(oc, *codec); if (!ost->st) { fprintf(stderr, "Could not allocate stream\n"); exit(1); } ost->st->id = oc->nb_streams - 1; c = ost->st->codec; switch ((*codec)->type) { case AVMEDIA_TYPE_VIDEO: c->codec_id = codec_id; c->width = 352; c->height = 288; ost->st->time_base = { 1, 15 }; c->time_base = ost->st->time_base; c->pix_fmt = AV_PIX_FMT_YUV420P; c->profile = FF_PROFILE_H264_BASELINE; break; default: break; } /* Some formats want stream headers to be separate. */ if (oc->oformat->flags & AVFMT_GLOBALHEADER) c->flags |= CODEC_FLAG_GLOBAL_HEADER; } uint64_t begin_timestamp_us = 0; static int write_video_frame(AVFormatContext *oc, OutputStream *ost) { int ret = 0; AVCodecContext *c; c = ost->st->codec; struct timeval now; gettimeofday(&now, NULL); uint64_t timestamp_us = (now.tv_sec * 1000000LL + now.tv_usec - begin_timestamp_us); { { AVFormatContext *fmt_ctx = oc; const AVRational *time_base = &c->time_base; AVStream *st = ost->st; char* buf; AVPacket pkt = { 0 }; av_init_packet(&pkt); char filename[256]; static int pts = 0; snprintf(filename, 256, "avc_raw\\avc_raw_%03d.h264", pts); FILE *f = fopen(filename, "rb"); int len = 0; if (f){ fseek(f, 0, SEEK_END); // seek to end of file len = ftell(f); // get current file pointer //fseek(f, 0, SEEK_SET); // seek back to beginning of file rewind(f); buf = new char[len + 1]; // to delete fread(buf, 1, len, f); fclose(f); } else{ return 1; } int nal_type = buf[4] & 0x0f; int is_IDR_nal = 0; if (nal_type == 0x7){ //7 sps; 8 pps; IDR frame follow with sps is_IDR_nal = 1; pkt.flags |= AV_PKT_FLAG_KEY; printf("frame %d is IDR\n", pts); } pkt.stream_index = ost->st->index; pkt.data = (uint8_t*)buf; pkt.size = len; pkt.dts = pkt.pts = timestamp_us; pts++; write_frame(fmt_ctx, &c->time_base, st, &pkt); } } if (ret < 0) { fprintf(stderr, "Error while writing video frame: %s\n", ret); exit(1); } return 0; } /**************************************************************/ /* media file output */ int main(int argc, char **argv) { OutputStream video_st = { 0 }; const char *filename; AVOutputFormat *fmt; AVFormatContext *oc; AVCodec *video_codec; int ret; int encode_video = 0; AVDictionary *opt = NULL; /* Initialize libavcodec, and register all codecs and formats. */ av_register_all(); if (argc < 2) { return 1; } filename = argv[1]; oc = avformat_alloc_context(); if (!oc) { fprintf(stderr, "Memory error\n"); exit(1); } // auto detect the output format from the name. default is mpeg. AVOutputFormat * avOutputFormat = av_guess_format(NULL, filename, NULL); if (!avOutputFormat) { printf("Could not deduce output format from file extension: using MPEG.\n"); avOutputFormat = av_guess_format("mpeg", NULL, NULL); } if (!avOutputFormat) { fprintf(stderr, "Could not find suitable output format\n"); exit(1); } oc->oformat = avOutputFormat; avOutputFormat->video_codec = AV_CODEC_ID_H264; avOutputFormat->audio_codec = AV_CODEC_ID_NONE; fmt = avOutputFormat; /* Add the audio and video streams using the default format codecs * and initialize the codecs. */ if (fmt->video_codec != AV_CODEC_ID_NONE) { add_stream(&video_st, oc, &video_codec, fmt->video_codec); encode_video = 1; } av_dump_format(oc, 0, filename, 1); /* open the output file, if needed */ if (!(fmt->flags & AVFMT_NOFILE)) { ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE); if (ret < 0) { fprintf(stderr, "Could not open '%s': %s\n", filename, ret); return 1; } } // Fragmented MP4 av_dict_set(&opt, "movflags", "empty_moov+frag_keyframe", 0); /* Write the stream header, if any. */ ret = avformat_write_header(oc, &opt); if (ret < 0) { fprintf(stderr, "Error occurred when opening output file: %s\n", ret); return 1; } struct timeval now; gettimeofday(&now, NULL); begin_timestamp_us = (now.tv_sec * 1000000LL + now.tv_usec); while (encode_video) { encode_video = !write_video_frame(oc, &video_st); _sleep(1); } av_write_trailer(oc); if (!(fmt->flags & AVFMT_NOFILE)) /* Close the output file. */ avio_close(oc->pb); /* free the stream */ avformat_free_context(oc); _sleep(1000); return 0; } H264 files can be downloaded from the link: https://github.com/gjwang/ffmpeg_mp4_mux/tree/master/avc_raw
This generates a MP4 video that I can play using Chrome and VLC, but Firefox and some tools (like Windows Media Player) say the video is corrupted.
Here is a link to the generated video: https://ufile.io/5b3241
Thanks for your help.
-
How to install latest ffmpeg on mac
2 décembre 2016, par eco_bachI am using this command
sudo port install ffmpeg +gpl +postproc +lame +theora +libogg +vorbis +xvid +x264 +a52 +faac +faad +dts +nonfree
But the installed version of ffmpeg I get is only 0.7.13.
I am using MacPorts which may be the issue
Apparently there is a 1.0 release! http://ffmpeg.org/download.html#release_1.0
-
How to extract duration time from ffmpeg output ?
2 décembre 2016, par LouiseTo get a lot of information about a media file one can do
ffmpeg -i
where it will output a lot of lines, one in particular
Duration: 00:08:07.98, start: 0.000000, bitrate: 2080 kb/s
I would like to output only
00:08:07.98
, so I tryffmpeg -i file.mp4 | grep Duration| sed 's/Duration: \(.*\), start/\1/g'
But it prints everything, and not just the length.
Even
ffmpeg -i file.mp4 | grep Duration
outputs everything.How do I get just the duration length?
-
ffpyplayer with libx264 error : cannot locate symbol "avio_seek" referenced by "player.so"
2 décembre 2016, par Gerasimov MikhailKivy's ffpyplayer 3.2 can be built with python-for-android's old toolchain recipe. I also want ffpyplayer to open more video formats so I try to add libx264 for ffmpeg2 recipe.
I created separate recipe for libx264, here's how I build lib:
try ./configure \ --cross-prefix=arm-linux-androideabi- \ --sysroot="$NDKPLATFORM" \ --host=arm-linux \ --enable-pic \ --disable-asm \ --disable-cli \ --disable-opencl \ --enable-shared \ --prefix=$BUILD_x264
Here's main part where I add libx264 to ffmpeg2:
debug "Activate flags for x264 / ffmpeg2" x264_CFLAGS="-I$BUILD_x264/include/" x264_LDFLAGS="-L$BUILD_x264/lib/ -Wl,-soname,libx264.so"
Everything compiles fine, but I'm getting error when trying to use ffpyplayer on Android device:
ffpyplayer - ImportError: dlopen failed: cannot locate symbol "avio_seek" referenced by "player.so"
Any idea what can it be?
I have no knowledges in compiling libs so I would be grateful if you suggest me way where to go to find reason of this error.
-
How to compile latest FFmpeg Library for Android ?
2 décembre 2016, par Timo SchuckStruggling to put together a working and generally best practice build of current FFmpeg. There seems to be no up-to-date documents or tutorials. What does exist is full of outdated links or build-scripts that do not work.