Setting B frames in a video with ffmpeg

roberutsu picture roberutsu · Apr 6, 2013 · Viewed 21.2k times · Source

According to ffmpeg manual, setting -g is to define space between "I" frames, and setting -bf to use "B" frames. The former I got, but the latter not.

The goal: I'm trying to have a video with a GOP 3,12 (M= 3, N=12). That means: 2 "B" frames separating each "P" frames, and "I" frames with 12 frames of distance. Or simply: "IBBPBBPBBPBBI"

I think that I got only the N=12, using the commands below:

  1. ffmpeg -s cif -r 30 -b 64000 -bt 3200 -g 12 -y -i video.yuv -vcodec mpeg4 video.m4v
  2. MP4Box -hint -mtu 1460 -fps 30 -add video.m4v video.mp4
  3. ffmpeg -y -i video.mp4 video_ref.yuv
  4. ../cmd/psnr 352 288 420 video.yuv video_ref.yuv > psnr_ref.txt
  5. ../cmd/mp4trace -f -s 192.168.0.2 12346 video.mp4 > trace
  6. head -n 20 trace

Result:

[robert@10-2Fontes]$ head -n 20 trace
1   H   12002   9   0.000
2   P   11479   8   0.034
3   P   12021   9   0.066
4   P   11239   8   0.099
5   P   5407    4   0.134
6   P   2735    2   0.166
7   P   1014    1   0.199
8   P   850 1   0.232
9   P   619 1   0.265
10  P   979 1   0.298
11  P   813 1   0.331
12  P   806 1   0.364
13  H   5109    4   0.396

*Note, the most important is the command -g 12 in ffmpeg, but I writing all the commands. The video that I'm using is the "highway", from cif page videos: http://www2.tkn.tu-berlin.de/research/evalvid/cif.html

I don't know why the result trace is using "H" instead of "I".

I tried to put -bf 2 in ffmpeg command, but did not worked (I think because I saw no "B" indications in the result)

List item

ffmpeg -s cif -r 30 -b 64000 -bt 3200 -g 12 -bf 2 -y -i video.yuv -vcodec mpeg4 video.m4v

Result:

[robert@10-2Fontes]$ head -n 20 trace
1   H   12002   9   0.001
2   P   11479   8   0.034
3   P   12021   9   0.067
4   P   11239   8   0.100
5   P   5407    4   0.132
6   P   2735    2   0.166
7   P   1014    1   0.199
8   P   850 1   0.232
9   P   619 1   0.265
10  P   979 1   0.298
11  P   813 1   0.331
12  P   806 1   0.363
13  H   5109    4   0.400

Answer

roberutsu picture roberutsu · Apr 8, 2013

Solved:

Like LordNeckbeard said, option placement matters in ffmpeg.

Basic order is:

ffmpeg [global options] [input options] -i input [output options] output

I changed to the command below (putting -g 30 -bf 2) and it works:

ffmpeg -s cif -r 30 -i video.yuv -vcodec mpeg4 -g 30 -bf 2 video.m4v

Note: Even after changed in the first time did not worked. The program asked me to overwrite the files and I said Yes. But apparently sth was not removed, at the moment I removed all files and did it again (from zero) it worked.

Now the result is:

[robert@10-2Fontes]$ head -n 40 trace
1   H   12038   9   0.034
2   P   13204   10  0.132
3   B   5367    4   0.132
4   B   5553    4   0.132
5   P   12157   9   0.232
6   B   5069    4   0.232
7   B   5613    4   0.232
8   P   12739   9   0.334
9   B   5935    5   0.334
10  B   3921    3   0.334
11  P   4474    4   0.429
12  B   1247    1   0.429
13  B   784 1   0.429
14  P   1448    1   0.528
15  B   350 1   0.528
16  B   397 1   0.528
17  P   1023    1   0.627
18  B   299 1   0.633
19  B   265 1   0.634
20  P   829 1   0.727
21  B   209 1   0.733
22  B   340 1   0.733
23  P   867 1   0.826
24  B   343 1   0.833
25  B   378 1   0.833
26  P   865 1   0.925
27  B   282 1   0.925
28  B   461 1   0.925
29  H   5083    4   1.034
30  B   818 1   1.034
31  B   838 1   1.034
32  P   1171    1   1.122
33  B   443 1   1.133
34  B   409 1   1.133
35  P   1078    1   1.221
36  B   269 1   1.233
37  B   327 1   1.233
38  P   795 1   1.321
39  B   298 1   1.333
40  B   304 1   1.334
41  P   854 1   1.419
42  B   477 1   1.419
43  B   412 1   1.419
44  P   869 1   1.519
45  B   371 1   1.519
46  B   314 1   1.519
47  P   983 1   1.617
48  B   337 1   1.617
49  B   454 1   1.617
50  P   1118    1   1.717
51  B   286 1   1.717
52  B   275 1   1.717
53  P   1044    1   1.815
54  B   362 1   1.815
55  B   273 1   1.815
56  P   973 1   1.914
57  B   302 1   1.914
58  B   324 1   1.915
59  H   4525    4   2.033