
Recherche avancée
Médias (1)
-
La conservation du net art au musée. Les stratégies à l’œuvre
26 mai 2011
Mis à jour : Juillet 2013
Langue : français
Type : Texte
Autres articles (91)
-
Configurer la prise en compte des langues
15 novembre 2010, parAccéder à la configuration et ajouter des langues prises en compte
Afin de configurer la prise en compte de nouvelles langues, il est nécessaire de se rendre dans la partie "Administrer" du site.
De là, dans le menu de navigation, vous pouvez accéder à une partie "Gestion des langues" permettant d’activer la prise en compte de nouvelles langues.
Chaque nouvelle langue ajoutée reste désactivable tant qu’aucun objet n’est créé dans cette langue. Dans ce cas, elle devient grisée dans la configuration et (...) -
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 -
Le profil des utilisateurs
12 avril 2011, parChaque utilisateur dispose d’une page de profil lui permettant de modifier ses informations personnelle. Dans le menu de haut de page par défaut, un élément de menu est automatiquement créé à l’initialisation de MediaSPIP, visible uniquement si le visiteur est identifié sur le site.
L’utilisateur a accès à la modification de profil depuis sa page auteur, un lien dans la navigation "Modifier votre profil" est (...)
Sur d’autres sites (10254)
-
Something wrong with recording video and merging audio and video by using FFmpeg
16 mars 2014, par VaFancyAt the beginning, I have to admit that I am a newbie in FFmpeg and also raspbian. Unfortunately, I need to solve this problem as soon as possible.
I tried to record videos on my Raspberry PI by using PIcamera and USB webcam separately. When I used command :
ffmpeg -t 10 -f video4linux2 -r 25 -i /dev/video0 out.h264
However, something was very wrong because there was nothing saved in that video file. I copy the log.
[video4linux2,v4l2 @ 0x267ad80] The driver changed the time per frame from 1/25 to 1/30
Input #0, video4linux2,v4l2, from '/dev/video0':
Duration: N/A, start: 37562.908020, bitrate: 147456 kb/s
Stream #0:0: Video: rawvideo (YUY2 / 0x32595559), yuyv422, 640x480, 147456 kb/s, 30 fps, 30 tbr, 1000k tbn, 1000k tbc
File 'out.h264' already exists. Overwrite ? [y/N] y
No pixel format specified, yuv422p for H.264 encoding chosen.
Use -pix_fmt yuv420p for compatibility with outdated media players.
[libx264 @ 0x267ca60] using cpu capabilities: ARMv6
[libx264 @ 0x267ca60] profile High 4:2:2, level 3.0, 4:2:2 8-bit
Output #0, h264, to 'out.h264':
Metadata:
encoder : Lavf55.34.101
Stream #0:0: Video: h264 (libx264), yuv422p, 640x480, q=-1--1, 90k tbn, 25 tbc
Stream mapping:
Stream #0:0 -> #0:0 (rawvideo -> libx264)
Press [q] to stop, [?] for help
DTS 140730117085, next:18648459 st:0 invalid dropping00.00 bitrate=N/A
PTS 140730117085, next:18648459 invalid dropping st:0
DTS 140740452429, next:6538221 st:0 invalid dropping:00.00 bitrate=N/A
PTS 140740452429, next:6538221 invalid dropping st:0
frame= 44 fps=1.1 q=28.0 size= 18kB time=00:00:00.04 bitrate=3712.6kbits/
frame= 45 fps=0.8 q=28.0 size= 22kB time=00:00:00.08 bitrate=2262.8kbits/
frame= 46 fps=0.7 q=28.0 size= 27kB time=00:00:00.12 bitrate=1813.4kbits/
frame= 47 fps=0.6 q=28.0 size= 30kB time=00:00:00.16 bitrate=1559.2kbits/
frame= 48 fps=0.5 q=28.0 size= 34kB time=00:00:00.20 bitrate=1397.7kbits/
frame= 49 fps=0.5 q=28.0 size= 39kB time=00:00:00.24 bitrate=1324.3kbits/
frame= 50 fps=0.4 q=28.0 size= 42kB time=00:00:00.28 bitrate=1241.7kbits/As you can see, the size of each frame was only few kb and fps was so low. Thus each frame was taken by using a long time(4s in this case). Then I test my USB webcam by using command :
ffplay -f video4linux2 /dev/video0
The video stream which displayed on the screen was so unnatural. It looked like there was such a long delay between each frame. So the video was hardly moved. The log I have copied :
pi@raspberrypi ~ $ ffplay -f video4linux2 /dev/video0
avplay version 0.8.6-6:0.8.6-1+rpi1, Copyright (c) 2003-2013 the Libav developers
built on Mar 31 2013 13:58:10 with gcc 4.6.3
[video4linux2 @ 0x1ad83c0] Estimating duration from bitrate, this may be inaccurate
Input #0, video4linux2, from '/dev/video0':
Duration: N/A, start: 36107.154598, bitrate: 147456 kb/s
Stream #0.0: Video: rawvideo, yuyv422, 640x480, 147456 kb/s, 30 tbr, 1000k tbn, 30 tbc
[avsink @ 0x1ad9960] auto-inserting filter 'auto-inserted scaler 0' between the filter 'src' and the filter 'out'
[scale @ 0x1ad9d20] w:640 h:480 fmt:yuyv422 -> w:640 h:480 fmt:yuv420p flags:0x4
36163.33 A-V: 0.000 s:0.0 aq= 0KB vq= 3000KB sq= 0B f=8/8The final problem is that no sound was added in video file when I tried to add some in. I used the command :
avconv -i /home/pi/Desktop/test.mp4 -i /home/pi/Desktop/test.mp3 -map 0:0 -vcodec copy -map 1:0 -acodec copy output.h264
I have checked separately that the video and audio were both able to play. However the merged file only had video but no audio. The log is shown below, it seems that the process was succeed.
pi@raspberrypi ~ $ avconv -i /home/pi/Desktop/test.mp4 -i /home/pi/Desktop/test.mp3 -map 0:0 -vcodec copy -map 1:0 -acodec copy output.h264
avconv version 0.8.6-6:0.8.6-1+rpi1, Copyright (c) 2000-2013 the Libav developers
built on Mar 31 2013 13:58:10 with gcc 4.6.3
[h264 @ 0x1aa6680] max_analyze_duration reached
[h264 @ 0x1aa6680] Estimating duration from bitrate, this may be inaccurate
Input #0, h264, from '/home/pi/Desktop/test.mp4':
Duration: N/A, bitrate: N/A
Stream #0.0: Video: h264 (High), yuv420p, 640x480, 25 fps, 25 tbr, 1200k tbn, 2400k tbc
[mp3 @ 0x1aaa120] Estimating duration from bitrate, this may be inaccurate
Input #1, mp3, from '/home/pi/Desktop/test.mp3':
Metadata:
title : We Are Legend (Original Mix)
artist : Dimitri Vegas & Like Mike & Steve Aoki
album : SPINNIN RECORDS
encoded_by : iTunes 11.1.5
TBP : 128
genre : Progressive House
date : 2013
Duration: 00:00:11.14, start: 0.000000, bitrate: 191 kb/s
Stream #1.0: Audio: mp3, 48000 Hz, stereo, s16, 192 kb/s
File 'output.h264' already exists. Overwrite ? [y/N] y
Output #0, h264, to 'output.h264':
Metadata:
encoder : Lavf53.21.1
Stream #0.0: Video: libx264, yuv420p, 640x480, q=2-31, 90k tbn, 1200k tbc
Stream #0.1: Audio: libmp3lame, 48000 Hz, stereo, 192 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Stream #1:0 -> #0:1 (copy)
Press ctrl-c to stop encoding
frame= 144 fps= 0 q=-1.0 Lsize= 1684kB time=5.06 bitrate=2724.7kbits/s
video:1566kB audio:119kB global headers:0kB muxing overhead 0.000000%Could somebody helps me, please. And sorry for my bad english.
BTW : When I finally captured 200 frames, it turns out all of them were green.
-
How to Convert a binary(.bin) file to mp3 using libav in C language ?
28 août 2018, par danglingptrI have created a bin file using my_audio_decode.c. How I can get back the original mp3 file by encoding ?
P.S. I have gone through ffmpeg code, I modified as my requirements like codec to mp3 etc., You can see my modified code below.
#include
#include
#include
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libavutil></libavutil>channel_layout.h>
#include <libavutil></libavutil>common.h>
#include <libavutil></libavutil>frame.h>
#include <libavutil></libavutil>samplefmt.h>
/* check that a given sample format is supported by the encoder */
static int check_sample_fmt(const AVCodec *codec, enum AVSampleFormat sample_fmt)
{
const enum AVSampleFormat *p = codec->sample_fmts;
while (*p != AV_SAMPLE_FMT_NONE) {
if (*p == sample_fmt)
return 1;
p++;
}
return 0;
}
/* just pick the highest supported samplerate */
static int select_sample_rate(const AVCodec *codec)
{
const int *p;
int best_samplerate = 0;
if (!codec->supported_samplerates)
return 44100;
p = codec->supported_samplerates;
while (*p) {
if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))
best_samplerate = *p;
p++;
}
return best_samplerate;
}
/* select layout with the highest channel count */
static int select_channel_layout(const AVCodec *codec)
{
const uint64_t *p;
uint64_t best_ch_layout = 0;
int best_nb_channels = 0;
if (!codec->channel_layouts)
return AV_CH_LAYOUT_STEREO;
p = codec->channel_layouts;
while (*p) {
int nb_channels = av_get_channel_layout_nb_channels(*p);
if (nb_channels > best_nb_channels) {
best_ch_layout = *p;
best_nb_channels = nb_channels;
}
p++;
}
return best_ch_layout;
}
static void encode(AVCodecContext *ctx, AVFrame *frame, AVPacket *pkt,
FILE *output)
{
int ret;
/* send the frame for encoding */
ret = avcodec_send_frame(ctx, frame);
if (ret < 0) {
fprintf(stderr, "Error sending the frame to the encoder\n");
exit(1);
}
/* read all the available output packets (in general there may be any
* number of them */
while (ret >= 0) {
ret = avcodec_receive_packet(ctx, pkt);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
return;
else if (ret < 0) {
fprintf(stderr, "Error encoding audio frame\n");
exit(1);
}
fwrite(pkt->data, 1, pkt->size, output);
av_packet_unref(pkt);
}
}
int main(int argc, char **argv)
{
const char *filename;
const AVCodec *codec;
AVCodecContext *c= NULL;
AVFrame *frame;
AVPacket *pkt;
int i, j, k, ret;
FILE *f;
uint16_t *samples;
float t, tincr;
av_register_all();
avcodec_register_all();
if (argc <= 1) {
fprintf(stderr, "Usage: %s <output file="file">\n", argv[0]);
return 0;
}
filename = argv[1];
/* find the MP2 encoder */
codec = avcodec_find_encoder(AV_CODEC_ID_MP3);
if (!codec) {
fprintf(stderr, "Codec not found\n");
exit(1);
}
c = avcodec_alloc_context3(codec);
if (!c) {
fprintf(stderr, "Could not allocate audio codec context\n");
exit(1);
}
/* put sample parameters */
c->bit_rate = 64000;
/* check that the encoder supports s16 pcm input */
c->sample_fmt = AV_SAMPLE_FMT_S16P;
if (!check_sample_fmt(codec, c->sample_fmt)) {
fprintf(stderr, "Encoder does not support sample format %s",
av_get_sample_fmt_name(c->sample_fmt));
exit(1);
}
/* select other audio parameters supported by the encoder */
c->sample_rate = select_sample_rate(codec);
c->channel_layout = select_channel_layout(codec);
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
/* open it */
if (avcodec_open2(c, codec, NULL) < 0) {
fprintf(stderr, "Could not open codec\n");
exit(1);
}
f = fopen(filename, "wb");
if (!f) {
fprintf(stderr, "Could not open %s\n", filename);
exit(1);
}
/* packet for holding encoded output */
pkt = av_packet_alloc();
if (!pkt) {
fprintf(stderr, "could not allocate the packet\n");
exit(1);
}
/* frame containing input raw audio */
frame = av_frame_alloc();
if (!frame) {
fprintf(stderr, "Could not allocate audio frame\n");
exit(1);
}
frame->nb_samples = c->frame_size;
frame->format = c->sample_fmt;
frame->channel_layout = c->channel_layout;
/* allocate the data buffers */
ret = av_frame_get_buffer(frame, 0);
if (ret < 0) {
fprintf(stderr, "Could not allocate audio data buffers\n");
exit(1);
}
/* encode a single tone sound */
t = 0;
tincr = 2 * M_PI * 440.0 / c->sample_rate;
for (i = 0; i < 200; i++) {
/* make sure the frame is writable -- makes a copy if the encoder
* kept a reference internally */
ret = av_frame_make_writable(frame);
if (ret < 0)
exit(1);
samples = (uint16_t*)frame->data[0];
for (j = 0; j < c->frame_size; j++) {
samples[2*j] = (int)(sin(t) * 10000);
for (k = 1; k < c->channels; k++)
samples[2*j + k] = samples[2*j];
t += tincr;
}
encode(c, frame, pkt, f);
}
/* flush the encoder */
encode(c, NULL, pkt, f);
fclose(f);
av_frame_free(&frame);
av_packet_free(&pkt);
avcodec_free_context(&c);
return 0;
}
</output>But the code is confusing and it is taking only one input as argument, I need a clear working example c code like it has to take 2 args, like input file and output file.
-
How to save h264 frames as jpeg images using ffmpeg ?
17 août 2020, par Matthew CzarnekI would like to save thumbnails from a h264 stream that I'm turning into ffmpeg avpackets as jpegs.


I'm started with a h264 AVPacket (iframe) and decode it into an AVFrame using avcodec_send_packet/avcodec_receive_frame. Now trying to go from AVFrame and convert into AVPacket using avcodec_send_frame/avcodec_receive_packet


I can convert to png instead jpg, though I do get a the video looking like it's outputting three separate frames squeezed side by side into one. Wondering if it's one frame is R, next G, and finally B. I'm not sure, clearly I'm doing something wrong there. I figured it's possible it's the png encoder and I don't need it, so let's get jpg working first. But jpg is outputting unopenable files.


Any advice ?


Here is my code :


int output_thumbnails(AVPacket* video_packet)
{
 char png_file_name[max_chars_per_filename];
 char thumbnail_id_char[10];
 _itoa_s(thumbnail_id, thumbnail_id_char, 10);
 strcpy_s(png_file_name, max_chars_per_filename, time_stamped_filepath);
 strcat_s(png_file_name, max_chars_per_filename, time_stamped_filename);
 strcat_s(png_file_name, max_chars_per_filename, thumbnail_id_char);
 strcat_s(png_file_name, max_chars_per_filename, ".jpg");
 thumbnail_id++;

 int error_code = send_AVPacket_to_videocard(video_packet, av_codec_context_RTSP);

 //if (error_code == AVERROR_EOF)
 //{
 // // error_code = videocard_to_PNG(png_file_name, av_codec_context_RTSP, av_codec_RTSP);
 //}
 if (error_code == AVERROR(EAGAIN)) //send packets to videocard until function returns EAGAIN
 {
 error_code = videocard_to_PNG(png_file_name, av_codec_context_RTSP);

 //EAGAIN means that the video card buffer is ready to have the png pulled off of it
 if (error_code == AVERROR_EOF)
 {
 // error_code = videocard_to_PNG(png_file_name, av_codec_context_RTSP, av_codec_RTSP);
 }
 else if (error_code == AVERROR(EAGAIN))
 {

 }
 else
 {
 deal_with_av_errors(error_code, __LINE__, __FILE__);
 }
 }
 else
 {
 deal_with_av_errors(error_code, __LINE__, __FILE__);
 }
 
 return 0;
}



VideoThumbnailGenerator.h :
#include "VideoThumbnailGenerator.h"


bool decoder_context_created = false;
bool encoder_context_created = false;

AVCodecContext* h264_decoder_codec_ctx;
AVCodecContext* thumbnail_encoder_codec_ctx;

int send_AVPacket_to_videocard(AVPacket* packet, AVCodecContext* codec_ctx)
{
 if(!decoder_context_created)
 {
 AVCodec* h264_codec = avcodec_find_decoder(codec_ctx->codec_id);
 h264_decoder_codec_ctx = avcodec_alloc_context3(h264_codec);

 h264_decoder_codec_ctx->width = codec_ctx->width;
 h264_decoder_codec_ctx->height = codec_ctx->height;
 h264_decoder_codec_ctx->pix_fmt = AV_PIX_FMT_RGB24;
 h264_decoder_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;
 h264_decoder_codec_ctx->skip_frame = AVDISCARD_NONINTRA;//AVDISCARD_NONREF;//AVDISCARD_NONINTRA;
 
 h264_decoder_codec_ctx->time_base.num = 1;
 h264_decoder_codec_ctx->time_base.den = 30;

 h264_decoder_codec_ctx->extradata = codec_ctx->extradata;
 h264_decoder_codec_ctx->extradata_size = codec_ctx->extradata_size;
 
 int error_code = avcodec_open2(h264_decoder_codec_ctx, h264_codec, NULL);
 if (!h264_codec) {
 return -1;
 }

 if (error_code < 0)
 {
 return error_code;
 }
 decoder_context_created = true;
 }

 
 //use hardware decoding to decode video frame
 int error_code = avcodec_send_packet(h264_decoder_codec_ctx, packet);
 if(error_code == AVERROR(EAGAIN))
 {
 return AVERROR(EAGAIN);
 }
 if(error_code<0)
 {
 printf("Error: Could not send packet to video card");
 return error_code;
 }

 return 0;
}

int videocard_to_PNG(char *png_file_path, AVCodecContext* codec_ctx)
{
 if (!encoder_context_created)
 {
 //AVCodec* thumbnail_codec = avcodec_find_encoder(AV_CODEC_ID_PNG);
 AVCodec* thumbnail_codec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);
 thumbnail_encoder_codec_ctx = avcodec_alloc_context3(thumbnail_codec);

 thumbnail_encoder_codec_ctx->width = 128;
 thumbnail_encoder_codec_ctx->height = (int)(((float)codec_ctx->height/(float)codec_ctx->width) * 128);
 thumbnail_encoder_codec_ctx->pix_fmt = AV_PIX_FMT_RGB24; //AV_PIX_FMT_YUVJ420P
 thumbnail_encoder_codec_ctx->codec_type = AVMEDIA_TYPE_VIDEO;

 thumbnail_encoder_codec_ctx->time_base.num = 1;
 thumbnail_encoder_codec_ctx->time_base.den = 30;

 bool thread_check = thumbnail_encoder_codec_ctx->thread_type & FF_THREAD_FRAME;
 bool frame_threads_check = thumbnail_encoder_codec_ctx->codec->capabilities & AV_CODEC_CAP_FRAME_THREADS;
 
 int error_code = avcodec_open2(thumbnail_encoder_codec_ctx, thumbnail_codec, NULL);
 if (!thumbnail_codec) {
 return -1;
 }

 if (error_code < 0)
 {
 return error_code;
 }
 encoder_context_created = true;
 }
 
 AVFrame* thumbnail_frame = av_frame_alloc();
 AVPacket* thumbnail_packet = av_packet_alloc();
 //av_init_packet(png_packet);
 int error_code = avcodec_receive_frame(h264_decoder_codec_ctx, thumbnail_frame);

 //check for errors everytime
 //note EAGAIN errors won't get here since they won't get past while
 if (error_code < 0 && error_code != AVERROR(EAGAIN))
 {
 printf("Error: Could not get frame from video card");
 return error_code;
 }
 //empty buffer if there are any more frames to pull (there shouldn't be)
 //while(error_code != AVERROR(EAGAIN))
 //{
 // //check for errors everytime
 // //note EAGAIN errors won't get here since they won't get past while
 // if (error_code < 0)
 // {
 // printf("Error: Could not get frame from video card");
 // return error_code;
 // }
 // 
 // error_code = avcodec_receive_frame(h264_decoder_codec_ctx, png_frame);
 //}

 //now we convert back to AVPacket, this time one holding PNG info, so we can store to file
 error_code = avcodec_send_frame(thumbnail_encoder_codec_ctx, thumbnail_frame);

 if (error_code >= 0) {
 error_code = avcodec_receive_packet(thumbnail_encoder_codec_ctx, thumbnail_packet);

 FILE* out_PNG;

 errno_t err = fopen_s(&out_PNG, png_file_path, "wb");
 if (err == 0) {
 fwrite(thumbnail_packet->data, thumbnail_packet->size, 1, out_PNG);
 }
 fclose(out_PNG);

 }
 return error_code;
}