
Recherche avancée
Autres articles (74)
-
La file d’attente de SPIPmotion
28 novembre 2010, parUne file d’attente stockée dans la base de donnée
Lors de son installation, SPIPmotion crée une nouvelle table dans la base de donnée intitulée spip_spipmotion_attentes.
Cette nouvelle table est constituée des champs suivants : id_spipmotion_attente, l’identifiant numérique unique de la tâche à traiter ; id_document, l’identifiant numérique du document original à encoder ; id_objet l’identifiant unique de l’objet auquel le document encodé devra être attaché automatiquement ; objet, le type d’objet auquel (...) -
Support de tous types de médias
10 avril 2011Contrairement à beaucoup de logiciels et autres plate-formes modernes de partage de documents, MediaSPIP a l’ambition de gérer un maximum de formats de documents différents qu’ils soient de type : images (png, gif, jpg, bmp et autres...) ; audio (MP3, Ogg, Wav et autres...) ; vidéo (Avi, MP4, Ogv, mpg, mov, wmv et autres...) ; contenu textuel, code ou autres (open office, microsoft office (tableur, présentation), web (html, css), LaTeX, Google Earth) (...)
-
Supporting all media types
13 avril 2011, parUnlike most software and media-sharing platforms, MediaSPIP aims to manage as many different media types as possible. The following are just a few examples from an ever-expanding list of supported formats : images : png, gif, jpg, bmp and more audio : MP3, Ogg, Wav and more video : AVI, MP4, OGV, mpg, mov, wmv and more text, code and other data : OpenOffice, Microsoft Office (Word, PowerPoint, Excel), web (html, CSS), LaTeX, Google Earth and (...)
Sur d’autres sites (10407)
-
x264 encoded frames into a mp4 container with ffmpeg API
10 septembre 2019, par PJDI'm struggling with understanding what is and what is not needed in getting my already encoded x264 frames into a video container file using ffmpeg's libavformat API.



My current program will get the x264 frames like this -



while( x264_encoder_delayed_frames( h ) )
{
 printf("Writing delayed frame %u\n", delayed_frame_counter++);
 i_frame_size = x264_encoder_encode( h, &nal, &i_nal, NULL, &pic_out );
 if( i_frame_size < 0 ) {
 printf("Failed to encode a delayed x264 frame.\n");
 return ERROR;
 }
 else if( i_frame_size )
 {
 if( !fwrite(nal->p_payload, i_frame_size, 1, video_file_ptr) ) {
 printf("Failed to write a delayed x264 frame.\n");
 return ERROR;
 }
 }
}




If I use the CLI to the ffmpeg binary, I can put these frames into a container using :



ffmpeg -i "raw_frames.h264" -c:v copy -f mp4 "video.mp4"




I would like to code this function into my program using the libavformat API though. I'm a little stuck in the concepts and the order on which each ffmpeg function is needed to be called.



So far I have written :



mAVOutputFormat = av_guess_format("gen_vid.mp4", NULL, NULL);
 printf("Guessed format\n");

 int ret = avformat_alloc_output_context2(&mAVFormatContext, NULL, NULL, "gen_vid.mp4");
 printf("Created context = %d\n", ret);
 printf("Format = %s\n", mAVFormatContext->oformat->name);

 mAVStream = avformat_new_stream(mAVFormatContext, 0);
 if (!mAVStream) {
 printf("Failed allocating output stream\n");
 } else {
 printf("Allocated stream.\n");
 }

 mAVCodecParameters = mAVStream->codecpar;
 if (mAVCodecParameters->codec_type != AVMEDIA_TYPE_AUDIO &&
 mAVCodecParameters->codec_type != AVMEDIA_TYPE_VIDEO &&
 mAVCodecParameters->codec_type != AVMEDIA_TYPE_SUBTITLE) {
 printf("Invalid codec?\n");
 }

 if (!(mAVFormatContext->oformat->flags & AVFMT_NOFILE)) {
 ret = avio_open(&mAVFormatContext->pb, "gen_vid.mp4", AVIO_FLAG_WRITE);
 if (ret < 0) {
 printf("Could not open output file '%s'", "gen_vid.mp4");
 }
 }

 ret = avformat_write_header(mAVFormatContext, NULL);
 if (ret < 0) {
 printf("Error occurred when opening output file\n");
 }




This will print out :



Guessed format
Created context = 0
Format = mp4
Allocated stream.
Invalid codec?
[mp4 @ 0x55ffcea2a2c0] Could not find tag for codec none in stream #0, codec not currently supported in container
Error occurred when opening output file




How can I make sure the codec type is set correctly for my video ?
Next I need to somehow point my mAVStream to use my x264 frames - advice would be great.



Update 1 :
So I've tried to set the H264 codec, so the codec's meta-data is available. I seem to hit 2 newer issues now.
1) It cannot find the device and therefore cannot configure the encoder.
2) I get the "dimensions not set".



mAVOutputFormat = av_guess_format("gen_vid.mp4", NULL, NULL);
printf("Guessed format\n");

// MUST allocate the media file format context. 
int ret = avformat_alloc_output_context2(&mAVFormatContext, NULL, NULL, "gen_vid.mp4");
printf("Created context = %d\n", ret);
printf("Format = %s\n", mAVFormatContext->oformat->name);

// Even though we already have encoded the H264 frames using x264,
// we still need the codec's meta-data.
const AVCodec *mAVCodec;
mAVCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
if (!mAVCodec) {
 fprintf(stderr, "Codec '%s' not found\n", "H264");
 exit(1);
}
mAVCodecContext = avcodec_alloc_context3(mAVCodec);
if (!mAVCodecContext) {
 fprintf(stderr, "Could not allocate video codec context\n");
 exit(1);
}
printf("Codec context allocated with defaults.\n");
/* put sample parameters */
mAVCodecContext->bit_rate = 400000;
mAVCodecContext->width = width;
mAVCodecContext->height = height;
mAVCodecContext->time_base = (AVRational){1, 30};
mAVCodecContext->framerate = (AVRational){30, 1};
mAVCodecContext->gop_size = 10;
mAVCodecContext->level = 31;
mAVCodecContext->max_b_frames = 1;
mAVCodecContext->pix_fmt = AV_PIX_FMT_NV12;

av_opt_set(mAVCodecContext->priv_data, "preset", "slow", 0);
printf("Set codec parameters.\n");

// Initialize the AVCodecContext to use the given AVCodec.
avcodec_open2(mAVCodecContext, mAVCodec, NULL); 

// Add a new stream to a media file. Must be called before
// calling avformat_write_header().
mAVStream = avformat_new_stream(mAVFormatContext, mAVCodec);
if (!mAVStream) {
 printf("Failed allocating output stream\n");
} else {
 printf("Allocated stream.\n");
}

// TODO How should codecpar be set?
mAVCodecParameters = mAVStream->codecpar;
if (mAVCodecParameters->codec_type != AVMEDIA_TYPE_AUDIO &&
 mAVCodecParameters->codec_type != AVMEDIA_TYPE_VIDEO &&
 mAVCodecParameters->codec_type != AVMEDIA_TYPE_SUBTITLE) {
 printf("Invalid codec?\n");
}

if (!(mAVFormatContext->oformat->flags & AVFMT_NOFILE)) {
 ret = avio_open(&mAVFormatContext->pb, "gen_vid.mp4", AVIO_FLAG_WRITE);
 if (ret < 0) {
 printf("Could not open output file '%s'", "gen_vid.mp4");
 }
 }
printf("Called avio_open()\n");

// MUST write a header.
ret = avformat_write_header(mAVFormatContext, NULL);
if (ret < 0) {
 printf("Error occurred when opening output file (writing header).\n");
}




Now I am getting this output -



Guessed format
Created context = 0
Format = mp4
Codec context allocated with defaults.
Set codec parameters.
[h264_v4l2m2m @ 0x556460344b40] Could not find a valid device
[h264_v4l2m2m @ 0x556460344b40] can't configure encoder
Allocated stream.
Invalid codec?
Called avio_open()
[mp4 @ 0x5564603442c0] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
[mp4 @ 0x5564603442c0] dimensions not set
Error occurred when opening output file (writing header).



-
How to add subtitles to the encoded video using ffmpeg ?
14 novembre 2019, par geo-freakI am trying to add my srt file to the video using the options which were answered here before. My input file has captions in it by default. I tried in different ways to get the captions enabled in my encoded video. Below are the commands I used.
ffmpeg -i input.ts -i captions.srt -b:a 32000 -ar 48000 -force_key_frames 'expr:gte(t,n_forced*3)' -acodec libfaac -hls_flags single_file -hls_list_size 0 -hls_time 3 -vcodec libx264 -s 320x240 -b:v 512000 -maxrate 512000 -c:s mov_text outfile.ts
But I couldn’t see the captions after I see the mediainfo of the encoded file. You can see the log file of my command.
[mpegts @ 0x56412e67b0c0] max_analyze_duration 5000000 reached at 5024000 microseconds st:1
input.ts FPS 29.970030 1
Input #0, mpegts, from 'input.ts':
Duration: 00:03:00.07, start: 1.400000, bitrate: 2172 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: mpeg2video (Main), 1 reference frame ([2][0][0][0] / 0x0002), yuv420p(tv), 704x480 [SAR 10:11 DAR 4:3], Closed Captions, max. 15000 kb/s,
29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:1[0x101](eng): Audio: ac3 ([129][0][0][0] / 0x0081), 48000 Hz, stereo, fltp, 192 kb/s
Input #1, srt, from 'captions.srt':
Duration: N/A, bitrate: N/A
Stream #1:0: Subtitle: subrip
[graph 0 input from stream 0:0 @ 0x56412e678540] w:704 h:480 pixfmt:yuv420p tb:1/90000 fr:30000/1001 sar:10/11 sws_param:flags=2
[scaler for output stream 0:0 @ 0x56412e9caac0] w:320 h:240 flags:'bicubic' interl:0
[scaler for output stream 0:0 @ 0x56412e9caac0] w:704 h:480 fmt:yuv420p sar:10/11 -> w:320 h:240 fmt:yuv420p sar:1/1 flags:0x4
[graph 1 input from stream 0:1 @ 0x56412e9f74e0] tb:1/48000 samplefmt:fltp samplerate:48000 chlayout:0x3
[audio format for output stream 0:1 @ 0x56412e9f7b40] auto-inserting filter 'auto-inserted resampler 0' between the filter 'Parsed_anull_0' and the filter 'audio format
for output stream 0:1'
[auto-inserted resampler 0 @ 0x56412e9f9f20] ch:2 chl:stereo fmt:fltp r:48000Hz -> ch:2 chl:stereo fmt:s16 r:48000Hz
[libx264 @ 0x56412e9bb8c0] VBV maxrate specified, but no bufsize, ignored
[libx264 @ 0x56412e9bb8c0] using SAR=1/1
[libx264 @ 0x56412e9bb8c0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2
[libx264 @ 0x56412e9bb8c0] profile High, level 2.0
[mpegts @ 0x56412e9ba5a0] muxrate VBR, pcr every 2 pkts, sdt every 200, pat/pmt every 40 pkts
Output #0, mpegts, to 'my_encoded_all-3.ts':
Metadata:
encoder : Lavf57.25.100
Stream #0:0: Video: h264 (libx264), -1 reference frame, yuv420p, 320x240 [SAR 1:1 DAR 4:3], q=-1--1, 512 kb/s, 29.97 fps, 90k tbn, 29.97 tbc
Metadata:
encoder : Lavc57.24.102 libx264
Side data:
unknown side data type 10 (24 bytes)
Stream #0:1(eng): Audio: aac (libfaac), 48000 Hz, stereo, s16, 32 kb/s
Metadata:
encoder : Lavc57.24.102 libfaac
Stream #0:2(eng): Subtitle: subrip (srt), 320x240
Metadata:
encoder : Lavc57.24.102 srt
Stream mapping:
Stream #0:0 -> #0:0 (mpeg2video (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (ac3 (native) -> aac (libfaac))
Stream #1:0 -> #0:2 (subrip (srt) -> subrip (srt))
Press [q] to stop, [?] for help
[scaler for output stream 0:0 @ 0x56412e9caac0] w:704 h:480 fmt:yuv420p sar:40/33 -> w:320 h:240 fmt:yuv420p sar:4/3 flags:0x4
No more output streams to write to, finishing.e=00:02:58.85 bitrate= 658.4kbits/s speed=13.2x
frame= 5377 fps=385 q=-1.0 Lsize= 16672kB time=00:59:16.18 bitrate= 38.4kbits/s speed= 255x
video:11401kB audio:1410kB subtitle:446kB other streams:0kB global headers:0kB muxing overhead: 25.753614%
Input file #0 (input.ts):
Input stream #0:0 (video): 5380 packets read (40279443 bytes); 5377 frames decoded;
Input stream #0:1 (audio): 5625 packets read (4320000 bytes); 5625 frames decoded (8640000 samples);
Total: 11005 packets (44599443 bytes) demuxed
Input file #1 (captions.srt):
Input stream #1:0 (subtitle): 10972 packets read (447147 bytes); 10972 frames decoded;
Total: 10972 packets (447147 bytes) demuxed
Output file #0 (output.ts):
Output stream #0:0 (video): 5377 frames encoded; 5377 packets muxed (11675098 bytes);
Output stream #0:1 (audio): 8438 frames encoded (8640000 samples); 8439 packets muxed (1444109 bytes);
Output stream #0:2 (subtitle): 10972 frames encoded; 10972 packets muxed (456619 bytes);
Total: 24788 packets (13575826 bytes) muxed
[libx264 @ 0x56412e9bb8c0] frame I:81 Avg QP:15.08 size: 16370
[libx264 @ 0x56412e9bb8c0] frame P:2312 Avg QP:17.77 size: 3393
[libx264 @ 0x56412e9bb8c0] frame B:2984 Avg QP:22.38 size: 839
[libx264 @ 0x56412e9bb8c0] consecutive B-frames: 20.6% 13.2% 9.4% 56.8%
[libx264 @ 0x56412e9bb8c0] mb I I16..4: 11.6% 37.0% 51.4%
[libx264 @ 0x56412e9bb8c0] mb P I16..4: 1.2% 3.2% 2.4% P16..4: 33.7% 18.0% 15.7% 0.0% 0.0% skip:25.6%
[libx264 @ 0x56412e9bb8c0] mb B I16..4: 0.2% 0.3% 0.2% B16..8: 30.7% 9.2% 3.2% direct: 5.1% skip:51.1% L0:33.6% L1:44.5% BI:21.9%
[libx264 @ 0x56412e9bb8c0] final ratefactor: 16.76
[libx264 @ 0x56412e9bb8c0] 8x8 transform intra:43.0% inter:49.8%
[libx264 @ 0x56412e9bb8c0] coded y,uvDC,uvAC intra: 77.9% 85.1% 68.8% inter: 22.9% 21.7% 6.0%
[libx264 @ 0x56412e9bb8c0] i16 v,h,dc,p: 30% 38% 5% 26%
[libx264 @ 0x56412e9bb8c0] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 21% 23% 18% 4% 5% 7% 6% 7% 8%
[libx264 @ 0x56412e9bb8c0] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 23% 11% 5% 6% 8% 6% 7% 6%
[libx264 @ 0x56412e9bb8c0] i8c dc,h,v,p: 46% 24% 21% 8%
[libx264 @ 0x56412e9bb8c0] Weighted P-Frames: Y:4.2% UV:2.6%
[libx264 @ 0x56412e9bb8c0] ref P L0: 73.3% 10.9% 11.1% 4.5% 0.1%
[libx264 @ 0x56412e9bb8c0] ref B L0: 92.1% 6.3% 1.6%
[libx264 @ 0x56412e9bb8c0] ref B L1: 97.4% 2.6%I found no issues in encoding but I couldn’t see the captions enabled in my output encoded video. I played it in my VLC player. No tracks for subtitles.
Can’t we add the subtitles to a video while encoding ?
Any help in achieving this would be appreciated.
-
avcodec_find_encoder_by_name() returns NULL
27 décembre 2019, par GiuTorI’m trying to compile the following example I found on ffmpeg documentation :
/*
* Copyright (c) 2001 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// compile with gcc -o encode_video encode_video.c -lavutil -lavcodec -lz -lm
/**
* @file
* video encoding with libavcodec API example
*
* @example encode_video.c
*/
#include
#include
#include
#include <libavcodec></libavcodec>avcodec.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>imgutils.h>
static void encode(AVCodecContext *enc_ctx, AVFrame *frame, AVPacket *pkt,
FILE *outfile)
{
int ret;
/* send the frame to the encoder */
if (frame)
printf("Send frame %3"PRId64"\n", frame->pts);
ret = avcodec_send_frame(enc_ctx, frame);
if (ret < 0) {
fprintf(stderr, "Error sending a frame for encoding\n");
exit(1);
}
while (ret >= 0) {
ret = avcodec_receive_packet(enc_ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error during encoding\n");
exit(1);
}
printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
fwrite(pkt->data, 1, pkt->size, outfile);
av_packet_unref(pkt);
}
}
int main(int argc, char **argv)
{
const char *filename, *codec_name;
const AVCodec *codec;
AVCodecContext *c= NULL;
int i, ret, x, y;
FILE *f;
AVFrame *frame;
AVPacket *pkt;
uint8_t endcode[] = { 0, 0, 1, 0xb7 };
if (argc <= 2) {
fprintf(stderr, "Usage: %s <output file="file"> <codec>\n", argv[0]);
exit(0);
}
filename = argv[1];
codec_name = argv[2];
/* find the mpeg1video encoder */
codec = avcodec_find_encoder_by_name(codec_name);
if (!codec) {
fprintf(stderr, "Codec '%s' not found\n", codec_name);
exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate video codec context\n");
exit(1);
}
pkt = av_packet_alloc();
if (!pkt)
exit(1);
/* put sample parameters */
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->framerate = (AVRational){25, 1};
/* emit one intra frame every ten frames
* check frame pict_type before passing frame
* to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
* then gop_size is ignored and the output of encoder
* will always be I frame irrespective to gop_size
*/
c->gop_size = 10;
c->max_b_frames = 1;
c->pix_fmt = AV_PIX_FMT_YUV420P;
if (codec->id == AV_CODEC_ID_H264)
av_opt_set(c->priv_data, "preset", "slow", 0);
/* open it */
ret = avcodec_open2(c, codec, NULL);
if (ret < 0) {
fprintf(stderr, "Could not open codec: %s\n", av_err2str(ret));
exit(1);
}
f = fopen(filename, "wb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate video frame\n");
exit(1);
}
frame->format = c->pix_fmt;
frame->width = c->width;
frame->height = c->height;
ret = av_frame_get_buffer(frame, 32);
if (ret < 0) {
fprintf(stderr, "Could not allocate the video frame data\n");
exit(1);
}
/* encode 1 second of video */
for (i = 0; i < 25; i++) {
fflush(stdout);
/* make sure the frame data is writable */
ret = av_frame_make_writable(frame);
if (ret < 0)
exit(1);
/* prepare a dummy image */
/* Y */
for (y = 0; y < c->height; y++) {
for (x = 0; x < c->width; x++) {
frame->data[0][y * frame->linesize[0] + x] = x + y + i * 3;
}
}
/* Cb and Cr */
for (y = 0; y < c->height/2; y++) {
for (x = 0; x < c->width/2; x++) {
frame->data[1][y * frame->linesize[1] + x] = 128 + y + i * 2;
frame->data[2][y * frame->linesize[2] + x] = 64 + x + i * 5;
}
}
frame->pts = i;
/* encode the image */
encode(c, frame, pkt, f);
}
/* flush the encoder */
encode(c, NULL, pkt, f);
/* add sequence end code to have a real MPEG file */
if (codec->id == AV_CODEC_ID_MPEG1VIDEO || codec->id == AV_CODEC_ID_MPEG2VIDEO)
fwrite(endcode, 1, sizeof(endcode), f);
fclose(f);
avcodec_free_context(&c);
av_frame_free(&frame);
av_packet_free(&pkt);
return 0;
}
</codec></output>ffmpeg -codecs return a bunch of codecs among which libx264 and libx265. When I run the above code I get NULL from avcodec_find_encoder_by_name() :
gt@gt-Aspire-E1-570 : /libav$ ./encode_video pippo libx264
Codec ’libx264’ not found
gt@gt-Aspire-E1-570 : /libav$ ./encode_video pippo x264
Codec ’x264’ not found
gt@gt-Aspire-E1-570 : /libav$ ./encode_video pippo H264
Codec ’H264’ not found
gt@gt-Aspire-E1-570 : /libav$Can someone help please ?
Thanks