
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 (112)
-
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 ;
-
Websites made with MediaSPIP
2 mai 2011, parThis page lists some websites based on MediaSPIP.
-
Creating farms of unique websites
13 avril 2011, parMediaSPIP platforms can be installed as a farm, with a single "core" hosted on a dedicated server and used by multiple websites.
This allows (among other things) : implementation costs to be shared between several different projects / individuals rapid deployment of multiple unique sites creation of groups of like-minded sites, making it possible to browse media in a more controlled and selective environment than the major "open" (...)
Sur d’autres sites (15305)
-
Encoding YUV file (uncompressed video) to mp4 playable file using H264 encoding with ffmpeg c++ (NOT command line)
20 avril 2023, par devprogMy goal is to encode a yuv file to a playable mp4 file (can be played with VLC) with ffmpeg c++ code.


First I have a source_1920x1080p30.mpg video (compressed) file.
Using ffmpeg CLI I created output_test.yuv file.

ffmpeg -i source_1920x1080p30.mpg -c:v rawvideo -pix_fmt yuv420p output_test.yuv


I used
ffplay -f rawvideo -pixel_format yuv420p -video_size 1920x1080 output_test.yuv
which played well.
Also usedffmpeg -f rawvideo -pix_fmt yuv420p -s:v 1920x1080 -r 30 -i output_test.yuv -c:v libx264 vid.mp4

and the new vid.mp4 was playble.
So I have good yuv file.

I want to encode this output_test.yuv YUV file with ffmpeg by code.
I tested the ffmpeg site encode example ffmpeg site encode example and it was running good.


In that example, the frames are self made inside the code - But I want that the input would be my YUV file.


Becasue I used ffmpeg CLI to convert YUV to mp4 (as noted above) I am sure it can be done by code - but I dont how..


So, I tried to add to their example the use of the methods :
avformat_open_input() & avformat_find_stream_info()


#include 
#include 
#include 

extern "C" {
#include <libavformat></libavformat>avformat.h>
#include <libavcodec></libavcodec>avcodec.h>
 
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>imgutils.h>
} 
// ffmpeg method.
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)
{
 
 int ret;
 char errbuf[100];

 AVFormatContext* ifmt_ctx = avformat_alloc_context();

 AVDictionary* options = NULL;
 //av_dict_set(&options, "framerate", "30", 0);
 av_dict_set(&options, "video_size", "1920x1080", 0);
 av_dict_set(&options,"pixel_format", "yuv420p",0);
 av_dict_set(&options, "vcodec", "rawvideo", 0);

 if ((ret = avformat_open_input(&ifmt_ctx, "output_test.yuv", NULL, &options)) < 0) {
 av_strerror(ret, errbuf, sizeof(errbuf));
 fprintf(stderr, "Unable to open err=%s\n", errbuf);
 }

 const AVCodec *pCodec = avcodec_find_decoder(AV_CODEC_ID_RAWVIDEO); //Get pointer to rawvideo codec.

 AVCodecContext *pCodecContext = avcodec_alloc_context3(pCodec); //Allocate codec context.

 //Fill the codec context based on the values from the codec parameters.
 AVStream *vid_stream = ifmt_ctx->streams[0];
 avcodec_parameters_to_context(pCodecContext, vid_stream->codecpar);

 avcodec_open2(pCodecContext, pCodec, NULL); //Open the codec

 //Allocate memory for packet and frame
 AVPacket *pPacket = av_packet_alloc();
 AVFrame *pFrame = av_frame_alloc();

 // For output use:
 
 const char *filename, *codec_name;
 const AVCodec *codec;
 AVCodecContext *c= NULL;
 int i, x, y;
 FILE *f;
 // origin ffmpeg code - 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);
 }
// This is ffmpeg code that I removed 
// 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, 0);
// if (ret < 0) {
// fprintf(stderr, "Could not allocate the video frame data\n");
// exit(1);
// }

 pFrame->format = c->pix_fmt;
 pFrame->width = c->width;
 pFrame->height = c->height;
 

 //Read video frames and pass through the decoder.
 //Note: Since the video is rawvideo, we don't really have to pass it through the decoder.
 while (av_read_frame(ifmt_ctx, pPacket) >= 0) 
 {
 //The video is not encoded - passing through the decoder is simply copy the data.
 avcodec_send_packet(pCodecContext, pPacket); //Supply raw packet data as input to the "decoder".
 avcodec_receive_frame(pCodecContext, pFrame); //Return decoded output data from the "decoder".

 fflush(stdout);
// This is ffmpeg code that I removed 
// /* Make sure the frame data is writable.
// On the first round, the frame is fresh from av_frame_get_buffer()
// and therefore we know it is writable.
// But on the next rounds, encode() will have called
// avcodec_send_frame(), and the codec may have kept a reference to
// the frame in its internal structures, that makes the frame
// unwritable.
// av_frame_make_writable() checks that and allocates a new buffer
// for the frame only if necessary.
// */
// ret = av_frame_make_writable(frame);
// if (ret < 0)
// exit(1);
// 
// /* Prepare a dummy image.
// In real code, this is where you would have your own logic for
// filling the frame. FFmpeg does not care what you put in the
// frame.
// */
// /* 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;
// }
// }
 
 pFrame->pts = i;
 
 /* encode the image */
 encode(c, pFrame, pkt, f);
 }
 
 /* flush the encoder */
 encode(c, NULL, pkt, f);
 
 /* Add sequence end code to have a real MPEG file.
 It makes only sense because this tiny examples writes packets
 directly. This is called "elementary stream" and only works for some
 codecs. To create a valid file, you usually need to write packets
 into a proper file format or protocol; see mux.c.
 */
 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(&pFrame);
 av_packet_free(&pkt);
 
 return 0;
}
</codec></output>


EDIT : I added combined code of ffmpeg and reading/decoding from YUV file according @Rotem (thanks). Frame from decoder pushed right to encode() method of ffmpeg. The out video was not good... Should I manipulate the YUV input frame before send it to encode() method ? if yes how to do it ?


-
ffmpeg stream recording : Invalid data found when processing input
14 mai 2020, par uncommon_nameI am trying to run this command :



ffmpeg -re -i "playlist-78293.m3u8" -vcodec libx264 -vprofile baseline -g 30 -acodec aac -strict -2 -loglevel +trace -f flv rtmp://127.0.0.1:1902/live2/78293




I get the
Invalid data found when processing input
error after running this.


This is the output I get in console :



ffmpeg version git-2020-05-01-39fb1e9 Copyright (c) 2000-2020 the FFmpeg developers
 built with gcc 9.3.1 (GCC) 20200328
 configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libdav1d --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libsrt --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --disable-w32threads --enable-libmfx --enable-ffnvcodec --enable-cuda-llvm --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth --enable-libopenmpt --enable-amf
 libavutil 56. 43.100 / 56. 43.100
 libavcodec 58. 82.100 / 58. 82.100
 libavformat 58. 42.101 / 58. 42.101
 libavdevice 58. 9.103 / 58. 9.103
 libavfilter 7. 80.100 / 7. 80.100
 libswscale 5. 6.101 / 5. 6.101
 libswresample 3. 6.100 / 3. 6.100
 libpostproc 55. 6.100 / 55. 6.100
Splitting the commandline.
Reading option '-re' ... matched as option 're' (read input at native frame rate) with argument '1'.
Reading option '-i' ... matched as input url with argument 'playlist-78293.m3u8'.
Reading option '-vcodec' ... matched as option 'vcodec' (force video codec ('copy' to copy stream)) with argument 'libx264'.
Reading option '-vprofile' ... matched as AVOption 'vprofile' with argument 'baseline'.
Reading option '-g' ... matched as AVOption 'g' with argument '30'.
Reading option '-acodec' ... matched as option 'acodec' (force audio codec ('copy' to copy stream)) with argument 'aac'.
Reading option '-strict' ...Routing option strict to both codec and muxer layer
 matched as AVOption 'strict' with argument '-2'.
Reading option '-loglevel' ... matched as option 'loglevel' (set logging level) with argument '+trace'.
Reading option '-f' ... matched as option 'f' (force format) with argument 'flv'.
Reading option 'rtmp://136.243.91.210:1902/live2/78293' ... matched as output url.
Finished splitting the commandline.
Parsing a group of options: global .
Applying option loglevel (set logging level) with argument +trace.
Successfully parsed a group of options.
Parsing a group of options: input url playlist-78293.m3u8.
Applying option re (read input at native frame rate) with argument 1.
Successfully parsed a group of options.
Opening an input file: playlist-78293.m3u8.
[NULL @ 0000026316afd700] Opening 'playlist-78293.m3u8' for reading
[file @ 0000026316afdec0] Setting default whitelist 'file,crypto,data'
[AVIOContext @ 0000026316b06100] Statistics: 32070 bytes read, 0 seeks
playlist-78293.m3u8: Invalid data found when processing input




I simplified a part of the input file to present it here (for example the path part and the ts Urls) :



#EXTM3U
#EXT-X-VERSION:3
#EXT-X-TARGETDURATION:6
#EXT-X-MEDIA-SEQUENCE:19981
#EXT-X-TWITCH-ELAPSED-SECS:39962.000
#EXT-X-TWITCH-TOTAL-SECS:39994.000
#EXT-X-DATERANGE:ID="1e265bc0de2e42cc8523222e6e9c8ba7",CLASS="twitch-assignment",START-DATE="2020-05-14T13:37:04.907Z",END-ON-NEXT=YES,X-TV-TWITCH-SERVING-ID="1e265bc0de2e42cc8523222e6e9c8ba7",X-TV-TWITCH-NODE="video-edge-18dbf4.jfk06",X-TV-TWITCH-CLUSTER="jfk06"
#EXT-X-DATERANGE:ID="source-1589462114",CLASS="twitch-stream-source",START-DATE="2020-05-14T13:15:14.907Z",END-ON-NEXT=YES,X-TV-TWITCH-STREAM-SOURCE="live"
#EXT-X-DATERANGE:ID="trigger-1589462114",CLASS="twitch-trigger",START-DATE="2020-05-14T13:15:14.907Z",END-ON-NEXT=YES,X-TV-TWITCH-TRIGGER-URL="**path**\playlist-78293.m3u8"
#EXT-X-PROGRAM-DATE-TIME:2020-05-14T13:38:00.907Z
#EXTINF:2.000,live
https://url.net/v1/segment/somestuff.ts




I'm new to ffmpeg. So any help would be appreciated.


-
Some H264-mp4 videos can't be loaded by any non-Chromium based web browser
3 mai 2020, par LaizrodI regularly use ffmpeg to encode some videos, blu-ray etc in mp4 files (encoded in H264 and AAC) in order to be played on web browsers.
Chromium based browsers such as Google Chrome or the new Microsoft Edge can play all of my files flawlessly.
But today I noticed that some video files couldn't be loaded by some web browsers. It looks like they can't be loaded by any non-Chromium based web browser. (Safari, Firefox etc..)



So I decided to check out and compare the specs of files that work with any web browser, and files that doesn't work with non-Chromium based browsers.



There's what I got :



- 

- Playable by any web browser :





General
Complete name : /storage/100.mp4
Format : MPEG-4
Format profile : Base Media / Version 2
Codec ID : mp42 (isom/iso2/avc1/mp41)
File size : 624 MiB
Duration : 23 min 54 s
Overall bit rate : 3 648 kb/s
Encoded date : UTC 2019-10-02 22:15:27
Tagged date : UTC 2019-10-02 22:15:27
Writing application : HandBrake 1.2.2 2019022300

Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L4
Format settings : CABAC / 1 Ref Frames
Format settings, CABAC : Yes
Format settings, ReFrames : 1 frame
Format settings, GOP : M=3, N=24
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 23 min 54 s
Bit rate : 3 481 kb/s
Width : 1 920 pixels
Height : 1 080 pixels
Display aspect ratio : 16:9
Frame rate mode : Variable
Frame rate : 23.976 (24000/1001) FPS
Minimum frame rate : 23.974 FPS
Maximum frame rate : 23.981 FPS
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.070
Stream size : 595 MiB (95%)
Encoded date : UTC 2019-10-02 22:15:27
Tagged date : UTC 2019-10-02 22:15:27
Color range : Limited
Color primaries : BT.709
Transfer characteristics : BT.709
Matrix coefficients : BT.709
Codec configuration box : avcC

Audio
ID : 2
Format : AAC LC
Format/Info : Advanced Audio Codec Low Complexity
Codec ID : mp4a-40-2
Duration : 23 min 54 s
Bit rate mode : Constant
Bit rate : 160 kb/s
Channel(s) : 2 channels
Channel layout : L R
Sampling rate : 44.1 kHz
Frame rate : 43.066 FPS (1024 SPF)
Compression mode : Lossy
Stream size : 27.4 MiB (4%)
Title : Stereo
Language : Japanese
Default : Yes
Alternate group : 1
Encoded date : UTC 2019-10-02 22:15:27
Tagged date : UTC 2019-10-02 22:15:27




- 

- Unplayable by non-chromium based web browsers :





General
Complete name : /storage/DL/test.mp4
Format : MPEG-4
Format profile : Base Media
Codec ID : isom (isom/iso2/avc1/mp41)
File size : 329 MiB
Duration : 23 min 52 s
Overall bit rate : 1 926 kb/s
Writing application : Lavf58.20.100

Video
ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format profile : High@L4
Format settings : CABAC / 4 Ref Frames
Format settings, CABAC : Yes
Format settings, ReFrames : 4 frames
Codec ID : avc1
Codec ID/Info : Advanced Video Coding
Duration : 23 min 52 s
Bit rate : 1 792 kb/s
Width : 1 920 pixels
Height : 1 080 pixels
Display aspect ratio : 16:9
Frame rate mode : Constant
Frame rate : 23.976 (24000/1001) FPS
Color space : YUV
Chroma subsampling : 4:2:0
Bit depth : 8 bits
Scan type : Progressive
Bits/(Pixel*Frame) : 0.036
Stream size : 306 MiB (93%)
Writing library : x264 core 155 r2917 0a84d98
Encoding settings : cabac=1 / ref=1 / deblock=1:0:0 / analyse=0x3:0x113 / me=hex / subme=2 / psy=1 / psy_rd=1.00:0.00 / mixed_ref=0 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=1 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=0 / threads=34 / lookahead_threads=8 / sliced_threads=0 / nr=0 / decimate=1 / interlaced=0 / bluray_compat=0 / constrained_intra=0 / bframes=3 / b_pyramid=2 / b_adapt=1 / b_bias=0 / direct=1 / weightb=1 / open_gop=0 / weightp=1 / keyint=250 / keyint_min=23 / scenecut=40 / intra_refresh=0 / rc_lookahead=10 / rc=crf / mbtree=1 / crf=21.0 / qcomp=0.60 / qpmin=0 / qpmax=69 / qpstep=4 / ip_ratio=1.40 / aq=1:1.00
Language : Japanese
Codec configuration box : avcC

Audio
ID : 2
Format : AAC LC
Format/Info : Advanced Audio Codec Low Complexity
Codec ID : mp4a-40-2
Duration : 23 min 52 s
Bit rate mode : Constant
Bit rate : 128 kb/s
Channel(s) : 2 channels
Channel layout : L R
Sampling rate : 48.0 kHz
Frame rate : 46.875 FPS (1024 SPF)
Compression mode : Lossy
Stream size : 21.9 MiB (7%)
Language : Japanese
Default : Yes
Alternate group : 1




My knowledge is limited and I'm unable to understand why and which differences are causing my issue.
Can someone identify the problem and help me fix it with ffmpeg ?



Thank you