
Recherche avancée
Médias (1)
-
Publier une image simplement
13 avril 2011, par ,
Mis à jour : Février 2012
Langue : français
Type : Video
Autres articles (99)
-
Personnaliser en ajoutant son logo, sa bannière ou son image de fond
5 septembre 2013, parCertains thèmes prennent en compte trois éléments de personnalisation : l’ajout d’un logo ; l’ajout d’une bannière l’ajout d’une image de fond ;
-
Ecrire une actualité
21 juin 2013, parPrésentez les changements dans votre MédiaSPIP ou les actualités de vos projets sur votre MédiaSPIP grâce à la rubrique actualités.
Dans le thème par défaut spipeo de MédiaSPIP, les actualités sont affichées en bas de la page principale sous les éditoriaux.
Vous pouvez personnaliser le formulaire de création d’une actualité.
Formulaire de création d’une actualité Dans le cas d’un document de type actualité, les champs proposés par défaut sont : Date de publication ( personnaliser la date de publication ) (...) -
Publier sur MédiaSpip
13 juin 2013Puis-je poster des contenus à partir d’une tablette Ipad ?
Oui, si votre Médiaspip installé est à la version 0.2 ou supérieure. Contacter au besoin l’administrateur de votre MédiaSpip pour le savoir
Sur d’autres sites (9775)
-
Muxing H264 packets into a MPEGTS container using libav*
30 avril 2024, par LucenI'm writing a C++ program where I need to encode packets in h264 format and mux them to a MPEG TS container. For the encoding part, I based my code on the encode_video example (https://ffmpeg.org/doxygen/trunk/encode_video_8c-example.html#a9) provided in FFMPEG documentation, and it seems to work fine. In particular, I generate a std::vector of packets which I sequentially write to an output .ts file for debug. Such .ts file plays fine with SMPlayer, and a ffproba command gives


>> ffprobe -print_format json -show_format -show_streams out.ts
Input #0, h264, from 'out.ts':
 Duration: N/A, bitrate: N/A
 Stream #0:0: Video: h264 (Main), yuv420p(progressive), 640x480 [SAR 1:1 DAR 4:3], 25 fps, 25 tbr, 1200k tbn, 50 tbc
 "streams": [
 {
 "index": 0,
 "codec_name": "h264",
 "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10",
 "profile": "Main",
 "codec_type": "video",
 "codec_time_base": "1/50",
 "codec_tag_string": "[0][0][0][0]",
 "codec_tag": "0x0000",
 "width": 640,
 "height": 480,
 "coded_width": 640,
 "coded_height": 480,
 "has_b_frames": 1,
 "sample_aspect_ratio": "1:1",
 "display_aspect_ratio": "4:3",
 "pix_fmt": "yuv420p",
 "level": 30,
 "chroma_location": "left",
 "field_order": "progressive",
 "refs": 1,
 "is_avc": "false",
 "nal_length_size": "0",
 "r_frame_rate": "25/1",
 "avg_frame_rate": "25/1",
 "time_base": "1/1200000",
 "bits_per_raw_sample": "8",
 "disposition": {
 "default": 0,
 "dub": 0,
 "original": 0,
 "comment": 0,
 "lyrics": 0,
 "karaoke": 0,
 "forced": 0,
 "hearing_impaired": 0,
 "visual_impaired": 0,
 "clean_effects": 0,
 "attached_pic": 0,
 "timed_thumbnails": 0
 }
 }
 ],
 "format": {
 "filename": "out.ts",
 "nb_streams": 1,
 "nb_programs": 0,
 "format_name": "h264",
 "format_long_name": "raw H.264 video",
 "size": "435443",
 "probe_score": 51
 }
}




The dts and pts timestamps are also set.
However, if I try to mux them in MPEG TS format, using as a base the example mux.c (https://ffmpeg.org/doxygen/trunk/mux_8c-example.html), it doesn't work. A shortened version of my muxing code is as follows : (the variables ending with "_" are class fields)


int MyProcessing::Mux(const std::string outputFilename) {
 AVFormatContext *muxingContest;
 avformat_alloc_output_context2(&muxingContest, NULL, NULL, m_output.c_str());

 auto outFormat = muxingContest->oformat;
 outFormat->video_codec = AV_CODEC_ID_H264;

 AVStream *outStream;
 const AVCodec *codec;

 Mux_AddStream(&outStream, muxingContest, &codec, outFormat->video_codec);

 AVDictionary *opt = nullptr;
 Mux_OpenVideo(muxingContest, codec, outStream, opt);
 
 if (!(muxingContest->flags & AVFMT_NOFILE)) {
 avio_open(&muxingContest->pb, m_output.c_str(), AVIO_FLAG_WRITE);
 }
 avformat_write_header(muxingContest, &opt);

 auto muxOk = true;
 size_t countMuxedFrames = 0;
 while ((muxOk) && (countMuxedFrames < packets_.size())) {
 muxOk = !MuxPacket(muxingContest, outStream, packets_[countMuxedFrames], &opt);
 countMuxedFrames++;
 }

 av_write_trailer(muxingContest);
 if (!(muxCodecContextPtr_->flags & AVFMT_NOFILE)) avio_closep(&muxingContest->pb);
 
 return 0;
}


int MyProcessing::Mux_AddStream(AVStream **stream, AVFormatContext *format, const AVCodec **codec, enum AVCodecID codecId) {
 *codec = avcodec_find_encoder(codecId);
 muxPacketTmpPtr_ = av_packet_alloc();
 *stream = avformat_new_stream(format, *codec);
 (*stream)->time_base = (AVRational){ 1, STREAM_FRAME_RATE };
 (*stream)->id = format->nb_streams-1;
 (*stream)->index = 0;
 muxCodecContextPtr_ = avcodec_alloc_context3(*codec);
 Mux_FillCodecContext(*muxCodecContextPtr_, codecId, **stream);
 if (format->oformat->flags & AVFMT_GLOBALHEADER)
 muxCodecContextPtr_->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
 return 0;
}


void MyProcessing::Mux_FillCodecContext(AVCodecContext &cc, enum AVCodecID codecId, AVStream &stream) {
 cc.codec_id = codecId;
 cc.bit_rate = 400000;
 cc.width = outputWidth_;
 cc.height = outputHeight_;
 cc.time_base = stream.time_base;
 cc.gop_size = 10;
 cc.max_b_frames = 1;
 cc.gop_size = 12;
 cc.pix_fmt = AV_PIX_FMT_YUV420P;
 if (cc.codec_id == AV_CODEC_ID_MPEG2VIDEO) cc.max_b_frames = 2;
 if (cc.codec_id == AV_CODEC_ID_MPEG1VIDEO) cc.mb_decision = 2;
 av_opt_set(&cc, "preset", "slow", 0);
 av_opt_set(&cc, "tune", "zerolatency", 0);
}


int MyProcessing::Mux_OpenVideo(AVFormatContext *format, const AVCodec *codec, AVStream *stream, AVDictionary *opt_arg) {
 AVDictionary *opt = nullptr;
 av_dict_copy(&opt, opt_arg, 0);
 muxCodecContextPtr_->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
 avcodec_open2(muxCodecContextPtr_, codec, &opt);
 av_dict_free(&opt);
 avcodec_parameters_from_context(stream->codecpar, muxCodecContextPtr_);
 return 0;
}

int MyProcessing::MuxPacket(AVFormatContext *format, AVStream *stream, AVPacket &pkt, AVDictionary **opt) {
 AVBitStreamFilterContext *bsf = av_bitstream_filter_init("h264_mp4toannexb");
 AVPacket filteredPkt = pkt;
 auto filterResult = av_bitstream_filter_filter(bsf, format->streams[stream->index]->codec, NULL,
 &filteredPkt.data, &filteredPkt.size,
 pkt.data, pkt.size,
 pkt.flags & AV_PKT_FLAG_KEY);

 if (filterResult < 0) return filterResult;
 else {
 filteredPkt.buf = av_buffer_create(filteredPkt.data, filteredPkt.size,
 av_buffer_default_free, NULL, 0);
 }
 av_bitstream_filter_close(bsf);
 filteredPkt.stream_index = stream->index;
 filteredPkt.dts = filteredPkt.pts;
 filteredPkt.duration = ((double)stream->time_base.num / (double)stream->time_base.den) / STREAM_FRAME_RATE;
 av_packet_rescale_ts(&filteredPkt, muxCodecContextPtr_->time_base, stream->time_base); // rescale output packet timestamp values from codec to stream timebase
 auto writePktResult = av_write_frame(format, &filteredPkt);
 // auto writePktResult = av_interleaved_write_frame(format, &filteredPkt);
 return 0;
}




The console error is


[mpegts @ 0x55555736edc0] H.264 bitstream malformed, no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it ('-bsf:v h264_mp4toannexb' option with ffmpeg)



It Is telling me to apply the h264_mp4toannexb filter. As you see from the code, I've put the filtering accordingly, but the error message persists (unless I'm applying the filter in a wrong way).


In the last lines of method MuxPacket(), if I uncomment the line with av_interleaved_write_frame() and comment the previous one, I get the same error, as well as a seg fault. Inspecting with GDB, the call stack for the seg fault is as follows :


#0 __memmove_avx_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:440
#1 0x00007ffff67c7cb6 in av_packet_copy_props () at /lib/x86_64-linux-gnu/libavcodec.so.58
#2 0x00007ffff67c8447 in av_packet_ref () at /lib/x86_64-linux-gnu/libavcodec.so.58
#3 0x00007ffff7e2fa13 in () at /lib/x86_64-linux-gnu/libavformat.so.58
#4 0x00007ffff7e2fb11 in () at /lib/x86_64-linux-gnu/libavformat.so.58
#5 0x00007ffff7e30575 in av_interleaved_write_frame () at /lib/x86_64-linux-gnu/libavformat.so.58



I tried to look at solutions online, but they are either old or they don't work. Some of the things I tried and didn't work :


- 

- Putting the line




muxCodecContextPtr_->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;



in Mux() after the call to avformat_alloc_output_context2.


- 

- Setting




packet.flags |= AV_PKT_FLAG_KEY;



before the call to av_write_frame / av_interleaved_write_frame.


- 

-
Trying to write by hand to the file the starting code as described here Need to convert h264 stream from annex-b format to AVCC format.


-
Playing with parameters in Mux_FillCodecContext().








-
Crash on ffmpeg avcodec_encode_video in a Console app
5 mars 2013, par Robel sharmaI want make an encoder which encode a raw image into h263 format.But after loading and initializing ffmpeg library I got crash on avcodec_encode_video for a demo image.
int _tmain(int argc, _TCHAR* argv[]) {
avcodec_register_all();
AVCodec *codec;
AVCodecContext *c= NULL;
int i, ret, x, y, got_output;
FILE *f;
AVFrame *frame;
AVPacket pkt;
int out_size, size, outbuf_size;
AVFrame *picture;
uint8_t *outbuf, *picture_buf;
AVRational rp;
rp.den = 1;
rp.num = 25;
uint8_t endcode[] = { 0, 0, 1, 0xb7 };
codec = avcodec_find_encoder(CODEC_ID_H263);
c = avcodec_alloc_context3(codec);
picture= avcodec_alloc_frame();
c->bit_rate = 400000;
/* resolution must be a multiple of two */
c->width = 352;
c->height = 288;
/* frames per second */
//c->time_base= (AVRational){1,25};
c->time_base = rp;
c->gop_size = 10; /* emit one intra frame every ten frames */
c->max_b_frames=1;
c->pix_fmt = PIX_FMT_YUV420P;
avcodec_open(c, codec);
outbuf_size = 100000;
outbuf = (uint8_t*)malloc(outbuf_size);
size = c->width * c->height;
picture_buf = (uint8_t*)malloc((size * 3) / 2); /* size for YUV 420 */
picture->data[0] = picture_buf;
picture->data[1] = picture->data[0] + size;
picture->data[2] = picture->data[1] + size / 4;
picture->linesize[0] = c->width;
picture->linesize[1] = c->width / 2;
picture->linesize[2] = c->width / 2;
/* encode 1 second of video */
for(i=0;i<25;i++) {
fflush(stdout);
/* prepare a dummy image */
/* Y */
for(y=0;yheight;y++) {
for(x=0;xwidth;x++) {
picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
}
}
/* Cb and Cr */
for(y=0;yheight/2;y++) {
for(x=0;xwidth/2;x++) {
picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
}
}
/* encode the image */
**Crash is here** ---> ///////////////////////////////////////////////////
out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
printf("encoding frame %3d (size=%5d)\n", i, out_size);
fwrite(outbuf, 1, out_size, f);
}
/* get the delayed frames */
for(; out_size; i++) {
fflush(stdout);
out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
printf("write frame %3d (size=%5d)\n", i, out_size);
fwrite(outbuf, 1, out_size, f);
}
/* add sequence end code to have a real mpeg file */
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0x01;
outbuf[3] = 0xb7;
fwrite(outbuf, 1, 4, f);
fclose(f);
free(picture_buf);
free(outbuf);
avcodec_close(c);
av_free(c);
av_free(picture);
printf("\n");
return 0;
} -
FFMPEG Segmenting skips when m3u8 changes file
25 septembre 2012, par user792164I am attempting to segment a large video file in to segments. When streamed (even locally) by opening the m3u8 file it will jump forward in time by some period of time less than 1 second.
The following commands are executed :
First mp4 —> ts :
ffmpeg -i input_file.mp4 -bsf:v h264_mp4toannexb -acodec libfaac -vcodec libx264 -f mpegts -threads 0 output.ts
Then I split using :
ffmpeg -i output.ts -vcodec copy -acodec copy -map 0 -f segment -segment_time 30 -segment_list output.m3u8 -segment_list_type m3u8 -segment_format mpegts output%03d.ts
Note : Changing segment time has no effect on issue.
Generated manifest :
#EXTM3U
#EXT-X-VERSION:4
#EXTINF:30.947578,
output000.ts
#EXTINF:30.155111,
output001.ts
...
#EXTINF:24.023989,
output082.ts
#EXT-X-TARGETDURATION:37
#EXT-X-ENDLIST
Meta Data :
$> ffmpeg -version
ffmpeg version git-2012-08-19-a93c221
built on Aug 19 2012 13:18:58 with gcc 4.4.5 (Debian 4.4.5-8)
configuration: --prefix=/usr --bindir=/usr/bin --datadir=/usr/share/ffmpeg --incdir=/usr/include/ffmpeg --libdir=/usr/lib/ --mandir=/usr/share/man --extra-cflags='-O3 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions \ -fstack-protector --param=ssp-buffer-size=4 -mtune=generic' --enable-gpl --enable-shared --enable-nonfree --enable-version3 --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-librtmp --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-pthreads --enable-libxvid --enable-postproc --enable-libgsm --enable-libspeex --enable-avfilter --disable-decoder=libdirac --enable-libfreetype --enable-libschroedinger --disable-decoder=libschroedinger --enable-libopenjpeg --disable-ffplay --disable-ffserver
libavutil 51. 70.100 / 51. 70.100
libavcodec 54. 53.100 / 54. 53.100
libavformat 54. 25.104 / 54. 25.104
libavdevice 54. 2.100 / 54. 2.100
libavfilter 3. 11.101 / 3. 11.101
libswscale 2. 1.101 / 2. 1.101
libswresample 0. 15.100 / 0. 15.100
libpostproc 52. 0.100 / 52. 0.100-
$> ffprobe input_file.mp4
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input_file.mp4':
Metadata:
major_brand : isom
minor_version : 1
compatible_brands: isom
creation_time : 2011-09-08 11:43:25
Duration: 00:41:31.00, start: 0.000000, bitrate: 1146 kb/s
Stream #0.0(und): Video: h264 (High), yuv420p, 720x404 [PAR 1:1 DAR 180:101], 1015 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc
Metadata:
creation_time : 2011-09-08 11:43:25
Stream #0.1(und): Audio: aac, 48000 Hz, stereo, s16, 124 kb/s
Metadata:
creation_time : 2011-09-08 11:43:25-
$> ffprobe output_file.ts
Input #0, mpegts, from 'output_file.ts':
Duration: 00:41:30.98, start: 1.400000, bitrate: 807 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0.0[0x100]: Video: h264 (High), yuv420p, 720x404 [PAR 1:1 DAR 180:101], 23.98 fps, 23.98 tbr, 90k tbn, 47.95 tbc
Stream #0.1[0x101](und): Audio: aac, 48000 Hz, stereo, s16, 141 kb/
Is it possible to remove this jump, if so, what encoding parameters or incorrect assumptions have I made ? Thanks.