
Recherche avancée
Autres articles (92)
-
Qualité du média après traitement
21 juin 2013, parLe bon réglage du logiciel qui traite les média est important pour un équilibre entre les partis ( bande passante de l’hébergeur, qualité du média pour le rédacteur et le visiteur, accessibilité pour le visiteur ). Comment régler la qualité de son média ?
Plus la qualité du média est importante, plus la bande passante sera utilisée. Le visiteur avec une connexion internet à petit débit devra attendre plus longtemps. Inversement plus, la qualité du média est pauvre et donc le média devient dégradé voire (...) -
Les images
15 mai 2013 -
Emballe médias : à quoi cela sert ?
4 février 2011, parCe plugin vise à gérer des sites de mise en ligne de documents de tous types.
Il crée des "médias", à savoir : un "média" est un article au sens SPIP créé automatiquement lors du téléversement d’un document qu’il soit audio, vidéo, image ou textuel ; un seul document ne peut être lié à un article dit "média" ;
Sur d’autres sites (9019)
-
ffmpeg sync issue when muxing audio and video
25 août 2018, par tweecoreI have an issue with ffmpeg. I am not a pro user so I’m on a constant trial & error mode. I would need a little help from this community that helped me countless time before.
I downloaded separate audio and video segments of a video stream. Both audio and video were shown in the Network tab as being .m4s files, but the .m3u8 playlist gave me .ts files for both. So then I downloaded the whole audio batch and concatenated it into a single .ts file. I did the same with video, but using a .mp4 output.EDIT : When merging the audio .ts files, I encounter this issue :
[mpegts @ 0x7f88cb805600] Non-monotonous DTS in output stream 0:0;
previous: 1077120, current: 1073280; changing to 1077121. This may
result in incorrect timestamps in the output file.UPDATE : Just noticed that when I examine the .ts audio file with MediaInfo, the frame rate is 41.667 fps (1152 SPF).
So now I have two files : a working single .ts with audio and a working single .mp4 with video.
Only when I tried to merge them, however, I noticed that audio and video resulted as out of sync.
This is because audio has a different length from video (don’t know why, they were the same amount of segments : 750).
Audio info :Input #0, mpegts, from 'output.ts':
Duration: 01:14:12.11, start: 1.400000, bitrate: 434 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Audio: mp2 ([3][0][0][0] / 0x0003), 48000 Hz, stereo, fltp, 384 kb/sI can see that for some reason, it starts at 1.4s.
Video info :
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'output.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf58.12.100
Duration: 01:14:57.84, start: 0.000000, bitrate: 2425 kb/s
Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1280x720 [SAR 1:1 DAR 16:9], 2423 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc (default)
Metadata:
handler_name : VideoHandlerVideo and audio are synced at the beginning (somehow the 1.4 start makes sense) but audio starts drifting and goes out of sync after a while. How do I solve the issue ?
-
libav how to change stream codec
9 novembre 2018, par Rodolfo PicoretiI am trying to reproduce with libav the same thing that the following ffmpeg command does :
ffmpeg -f v4l2 -framerate 25 -video_size 640x480 -i /dev/video 0 -f
mpegts -codec:v mpeg1video -s 640x480 -b:v 1000k -bf 0 -muxdelay
0.001 http://localhost:8081/supersecretI manage to reproduce most part of it. The problem is that when allocating the "mpegts" stream (line 23) the codec that is selected is "mpeg2video", but I need it to be "mpeg1video". I tried forcing the codec_id variable to be "mpeg1video" (line 25) and it kinda worked, although I get a lot of artifacts in the image so I am guessing this is not how you do it. How can I properly change the codec in this case (that is, the "-codec:v mpeg1video" part of the ffmpeg command) ?
C++ code being used :
#include <exception>
#include <opencv2></opencv2>core.hpp>
#include <opencv2></opencv2>highgui.hpp>
#include <opencv2></opencv2>imgproc.hpp>
extern "C" {
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libavutil></libavutil>error.h>
#include <libavutil></libavutil>frame.h>
#include <libavutil></libavutil>imgutils.h>
}
struct AVStreamer {
AVFormatContext* format_context;
AVStream* stream;
AVCodecContext* codec_context;
AVCodec* codec;
AVFrame* frame;
int64_t next_pts;
void init_format_context(const char* url) {
avformat_alloc_output_context2(&format_context, nullptr, "mpegts", url);
if (format_context == nullptr) throw std::runtime_error("Could not create output context");
format_context->oformat->video_codec = AV_CODEC_ID_MPEG1VIDEO;
}
void init_codec() {
auto codec_id = format_context->oformat->video_codec;
codec = avcodec_find_encoder(codec_id);
if (codec == nullptr) throw std::runtime_error("Could not find encoder");
}
void init_stream() {
stream = avformat_new_stream(format_context, nullptr);
if (stream == nullptr) throw std::runtime_error("Failed to alloc stream");
stream->id = format_context->nb_streams - 1;
}
void init_codec_context() {
codec_context = avcodec_alloc_context3(codec);
if (codec_context == nullptr) throw std::runtime_error("Failed to alloc encoding context");
auto codec_id = format_context->oformat->video_codec;
codec_context->codec_id = codec_id;
codec_context->bit_rate = 400000;
codec_context->width = 640;
codec_context->height = 480;
stream->time_base = AVRational{1, 30};
codec_context->time_base = stream->time_base;
codec_context->gop_size = 30; // one intra frame every gop_size
// codec_context->max_b_frames = 0; // output delayed by max_b_frames
codec_context->pix_fmt = AV_PIX_FMT_YUV420P;
if (codec_context->codec_id == AV_CODEC_ID_MPEG1VIDEO) { codec_context->mb_decision = 2; }
if (format_context->oformat->flags & AVFMT_GLOBALHEADER) {
codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
}
void init_frame() {
frame = av_frame_alloc();
if (frame == nullptr) throw std::runtime_error("Failed to alloc frame");
frame->format = codec_context->pix_fmt;
frame->width = codec_context->width;
frame->height = codec_context->height;
auto status = av_frame_get_buffer(frame, 32);
if (status < 0) { throw std::runtime_error("Could not allocate frame data.\n"); }
}
void open_stream(const char* url) {
int status = avcodec_open2(codec_context, codec, nullptr);
if (status != 0) throw std::runtime_error("Failed to open codec");
// copy the stream parameters to the muxer
status = avcodec_parameters_from_context(stream->codecpar, codec_context);
if (status < 0) throw std::runtime_error("Could not copy the stream parameters");
av_dump_format(format_context, 0, url, 1);
if (!(format_context->oformat->flags & AVFMT_NOFILE)) {
status = avio_open(&format_context->pb, url, AVIO_FLAG_WRITE);
if (status < 0) throw std::runtime_error("Could not open output file");
}
// Write the stream header, if any.
status = avformat_write_header(format_context, nullptr);
if (status < 0) throw std::runtime_error("Error occurred when opening output file");
}
AVStreamer(const char* url) : next_pts(0) {
init_format_context(url);
init_codec();
init_stream();
init_codec_context();
init_frame();
open_stream(url);
}
virtual ~AVStreamer() {
avformat_free_context(format_context);
avcodec_free_context(&codec_context);
av_frame_free(&frame);
}
void send(cv::Mat const& image) {
cv::cvtColor(image, image, CV_BGR2YUV);
cv::Mat planes[3];
cv::split(image, planes);
cv::pyrDown(planes[1], planes[1]);
cv::pyrDown(planes[2], planes[2]);
if (av_frame_make_writable(frame) < 0) {
throw std::runtime_error("Failed to make frame writable");
}
frame->data[0] = planes[0].data;
frame->linesize[0] = planes[0].step;
frame->data[1] = planes[1].data;
frame->linesize[1] = planes[1].step;
frame->data[2] = planes[2].data;
frame->linesize[2] = planes[2].step;
frame->pts = next_pts++;
AVPacket packet;
av_init_packet(&packet);
int status = avcodec_send_frame(codec_context, frame);
if (status < 0) throw std::runtime_error("Send frame failed");
status = avcodec_receive_packet(codec_context, &packet);
if (status == AVERROR(EAGAIN)) { return; }
if (status < 0) throw std::runtime_error("Receive packet failed");
av_packet_rescale_ts(&packet, codec_context->time_base, stream->time_base);
packet.stream_index = stream->index;
av_interleaved_write_frame(format_context, &packet);
}
};
int main(int argc, char** argv) {
av_register_all();
avformat_network_init();
auto url = argc == 2 ? argv[1] : "http://localhost:8081/supersecret";
AVStreamer streamer(url);
cv::VideoCapture video(0);
assert(video.isOpened() && "Failed to open video");
for (;;) {
cv::Mat image;
video >> image;
streamer.send(image);
}
}
</exception> -
Create movie programatically with ffmpeg
16 janvier 2019, par Martin DelilleI would like to create a movie programatically with ffmpeg.
Here is my code :
QString fileName = "test.mov";
static char errorString[AV_ERROR_MAX_STRING_SIZE];
printf("Video encoding\n");
AVOutputFormat *outputFormat = av_guess_format(nullptr, fileName.toStdString().c_str(), nullptr);
if (outputFormat == nullptr) {
qDebug() << "Could not find suitable format for" << fileName;
return false;
}
enum AVCodecID codec_id = AV_CODEC_ID_MJPEG;
qDebug() << "Format Name:" << outputFormat->name;
qDebug() << "Format Video Codec:" << outputFormat->video_codec;
outputFormat->video_codec = codec_id;
/// allocate the output media context, formatContext
AVFormatContext *formatContext = avformat_alloc_context();
formatContext->oformat = outputFormat;
// find the mpeg1 video encoder
AVCodec *codec = avcodec_find_encoder(codec_id);
if (!codec) {
qDebug() << "codec not found";
return false;
}
qDebug() << "Codec name:" << codec->name;
AVStream *videoStream = avformat_new_stream(formatContext, codec);
// put sample parameters
videoStream->codecpar->bit_rate = 400000;
videoStream->codecpar->width = width;
videoStream->codecpar->height = height;
videoStream->codecpar->codec_type = AVMEDIA_TYPE_VIDEO;
videoStream->codecpar->format = AV_PIX_FMT_YUVJ422P;
videoStream->time_base.num = 1;
videoStream->time_base.den = 25;
AVCodecContext *codecContext = avcodec_alloc_context3(nullptr);
codecContext->codec_id = codec_id;
codecContext->codec_type = AVMEDIA_TYPE_VIDEO;
codecContext->width = width;
codecContext->height = height;
codecContext->time_base.num = 1;
codecContext->time_base.den = 25;
codecContext->pix_fmt = AV_PIX_FMT_YUVJ422P;
if (int error = avcodec_parameters_to_context(codecContext, videoStream->codecpar)) {
qDebug() << "Error parameting the context:" << av_make_error_string(errorString, AV_ERROR_MAX_STRING_SIZE, error);
return false;
}
// open it
if (int error = avcodec_open2(codecContext, codec, nullptr)) {
qDebug() << "Could not open codec:" << av_make_error_string(errorString, AV_ERROR_MAX_STRING_SIZE, error);
return false;
}
// alloc image and output buffer
AVFrame *frame = av_frame_alloc();
frame->format = codecContext->pix_fmt;
frame->width = codecContext->width;
frame->height = codecContext->height;
if (int error = av_image_alloc(frame->data, frame->linesize, frame->width, frame->height, static_cast<enum avpixelformat="avpixelformat">(frame->format), 0)) {
qDebug() << "Error allocating image data:" << av_make_error_string(errorString, AV_ERROR_MAX_STRING_SIZE, error);
return false;
}
// Open the output file, if needed
if (!(outputFormat->flags & AVFMT_NOFILE)) {
if (avio_open(&formatContext->pb, fileName.toStdString().c_str(), AVIO_FLAG_WRITE) < 0) {
qDebug() << "Could not open" << fileName;
return false;
}
}
// some formats want stream headers to be separate
if (formatContext->oformat->flags & AVFMT_GLOBALHEADER) {
codecContext->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
if (int error = avformat_write_header(formatContext, nullptr)) {
qDebug() << "error writing header:" << av_make_error_string(errorString, AV_ERROR_MAX_STRING_SIZE, error);
return false;
}
</enum>Unfortunately, when I execute it I have the following output :
Format Name: mov
Format Video Codec: 27
Codec name: mjpeg
[mov @ 0x1048c7000] Could not find tag for codec none in stream #0, codec not currently supported in container
error writing header: Invalid argumentWhat am I doing wrong ?