Newest 'ffmpeg' Questions - Stack Overflow
Les articles publiés sur le site
-
BGR0 -> VAAPI fmt -> VAAPI h264 encode - with BGR0 file input gives junk encoded data
4 avril 2019, par rgagI am writing a small c application using ffmpeg libraries to take a raw BGR0 file input and use VA apis to convert BGR0 to VAAPI pix_fmt which is directly fed to H264 encoder in GPU. The entire pipeline is working but the encoded frames are junk. Please go through the code and suggest if any changes needs to be done to the code. I suspect something related to setting of input and output frame, but I am not able to figure it out.
Summary of the code is
Create filters buffersrc and buffersink. Create vf_scale_vaapi filter and connect all filters as buffersrc->vf_scale_vaapi->buffersink. Input to buffersrc is BGR0 frame and output of buffersink is VAAPI frame. This VAAPI frame is fed to h264 encoder in gpu for encoding.
const char *filter_descr = "hwupload,scale_vaapi=format=nv12"; avfilter_register_all(); av_log_set_level(48); char args[512]; AVFilter *buffersrc = avfilter_get_by_name("buffer"); AVFilter *buffersink = avfilter_get_by_name("buffersink"); if ( buffersink == NULL ) printf("getting nul as sfilter name\n"); AVFilterInOut *outputs = avfilter_inout_alloc(); AVFilterInOut *inputs = avfilter_inout_alloc(); enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_VAAPI, AV_PIX_FMT_NONE }; AVBufferSinkParams *buffersink_params; filter_graph = avfilter_graph_alloc(); /* buffer video source: the decoded frames from the decoder will be inserted here. */ snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", in_width,in_height,AV_PIX_FMT_BGR0, 1, 25,1,1); ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in", args, NULL, filter_graph); if (ret < 0) { printf("Cannot create buffer source\n"); return ret; } /* buffer video sink: to terminate the filter chain. */ buffersink_params = av_buffersink_params_alloc(); buffersink_params->pixel_fmts = pix_fmts; ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out", NULL, buffersink_params, filter_graph); av_free(buffersink_params); if (ret < 0) { printf("Cannot create buffer sink\n"); return ret; } /* Endpoints for the filter graph. */ outputs->name = av_strdup("in"); outputs->filter_ctx = buffersrc_ctx; outputs->pad_idx = 0; outputs->next = NULL; inputs->name = av_strdup("out"); inputs->filter_ctx = buffersink_ctx; inputs->pad_idx = 0; inputs->next = NULL; type = av_hwdevice_find_type_by_name("vaapi"); printf("did not find the hw device type\n"); ret = -1; } ret = av_hwdevice_ctx_create(&device_ref, AV_HWDEVICE_TYPE_VAAPI, "/dev/dri/renderD128", NULL, 0); if ( ret < 0 ) printf("could not create tghe hw devicew nctx\n"); if (!(codec = avcodec_find_encoder_by_name(enc_name))) { fprintf(stderr, "Could not find encoder.\n"); err = -1; goto fail; } if (!(avctx = avcodec_alloc_context3(codec))) { err = AVERROR(ENOMEM); goto fail; } avctx->width = width; avctx->height = height; avctx->time_base = (AVRational){1, 25}; avctx->framerate = (AVRational){25, 1}; avctx->sample_aspect_ratio = (AVRational){1, 1}; avctx->pix_fmt = AV_PIX_FMT_VAAPI; avctx->profile = FF_PROFILE_H264_HIGH; hw_device_ctx = av_buffer_ref(device_ref); if (!hw_device_ctx){ printf("could not ref hw device buf\n"); ret= AVERROR(ENOMEM); } AVBufferRef *device = hw_device_ctx; for (i = 0; i < filter_graph->nb_filters; i++) { filter_graph->filters[i]->hw_device_ctx = av_buffer_ref(device); if (!filter_graph->filters[i]->hw_device_ctx) { ret = AVERROR(ENOMEM); goto fail; } } //========== BLOCK of CODE which I feel needs help=================== if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_descr, &inputs, &outputs, NULL)) < 0) return ret; for (i = 0; i < filter_graph->nb_filters; i++) { filter_graph->filters[i]->hw_device_ctx = av_buffer_ref(device); if (!filter_graph->filters[i]->hw_device_ctx) { ret = AVERROR(ENOMEM); goto fail; } } frame_out=av_frame_alloc(); frame_buffer_out=(unsigned char *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_VAAPI, in_width,in_height,1)); av_image_fill_arrays(frame_out->data, frame_out->linesize,frame_buffer_out, AV_PIX_FMT_VAAPI,in_width, in_height,1); frame_in->width=in_width; frame_in->height=in_height; frame_in->format=AV_PIX_FMT_BGR0; avctx->hw_frames_ctx = av_buffer_ref(av_buffersink_get_hw_frames_ctx(buffersink_ctx)); if ( !avctx->hw_frames_ctx){ fprintf(stderr, "Failed to set hwframe context.\n"); goto fail; } if ((err = avcodec_open2(avctx, codec, NULL)) < 0) { fprintf(stderr, "Cannot open video encoder codec. Error code: %s\n", av_err2str(err)); goto fail; } //===================================================================== i = 0; while (1) { if(fread(frame_buffer_in, 1, in_width*in_height*4, fp_in)!= in_width*in_height*4){ //bgr0 read break; } //input BGR 0 frame_in->data[0]=frame_buffer_in; frame_in->pts = (1.0 / 30) * 90 * i; i++; if (av_buffersrc_add_frame(buffersrc_ctx, frame_in) < 0) { printf( "Error while add frame.\n"); break; } /* pull filtered pictures from the filtergraph */ ret = av_buffersink_get_frame(buffersink_ctx, frame_out); if (ret < 0){ printf("unable to get frma e from buffer sink\n"); break; } // Make sure Closed Captions will not be duplicated av_frame_remove_side_data(frame_out, AV_FRAME_DATA_A53_CC); int ret = 0; AVPacket enc_pkt; av_init_packet(&enc_pkt); enc_pkt.data = NULL; enc_pkt.size = 0; if ((ret = avcodec_send_frame(avctx, frame_out)) < 0) { fprintf(stderr, "Error code: %s\n", av_err2str(ret)); goto fail; } // Make sure Closed Captions will not be duplicated av_frame_remove_side_data(frame, AV_FRAME_DATA_A53_CC); while (1) { ret = avcodec_receive_packet(avctx, &enc_pkt); if (ret ) //< 0 && ret != AVERROR(EAGAIN)) break; enc_pkt.stream_index = 0; ret = fwrite(enc_pkt.data, enc_pkt.size, 1, fp_out); } av_packet_unref(&enc_pkt); }
-
EC2 for video-encoding
4 avril 2019, par Naftuli KayI have a potential job which will require me to do some video encoding with FFMPEG and x264. I'll have a series of files which I'll need to encode once, then I'll be able to bring down the instances. Since I'm not really sure of the resource utilization of x264 and FFMPEG, what kind of instances should I get? I'm thinking either a
High-CPU Extra Large Instance
7 GB of memory
20 EC2 Compute Units (8 virtual cores with 2.5 EC2 Compute Units each)
1690 GB of instance storage
64-bit platform
I/O Performance: High
API name: c1.xlargeor, alternatively a
Cluster GPU Quadruple Extra Large Instance
22 GB of memory
33.5 EC2 Compute Units (2 x Intel Xeon X5570, quad-core “Nehalem” architecture)
2 x NVIDIA Tesla “Fermi” M2050 GPUs
1690 GB of instance storage
64-bit platform
I/O Performance: Very High (10 Gigabit Ethernet)
API name: cg1.4xlargeWhat should I use? Does x264/FFMPEG perform better with faster/more CPUs or does it really pound the GPU more? In any case, it seems that the Cluster GPU seems to be the higher performance instance. What should I prefer?
-
Making a video (.mp4) using every Nth numbering plotted images (.png) with ffmpeg
3 avril 2019, par vanessaI have images file in format starting with number 10000 with every 500 step as shown here
"Qen_10000.png, Qen_10500.png, Qen_11000.png, Qen_11500.png..."
untilQen_80500.png
I want to combine them and make a video .mp4
I've tried
ffmpeg -r 5 -i Qen_%1d000.png video.mp4
and some other combination, but only every 10000 of the numbering.I also tried
ffmpeg -start_number 10000....
but it showed unrecognized option./usr/bin/ffmpeg -start_number 10000 -r 1 -i Qen_distribution_*.png video.mp4 FFmpeg version 0.6.5, Copyright (c) 2000-2010 the FFmpeg developers built on Jan 29 2012 23:55:02 with gcc 4.1.2 20080704 (Red Hat 4.1.2-51) configuration: --prefix=/usr --libdir=/usr/lib64 --shlibdir=/usr/lib64 --mandir=/usr/share/man --incdir=/usr/include --disable-avisynth --extra-cflags='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --enable-avfilter --enable-avfilter-lavf --enable-libdirac --enable-libfaac --enable-libfaad --enable-libfaadbin --enable-libgsm --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libx264 --enable-gpl --enable-nonfree --enable-postproc --enable-pthreads --enable-shared --enable-swscale --enable-vdpau --enable-version3 --enable-x11grab libavutil 50.15. 1 / 50.15. 1 libavcodec 52.72. 2 / 52.72. 2 libavformat 52.64. 2 / 52.64. 2 libavdevice 52. 2. 0 / 52. 2. 0 libavfilter 1.19. 0 / 1.19. 0 libswscale 0.11. 0 / 0.11. 0 libpostproc 51. 2. 0 / 51. 2. 0 Unrecognized option 'start_number'
Please suggest some options. Thank you.
-
need help on solving my ffmpeg command line
3 avril 2019, par DRMTVi create a small bash script to encode 1080p video , the video will be added with watermark at bottom left and i need to add a black padding on top and bottom
i tried several way but still no luck , i tried -vf and yes it worked but cant use padding and watermark together , and suggest use filter_complex
if i use this code directly without bash script it work
time ffmpeg -hide_banner -i transformers.mp4 -i transformers.ass -loop 1 -i watermark.png -loop 1 -i logo.png -map 0:0 -map 0:1 -filter_complex "[0:0]scale=(iw*sar)*min(1920/(iw*sar)\,800/ih):ih*min(1920/(iw*sar)\,800/ih), pad=1920:800:(1920-iw*min(1920/iw\,800/ih))/2:(800-ih*min(1920/iw\,800/ih))/2;ass=transformers.ass[FID1];[FID1][2:v]overlay=10:${WATERMARKPOSITION}:repeatlast=0:enable='between(t,300,600)'[FID3];[3:v]fade=in:st=1200:d=1.6:alpha=1,fade=out:st=107998:d=1.6:alpha=1[FID6];[FID3][FID6]overlay=10:5:repeatlast=0:enable='between(t,1200,187922)'" -c:v libx264 -minrate 1800k -maxrate 1800k -bufsize 3600k -profile:v high -c:a aac -b:a 128k -profile:a aac_main -movflags faststart -strict -2 -f mp4 -y "transformers.mp4"
but when i include it with my bash script i got this error ,
[libx264 @ 0x2a063e0] height not divisible by 2 (300x39) Output #0, mp4, to '/movie/Paddy/output/Transformers.Age.of.Extinction.2014.1080p.BluRay.H264.AAC-RARBG.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 comment : Transformers.Age.of.Extinction.2014.1080p.BluRay.H264.AAC-RARBG encoder : Lavf57.71.100 title : Transformers Age of Extinction 2014 1080p BluRay H264 AAC-RARBG - Visit us @ Juraganfilm.COM Stream #0:0: Video: h264 (libx264), yuv420p, 1920x800 [SAR 1:1 DAR 12:5], q=-1--1, max. 2300 kb/s, 23.98 fps, 23.98 tbn, 23.98 tbc Metadata: encoder : Lavc56.60.100 libx264 Stream #0:1: Video: h264, none, q=2-31, 128 kb/s, SAR 1:1 DAR 0:0, 25 fps Metadata: encoder : Lavc56.60.100 libx264 Stream #0:2(eng): Audio: aac, 0 channels, 128 kb/s (default) Metadata: creation_time : 2017-12-19 07:58:39 handler_name : SoundHandler encoder : Lavc56.60.100 aac Stream mapping: Stream #0:0 (h264) -> scale (graph 0) Stream #0:0 (h264) -> overlay:overlay (graph 0) Stream #2:0 (png) -> ass (graph 0) Stream #3:0 (png) -> fade (graph 0) pad (graph 0) -> Stream #0:0 (libx264) overlay (graph 0) -> Stream #0:1 (libx264) Stream #0:1 -> #0:2 (aac (native) -> aac (native)) Error while opening encoder for output stream #0:1 - maybe incorrect parameters such as bit_rate, rate, width or height
and below is the script i have been working on
output="/movie/output" FILESDIR=`find $PWD -type f -regex ".*\.\(mp4\|mkv\|avi\)" | sed 's@.*/@@' | sort -n` for video in $FILESDIR do MOVIETITLE=${video%.*} INFOVID=${MOVIETITLE//./ } BITRATE="${HEIGHT}" WIDTH=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 ${video} 2>&1 | sed -e 's|\[.*||g' | sed ':a;N;$!ba;s/\n//g' | sed -e 's|x.*||g') HEIGHT=$(ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 ${video} | sed -e 's/.*x//') WATERMARKPOSITION=$(expr $HEIGHT - 50) VIDEOMAP=$(ffmpeg -i $video 2>&1 | grep "Stream #" | grep Video | sed -e "s|.*\#||g" | sed -e "s|: Video.*||g" | sed -e "s|(.*||g") AUDIOMAP=$(ffmpeg -i $video 2>&1 | grep "Stream #" | grep Audio | sed -e "s|.*\#||g" | sed -e "s|: Audio.*||g" | sed -e "s|(.*||g") MAXRATE=$(expr $BITRATE + 500) BUFFSIZE=$(expr $MAXRATE \* 2) time ffmpeg -hide_banner -i $video -i $MOVIETITLE.ass -loop 1 -i $WATERMARK -loop 1 -i $LOGO -map ${VIDEOMAP} -map ${AUDIOMAP} -filter_complex "[${VIDEOMAP}]scale=(iw*sar)*min(${WIDTH}/(iw*sar)\,${HEIGHT}/ih):ih*min(${WIDTH}/(iw*sar)\,${HEIGHT}/ih), pad=${WIDTH}:${HEIGHT}:(${WIDTH}-iw*min(${WIDTH}/iw\,${HEIGHT}/ih))/2:(${HEIGHT}-ih*min(${WIDTH}/iw\,${HEIGHT}/ih))/2;ass=$MOVIETITLE.ass[FID1];[FID1][2:v]overlay=10:${WATERMARKPOSITION}:repeatlast=0:enable='between(t,300,600)'[FID3];[3:v]fade=in:st=1200:d=1.6:alpha=1,fade=out:st=107998:d=1.6:alpha=1[FID6];[FID3][FID6]overlay=10:5:repeatlast=0:enable='between(t,1200,187922)'" -c:v libx264 -minrate ${BITRATE}k -maxrate ${MAXRATE}k -bufsize ${BUFFSIZE}k -profile:v high -c:a aac -b:a 128k -profile:a aac_main -movflags faststart -strict -2 -f mp4 -y "${output}/$MOVIETITLE.mp4" done
has been working all day and still i cant make it to work.
can someone guide me which part is wrong ?
-
Stacking different length videos not working with ffmpeg and -itsoffset
3 avril 2019, par Lucas MadalozzoI developed a video conferencing app that records the video streams separately, and I am now looking for a way to merge them. At the moment I am experimenting with -itsoffset and hstack to stack 2 videos side by side using this command:
ffmpeg \ -itsoffset 17 -i smaller.mp4 \ -itsoffset 0 -i bigger.mp4 \ -filter_complex hstack=inputs=2 \ -c:v libx264 -crf 23 out.mp4
The result is a side by side video where both streams remain frozen for 17 seconds then start playing, even the bigger.mp4 video that should start at time 0.
Any help would be really appreciated!
ffmpeg verbose:
ffmpeg version 4.1 Copyright (c) 2000-2018 the FFmpeg developers built with gcc 4.9.2 (Debian 4.9.2-10+deb8u1) configuration: --enable-gpl --enable-postproc --enable-swscale --enable-avfilter --enable-libmp3lame --enable-libvorbis --enable-libtheora --enable-libx264 --enable-libspeex --enab le-shared --enable-pthreads --enable-libopenjpeg --enable-nonfree --enable-libopus --enable-libvorbis --enable-libvpx libavutil 56. 22.100 / 56. 22.100 libavcodec 58. 35.100 / 58. 35.100 libavformat 58. 20.100 / 58. 20.100 libavdevice 58. 5.100 / 58. 5.100 libavfilter 7. 40.101 / 7. 40.101 libswscale 5. 3.100 / 5. 3.100 libswresample 3. 3.100 / 3. 3.100 libpostproc 55. 3.100 / 55. 3.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'smaller.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.20.100 Duration: 00:00:05.16, start: 0.000000, bitrate: 444 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x360 [SAR 1:1 DAR 4:3], 330 kb/s, 32 fps, 32 tbr, 16384 tbn, 64 tbc (default) Metadata: handler_name : VideoHandler Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 103 kb/s (default) Metadata: handler_name : SoundHandler Input #1, mov,mp4,m4a,3gp,3g2,mj2, from 'bigger.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf56.36.100 Duration: 00:00:22.03, start: 0.000000, bitrate: 290 kb/s Stream #1:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 480x360 [SAR 1:1 DAR 4:3], 177 kb/s, 32 fps, 32 tbr, 16384 tbn, 64 tbc (default) Metadata: handler_name : VideoHandler Stream #1:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 103 kb/s (default) Metadata: handler_name : SoundHandler File 'out.mp4' already exists. Overwrite ? [y/N] y Stream mapping: Stream #0:0 (h264) -> hstack:input0 (graph 0) Stream #1:0 (h264) -> hstack:input1 (graph 0) hstack (graph 0) -> Stream #0:0 (libx264) Stream #0:1 -> #0:1 (aac (native) -> aac (native)) Press [q] to stop, [?] for help [libx264 @ 0x206ed00] using SAR=1/1 [libx264 @ 0x206ed00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2 [libx264 @ 0x206ed00] profile High, level 3.1 [libx264 @ 0x206ed00] 264 - core 146 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=3 lookahead_threads=1 sliced_thre ads=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=2 keyint=250 keyint_min=25 scene cut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00 Output #0, mp4, to 'out.mp4': Metadata: major_brand : isom minor_version : 512 compatible_brands: isomiso2avc1mp41 encoder : Lavf58.20.100 Stream #0:0: Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 960x360 [SAR 1:1 DAR 8:3], q=-1--1, 32 fps, 16384 tbn, 32 tbc (default) Metadata: encoder : Lavc58.35.100 libx264 Side data: cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1 Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default) Metadata: handler_name : SoundHandler encoder : Lavc58.35.100 aac frame= 709 fps=130 q=-1.0 Lsize= 573kB time=00:00:22.12 bitrate= 212.2kbits/s dup=544 drop=0 speed=4.05x video:478kB audio:81kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.445685% [libx264 @ 0x206ed00] frame I:3 Avg QP:16.86 size: 38355 [libx264 @ 0x206ed00] frame P:190 Avg QP:18.22 size: 1633 [libx264 @ 0x206ed00] frame B:516 Avg QP:16.23 size: 123 [libx264 @ 0x206ed00] consecutive B-frames: 1.3% 2.8% 6.8% 89.1% [libx264 @ 0x206ed00] mb I I16..4: 6.6% 54.8% 38.6% [libx264 @ 0x206ed00] mb P I16..4: 0.4% 1.5% 0.2% P16..4: 8.7% 4.0% 1.9% 0.0% 0.0% skip:83.3% [libx264 @ 0x206ed00] mb B I16..4: 0.0% 0.1% 0.0% B16..8: 4.2% 0.3% 0.0% direct: 0.0% skip:95.4% L0:37.2% L1:58.9% BI: 4.0% [libx264 @ 0x206ed00] 8x8 transform intra:66.2% inter:63.4% [libx264 @ 0x206ed00] coded y,uvDC,uvAC intra: 66.1% 65.6% 21.6% inter: 1.7% 1.1% 0.0% [libx264 @ 0x206ed00] i16 v,h,dc,p: 21% 26% 11% 42% [libx264 @ 0x206ed00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 25% 23% 13% 5% 7% 8% 7% 7% 6% [libx264 @ 0x206ed00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 28% 24% 9% 6% 7% 7% 6% 7% 7% [libx264 @ 0x206ed00] i8c dc,h,v,p: 46% 25% 20% 9% [libx264 @ 0x206ed00] Weighted P-Frames: Y:1.1% UV:0.0% [libx264 @ 0x206ed00] ref P L0: 66.0% 19.4% 12.0% 2.6% 0.0% [libx264 @ 0x206ed00] ref B L0: 87.2% 11.8% 1.0% [libx264 @ 0x206ed00] ref B L1: 95.2% 4.8% [libx264 @ 0x206ed00] kb/s:176.52 [aac @ 0x204aa00] Qavg: 247.398