Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
How to set buffer size when encoding video ?
1er février 2016, par NyarukoI use the following code to encode an incoming h264 stream:
int ret; ffmpeg::AVDictionary *opts2 = NULL; av_dict_set(&opts2, "preset", "medium", 0); av_dict_set(&opts2, "crf", "29", 0); av_dict_set(&opts2, "profile", "baseline", 0); av_dict_set(&opts2, "level", "30", 0); av_dict_set(&opts2, "maxrate", "200000", 0); av_dict_set(&opts2, "minrate", "0", 0); av_dict_set(&opts2, "bufsize", "2000000", 0); ffmpeg::AVOutputFormat* fmt = ffmpeg::av_guess_format("mpeg", NULL, NULL); // Open the context //--------------------------------------------------------------------- outFormatCtx = ffmpeg::avformat_alloc_context(); if (!outFormatCtx) { return false; } //Set the output format //---------------------------------------------------------------------- outFormatCtx->oformat = fmt; // Open the output file //------------------------------------- if (!(outFormatCtx->flags & AVFMT_NOFILE)) { ret = ffmpeg::avio_open2(&outFormatCtx->pb, "Record.avi", AVIO_FLAG_WRITE, NULL, NULL); if (ret < 0) { return false; } } // Create the output stream // ------------------------------------- ffmpeg::AVStream* out_stream = ffmpeg::avformat_new_stream(outFormatCtx, inputStream->codec->codec); if (!out_stream) { return false; } //Set the stream parameters //------------------------------------- ret = ffmpeg::avcodec_copy_context(out_stream->codec, inputStream->codec); if (ret < 0) { return false; } // Check then setup for global headers //------------------------------------- if (outFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; av_dump_format(outFormatCtx, 0, "Record.avi", 1); //Write the header //-------------------------------------- ret = ffmpeg::avformat_write_header(outFormatCtx, &opts2); if (ret < 0) { return false; }
However I always get the warning:
"VBV buffer size not set, using default size of 130KB\n" "If you want the mpeg file to be compliant to some specification\n" "Like DVD, VCD or others, make sure you set the correct buffer size\n"
I have tried very hard, but how could I set this VBV buffer size?
-
FFmpeg splitting large files
1er février 2016, par Yusaf KhaliqI need to split a large video file into multiple pieces quickly and without files with errors. The basic idea is, I have a 2GB video file which I want to change to multiple formats. I have read some encoders can not make use of more than 1 core when encoding to a different format. So I had the idea to split the large file (which is very quick) into 4/8 pieces depending on how many cores I have available on individual servers. re-encode each piece to a new format and use these to display video in sequence.
so
testfile.mp4
becomes
- testfile_piece_1.mp4
- testfile_piece_2.mp4
- testfile_piece_3.mp4
- testfile_piece_4.mp4
these can then be individually be converted.
The solution should not be format specific. However I have found issues with mp4 files.
I have tried the command below, which works really well and fast but creates files with errors.
ffmpeg -i testfile.mp4 -ss 00:00:00 -t 00:20:00 -c copy testfile_piece_1.mp4
When I play the testfile_piece_1.mp4 on VLC it works fine. An issue arises when converting the split file to a different height and width mp4 file. I would get an error similar to "moov atom not found"
I tried adding
-movflags faststart
with no luckI then came across this library https://code.google.com/archive/p/moovrelocator/ which fixed the moov issue but I would then get an error with regards to aac "Error while opening encoder for output stream #0.0 - maybe incorrect parameters such as bit_rate, rate, width or height"
The other way of splitting the files is useless but involved re-encoding the file. not too bad for smaller file size but the 2GB file would probably take days to complete.
Is there a way to split the largr file quickly without producing files with errors? I have been working on it for days with no luck.
Console output for comment - FFmpeg splitting large files
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/usr/share/nginx/html/uploads/testfile01.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf56.36.100 Duration: 00:05:02.08, start: 302.120000, bitrate: 3254 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709) , 1920x1080 [SAR 1:1 DAR 16:9], 3252 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (de fault) Metadata: handler_name : VideoHandler [libx264 @ 0x165ffc0] width not divisible by 2 (853x480) Output #0, mp4, to '/usr/share/nginx/html/uploads/testfile01_480.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf56.36.100 Stream #0:0(und): Video: h264, none, q=2-31, 128 kb/s, SAR 2560:2559 DAR 0:0 , 25 fps (default) Metadata: handler_name : VideoHandler encoder : Lavc56.41.100 libx264 Stream mapping: Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264)) Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
console output 2
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/usr/share/nginx/html/uploads/testfile.mp4': Metadata: major_brand : dash minor_version : 0 compatible_brands: iso6avc1mp41 creation_time : 2016-01-24 04:26:37 Duration: 01:15:58.08, start: 0.000000, bitrate: 3163 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 3161 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc (default) Metadata: creation_time : 2016-01-24 04:26:37 handler_name : VideoHandler [segment @ 0x1197060] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers Output #0, segment, to '/usr/share/nginx/html/uploads/testfile%02d.mp4': Metadata: major_brand : dash minor_version : 0 compatible_brands: iso6avc1mp41 encoder : Lavf56.36.100 Stream #0:0(und): Video: h264 (avc1 / 0x31637661), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 3161 kb/s, 25 fps, 25 tbr, 12800 tbn, 25 tbc (default) Metadata: creation_time : 2016-01-24 04:26:37 handler_name : VideoHandler Stream mapping: Stream #0:0 -> #0:0 (copy) Press [q] to stop, [?] for help [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers [mp4 @ 0x11512a0] Codec for stream 0 does not use global headers but container format requires global headers
-
How to determine the correct format when remuxing streams ?
1er février 2016, par NyarukoI used ffmpeg(libavcodec) to remuxing a incoming h264 stream. The following is the part where I initialize the output format context:
int ret; ffmpeg::AVDictionary *opts2 = NULL; av_dict_set(&opts2, "preset", "medium", 0); av_dict_set(&opts2, "crf", "29", 0); av_dict_set(&opts2, "profile", "baseline", 0); av_dict_set(&opts2, "level", "30", 0); av_dict_set(&opts2, "maxrate", "200000", 0); av_dict_set(&opts2, "minrate", "0", 0); av_dict_set(&opts2, "bufsize", "2000000", 0); ffmpeg::AVOutputFormat* fmt = ffmpeg::av_guess_format("mpeg", NULL, NULL); // Open the context //--------------------------------------------------------------------- outFormatCtx = ffmpeg::avformat_alloc_context(); if (!outFormatCtx) { return false; } //Set the output format //---------------------------------------------------------------------- outFormatCtx->oformat = fmt; // Open the output file //------------------------------------- if (!(outFormatCtx->flags & AVFMT_NOFILE)) { ret = ffmpeg::avio_open2(&outFormatCtx->pb, "Record.avi", AVIO_FLAG_WRITE, NULL, NULL); if (ret < 0) { return false; } } // Create the output stream // ------------------------------------- ffmpeg::AVStream* out_stream = ffmpeg::avformat_new_stream(outFormatCtx, inputStream->codec->codec); if (!out_stream) { return false; } //Set the stream parameters //------------------------------------- ret = ffmpeg::avcodec_copy_context(out_stream->codec, inputStream->codec); if (ret < 0) { return false; } // Check then setup for global headers //------------------------------------- if (outFormatCtx->oformat->flags & AVFMT_GLOBALHEADER) out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER; av_dump_format(outFormatCtx, 0, "Record.avi", 1); //Write the header //-------------------------------------- ret = ffmpeg::avformat_write_header(outFormatCtx, &opts2); if (ret < 0) { return false; }
Then it says I am not setting the VBV buffer size, and remuxing might fail. And the results actually give black screen.
How could I choose the correct format instead of guessing it? Or How could I set the VBV buffer size?
-
How to optimize YUV to RGB color conversion code
1er février 2016, par user_12I have written a function to convert an image in YUV420P to RGB but it is taking 30 millisecond to convert an image (size: 1280 x 720) into RGB, but when I am using ffmpeg function ( as this) to convert YUV image into RGB its taking only 2 millisecond for the same image. What is the problem with my code ? How can I optimize the code that I have written ?? My code is given below
int step = origImage->widthStep; uchar *data = (uchar *)origImage->imageData; int size = origImage->width * origImage->height; IplImage* img1 = cvCreateImage(cvGetSize(origImage), IPL_DEPTH_8U, 3); for (int i = 0; iheight; i++) { for (int j=0; jwidth; j++) { float Y = data[i*step + j]; float U = data[ (int)(size + (i/2)*(step/2) + j/2) ]; float V = data[ (int)(size*1.25 + (i/2)*(step/2) + j/2)]; float R = Y + 1.402 * (V - 128); float G = Y - 0.344 * (U - 128) - 0.714 * (V - 128); float B = Y + 1.772 * (U - 128); if (R < 0){ R = 0; } if (G < 0){ G = 0; } if (B < 0){ B = 0; } if (R > 255 ){ R = 255; } if (G > 255) { G = 255; } if (B > 255) { B = 255; } cvSet2D(img1, i, j,cvScalar(B,G,R)); } }
-
Making changes on a video before a download [on hold]
1er février 2016, par Chris RussoWe have a website with lots of videos (>10,000) and lots of users (>50,000). Each video might be around 1,2 Gbs.
The goal is to generate a unique video for each one of the members or users of the website making a small variation either on the audio track of the video, or the keyframes on the video itself, using low frequency beeps, or a few random pixels on specific keyframes.
I understand that this operation can be done using SoX for the audio track, or FFmpeg in the case of the video, manipulating the keyframes.
Here's my question, as we have to choose between 2 options at this point:
In terms of resources: would it be convenient or even possible to generate in real time a clone of the video and make this variations at the moment that the user's request the download (please considerate the big volume of videos and users).
Or it would be convenient to face this problem using HDD, pre-generating and storing the already manipulated videos ready to be served?
I understand that storing all this data will require a few many Tera's but I'm not sure about how much RAM and Microprocessing could be demanded to perform this operations in real time, over videos that might be as big as 1Gbs.