Recently I made an Audio Visualizer in Processing. From there I wanted to render the animation created in Processing into a mp4 file. I am on a windows computer, and am using ffmpeg to convert my TIF files produced in Processing into mp4.
When I do this I am able to render the images into an mp4 file, but when I playback this file the animation is sped up compared to the animation when I play it on Processing. Because of this the animation does not sync with the audio when I combine the mp4 file and audio on a video editing program.
When I set my frame rate to 25 and have the limit on the number of frames to be 250 and render it into a mp4 file it is 10 seconds long like it should be, but it contains more than 10 seconds of the animation when compared to the animation played directly in Processing.
I have no idea why this is so any help will be much appreciated.
My Processing code:
import ddf.minim.*;
import ddf.minim.analysis.*;
Minim minim;
AudioPlayer player;
PImage img;
FFT fft;
void setup() {
size(728, 546);
minim = new Minim(this);
// this loads mysong.wav from the data folder as a stream with a internal buffer of size 1024
player = minim.loadFile("new_years_good.mp3");
fft = new FFT(player.bufferSize(), player.sampleRate());
player.play();
img= loadImage("cat-in-shades-.jpg");
frameRate(25);
}
void draw() {
image(img, 0, 0);
//tint(0, 100, 150);
stroke(255);
strokeWeight(4);
float a = 0;
float angle = (2*PI) / 200;
fft.forward(player.mix);
for(int i=0; i < player.bufferSize() - 1; i++) {
//player.mix.get(i) is a value between [-1,1]
float x = 250 + cos(a) * (20 * player.mix.get(i) + 100);
float x2 = 540 + cos(a) * (20 * player.mix.get(i) + 100);
float y = 230 + sin(a) * (20 * player.mix.get(i) + 100);
float y2 = 240 + sin(a) * (20 * player.mix.get(i) + 100);
float xFinal = 250 + cos(a+angle) * (20 * player.mix.get(i+1) + 100);
float x2Final = 540 + cos(a+angle) * (20 * player.mix.get(i+1) + 100);
float yFinal = 230 + sin(a+angle) * (20 * player.mix.get(i+1) + 100);
float y2Final = 240 + sin(a+angle) * (20 * player.mix.get(i+1) + 100);
line(x,y,xFinal,yFinal);
line(x2,y2,x2Final,y2Final);
a += angle;
}
noStroke();
fill(255, 0, 0, 128);
for(int i = 0; i < 250; i++)
{
float b = fft.getBand(i);
float yAxis = random(-b, b) + 480;
float xAxis = i*3;
ellipse(xAxis, yAxis, b, b);
}
saveFrame("frame-####.tif");
if(frameCount>250)
{
noLoop();
stop();
}
}
void stop() {
player.close();
minim.stop();
super.stop();
}
What I input into the command line (as one line) on the cmd:
C:\Users\Robert\Documents\Processing\AudioVisulizer>ffmpeg -i C:\Users\Robert\Do
cuments\Processing\AudioVisulizer\frame-%04d.tif -r 25 -pix_fmt yuv420p smallVid
.mp4
What it outputted:
ffmpeg version N-77836-g62dfe1d Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 5.2.0 (GCC)
configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --
enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-l
ibilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enab
le-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --en
able-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --ena
ble-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc
--enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enabl
e-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --
enable-lzma --enable-decklink --enable-zlib
libavutil 55. 13.100 / 55. 13.100
libavcodec 57. 22.100 / 57. 22.100
libavformat 57. 21.101 / 57. 21.101
libavdevice 57. 0.100 / 57. 0.100
libavfilter 6. 23.100 / 6. 23.100
libswscale 4. 0.100 / 4. 0.100
libswresample 2. 0.101 / 2. 0.101
libpostproc 54. 0.100 / 54. 0.100
Input #0, image2, from 'C:\Users\Robert\Documents\Processing\AudioVisulizer\fram
e-%04d.tif':
Duration: 00:00:10.04, start: 0.000000, bitrate: N/A
Stream #0:0: Video: tiff, rgb24, 728x546, 25 fps, 25 tbr, 25 tbn, 25 tbc
[libx264 # 00000092cd5e3700] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2
AVX FMA3 AVX2 LZCNT BMI2
[libx264 # 00000092cd5e3700] profile High, level 3.0
[libx264 # 00000092cd5e3700] 264 - core 148 r2638 7599210 - H.264/MPEG-4 AVC cod
ec - Copyleft 2003-2015 - http://www.videolan.org/x264.html - options: cabac=1 r
ef=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_pski
p=1 chroma_qp_offset=-2 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 deci
mate=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=2
5 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.6
0 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'smallVid.mp4':
Metadata:
encoder : Lavf57.21.101
Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 728x54
6, q=-1--1, 25 fps, 12800 tbn, 25 tbc
Metadata:
encoder : Lavc57.22.100 libx264
Side data:
unknown side data type 10 (24 bytes)
Stream mapping:
Stream #0:0 -> #0:0 (tiff (native) -> h264 (libx264))
Press [q] to stop, [?] for help
frame= 52 fps=0.0 q=28.0 size= 77kB time=00:00:00.00 bitrate=N/A speed=
frame= 74 fps= 65 q=28.0 size= 127kB time=00:00:00.88 bitrate=1178.0kbits/
frame= 93 fps= 57 q=28.0 size= 164kB time=00:00:01.64 bitrate= 820.0kbits/
frame= 113 fps= 52 q=28.0 size= 201kB time=00:00:02.44 bitrate= 676.3kbits/
frame= 136 fps= 51 q=28.0 size= 245kB time=00:00:03.36 bitrate= 596.3kbits/
frame= 157 fps= 49 q=28.0 size= 282kB time=00:00:04.20 bitrate= 550.2kbits/
frame= 178 fps= 48 q=28.0 size= 324kB time=00:00:05.04 bitrate= 527.2kbits/
frame= 199 fps= 47 q=28.0 size= 362kB time=00:00:05.88 bitrate= 504.1kbits/
frame= 219 fps= 46 q=28.0 size= 403kB time=00:00:06.68 bitrate= 494.2kbits/
frame= 242 fps= 46 q=28.0 size= 452kB time=00:00:07.60 bitrate= 486.8kbits/
frame= 251 fps= 38 q=-1.0 Lsize= 623kB time=00:00:09.96 bitrate= 512.3kbits
/s speed=1.52x
video:619kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing o
verhead: 0.607807%
[libx264 # 00000092cd5e3700] frame I:2 Avg QP:21.74 size: 56596
[libx264 # 00000092cd5e3700] frame P:66 Avg QP:23.36 size: 2523
[libx264 # 00000092cd5e3700] frame B:183 Avg QP:31.50 size: 1932
[libx264 # 00000092cd5e3700] consecutive B-frames: 0.8% 4.0% 6.0% 89.2%
[libx264 # 00000092cd5e3700] mb I I16..4: 10.9% 72.6% 16.5%
[libx264 # 00000092cd5e3700] mb P I16..4: 0.0% 0.0% 0.2% P16..4: 4.4% 2.3
% 3.3% 0.0% 0.0% skip:89.7%
[libx264 # 00000092cd5e3700] mb B I16..4: 0.0% 0.0% 0.5% B16..8: 3.2% 2.0
% 2.3% direct: 1.3% skip:90.7% L0:50.9% L1:42.2% BI: 7.0%
[libx264 # 00000092cd5e3700] 8x8 transform intra:50.2% inter:10.1%
[libx264 # 00000092cd5e3700] coded y,uvDC,uvAC intra: 83.4% 32.6% 14.2% inter: 3
.5% 0.2% 0.0%
[libx264 # 00000092cd5e3700] i16 v,h,dc,p: 22% 8% 8% 62%
[libx264 # 00000092cd5e3700] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 19% 12% 19% 6% 9%
9% 9% 9% 9%
[libx264 # 00000092cd5e3700] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 22% 14% 22% 6% 9%
8% 8% 5% 6%
[libx264 # 00000092cd5e3700] i8c dc,h,v,p: 74% 12% 12% 2%
[libx264 # 00000092cd5e3700] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 # 00000092cd5e3700] ref P L0: 41.8% 3.9% 24.3% 30.0%
[libx264 # 00000092cd5e3700] ref B L0: 64.5% 25.0% 10.5%
[libx264 # 00000092cd5e3700] ref B L1: 82.2% 17.8%
[libx264 # 00000092cd5e3700] kb/s:504.56
The fact that you managed to get the sound and the video synchronized in the running program probably means you run it on a free CPU
If music and video are two separate threads (as I understand it) its not guaranteed that the CPU will run them always so.
So if you want to have your final video .mp4 synchronized you have to work outside the program: take them together to a video editing software and synchronize them.
But if you want them synchronized in the program you are fine.
Related
The first function isCycle is getting parameters from driver code of website. I have worked for more than 2 hours in order to debug why its not working in my logic. Anything i need to improve here.
This is the link of question.
https://practice.geeksforgeeks.org/problems/detect-cycle-in-an-undirected-graph/1/#
public boolean isCycle(int V, ArrayList<ArrayList<Integer>> adj) {
if(V<=2){
return false;
}
Map<Integer, ArrayList<Integer> > graph = new TreeMap<>();
for(int i = 0; i<V; i++){
graph.put(i,adj.get(i) );
}
return detectCycle(graph);
}
static boolean detectCycle(Map<Integer, ArrayList<Integer> > graph){
Map<Integer, Integer> parentChild = new TreeMap<>();
Map<Integer, Boolean> isVisited = new TreeMap<>();
Queue<Integer> q = new LinkedList<>();
q.add(0);
isVisited.put(0,true);
parentChild.put(0,-1);
while(!q.isEmpty() ){
int curNode = q.remove();
if(graph.get(curNode) == null){
q.add(++curNode);
continue;
}
ArrayList<Integer> curElements = graph.get(curNode);
if(!isVisited.containsKey(curNode)){
q.add(curNode);
isVisited.put(0,true);
parentChild.put(0,0);
}
for(int temp: curElements){
if(!isVisited.containsKey(temp)){
isVisited.put(temp, true);
parentChild.put(temp,curNode);
q.add(temp);
}
else{
if((parentChild.get(temp) != curNode) && (isVisited.get(temp) == true)){
return true;
}
}
}
}
return false;
}
In this test case it failed. Short testcases are passing actually.
1805 829
1 321
1 499
1 694
1 1360
2 115
2 881
3 1592
4 846
5 1085
6 1654
7 691
7 872
7 1172
7 1243
7 1721
9 844
9 984
12 724
13 353
13 895
13 1107
15 1514
20 1679
22 329
22 759
23 124
23 1003
24 1239
30 1032
31 1004
31 1342
31 1641
33 466
34 393
35 518
38 1666
39 831
40 362
41 1615
42 1006
43 886
45 428
46 1084
47 178
48 1177
49 1564
51 1283
52 1042
54 593
55 1456
57 1460
58 806
64 521
64 741
65 86
65 1325
66 759
66 1773
67 704
67 769
67 1541
69 244
70 896
70 1470
72 263
72 1460
73 1013
74 465
74 496
75 733
77 218
77 436
77 1105
79 1664
82 221
82 577
82 714
84 485
84 1768
85 1313
87 166
88 510
88 1266
90 139
92 679
92 1196
93 441
93 1157
94 1417
96 441
96 1716
97 768
97 1578
100 1047
102 1508
105 724
106 1356
106 1756
110 801
114 862
114 1188
115 892
115 1787
116 782
117 1178
118 572
118 1593
120 1156
121 128
123 494
127 456
127 1591
129 659
131 444
133 1352
134 146
134 1319
135 1049
136 735
137 349
137 1799
138 1042
139 375
139 887
140 170
142 453
142 1703
143 911
144 976
145 1415
146 1548
148 1581
149 396
149 546
149 1602
150 1548
150 1703
151 828
151 1404
153 406
158 513
160 187
161 292
164 1009
164 1465
165 786
166 817
168 1493
169 1320
170 211
170 615
174 1798
175 1300
176 1079
177 599
177 715
177 747
178 999
179 691
179 869
179 870
180 883
181 1318
183 233
186 1323
188 837
188 1089
189 720
189 1799
192 891
192 1514
195 913
198 1604
199 926
201 1004
201 1239
203 1109
204 841
204 1088
206 1110
207 1523
209 584
209 1649
210 738
211 773
212 915
212 1060
214 623
214 1313
215 318
215 1082
217 303
218 241
221 1247
223 1740
225 1351
226 353
229 1298
232 1558
233 1287
234 1309
236 624
238 1539
239 911
241 1145
242 787
243 991
244 1234
245 1195
246 657
246 1113
247 1339
248 342
249 454
252 1781
253 619
254 1354
254 1531
255 1508
258 1617
261 419
261 1728
262 530
262 1678
263 562
263 984
263 1288
263 1631
263 1683
265 738
265 982
265 1454
266 1613
271 828
276 1486
279 1719
280 549
280 1259
283 1722
284 1117
289 1301
290 419
291 494
291 1180
291 1738
292 527
294 686
294 1667
294 1702
294 1752
296 1318
300 1597
303 356
305 425
305 1071
305 1726
306 780
307 1098
307 1236
308 469
308 662
311 1520
313 1386
315 1374
323 888
326 1062
330 839
330 1586
336 415
337 645
338 1606
340 1443
341 1572
343 1380
344 569
344 1062
345 1227
346 1026
346 1066
347 1685
348 1404
349 1470
351 407
353 780
355 748
357 1532
358 1610
359 551
359 1674
360 1013
361 1443
362 726
362 768
362 1430
366 509
366 1617
370 1318
370 1334
377 1582
378 933
380 556
383 1553
385 470
385 1493
389 986
389 1738
390 1071
391 1268
392 941
393 1793
394 1504
394 1537
398 1022
398 1089
399 1540
400 1124
400 1619
401 1086
404 1585
410 491
410 561
410 856
413 1651
417 1260
418 1425
418 1723
420 1595
421 1063
423 559
423 1383
424 1572
425 861
425 1775
427 1584
428 1672
432 708
435 783
435 1037
435 1576
436 856
436 1022
437 1731
442 635
443 835
444 629
445 1135
446 743
446 836
448 918
450 1582
454 1136
455 767
458 1769
459 928
459 1448
462 1745
463 1161
466 789
466 1144
468 1799
470 971
472 565
472 709
472 810
474 542
474 854
477 654
479 848
482 1385
484 642
485 1207
489 1476
489 1788
490 1569
494 1087
495 617
498 1594
500 571
501 1747
506 720
506 1069
507 912
507 962
508 921
508 1343
509 1143
511 1291
514 1557
515 849
517 554
518 1141
520 1022
520 1354
520 1607
522 907
523 902
527 972
527 1170
527 1254
528 1051
531 606
531 860
535 1177
536 957
539 913
541 698
541 1213
543 1782
544 1242
544 1429
544 1575
547 789
548 1086
550 560
554 1759
555 801
557 1706
559 1551
561 1163
569 1167
571 859
571 1336
572 1135
573 1704
576 990
579 847
579 1684
580 1153
580 1794
581 1123
581 1201
582 1226
586 1315
588 1135
589 1414
594 1084
594 1284
595 1766
598 1650
602 1715
604 1168
605 1337
607 1429
610 816
610 1263
616 1435
619 1281
620 1180
624 704
625 860
625 1242
626 959
626 1171
627 1425
629 1382
630 878
630 1211
631 1494
633 916
638 927
638 975
638 1773
639 842
640 695
640 787
642 802
644 1333
645 900
650 890
653 1724
654 890
654 1214
655 1226
655 1712
657 1388
658 1208
659 840
659 1519
663 892
664 1523
668 1336
672 1062
675 745
677 1561
678 1060
680 1255
681 1475
681 1655
682 708
682 781
683 1486
687 1609
690 1618
690 1679
691 903
693 1084
695 719
696 1310
696 1357
699 908
705 758
714 1745
718 782
718 978
719 1682
723 978
723 1762
724 1454
731 1017
732 1393
736 1730
739 1764
740 1674
742 1618
748 1068
748 1071
748 1202
748 1569
749 853
749 1427
751 1625
754 1506
755 1615
757 1663
759 1132
760 789
760 1024
760 1575
760 1771
764 819
766 1021
766 1369
767 1424
770 1751
773 1363
773 1402
775 1372
777 1515
779 1519
780 1702
781 1170
782 1137
782 1353
785 1058
786 983
786 1080
787 1721
789 929
792 1392
792 1784
793 1246
795 1782
799 937
801 959
804 1480
805 1776
809 941
812 1275
813 1554
814 1388
817 904
817 1290
817 1345
818 1044
819 1014
820 1346
821 1607
822 1093
831 1209
835 1555
836 1138
844 1004
846 1223
852 974
852 1177
853 1121
858 870
860 1079
863 1261
867 1442
873 1763
876 1704
877 1301
877 1501
877 1643
883 1342
884 1745
889 1723
890 1376
894 1016
896 1132
896 1340
901 1628
903 1586
906 1388
910 1057
910 1082
912 1206
912 1413
912 1536
917 1670
918 1295
919 1105
925 1110
926 1716
927 1442
928 1335
931 1751
934 1337
939 1573
941 1559
948 1646
950 1314
951 1504
953 1675
954 1563
955 1522
955 1648
956 1229
956 1309
960 1232
962 1406
962 1672
964 998
964 1420
965 1661
967 1650
969 1771
972 1494
972 1744
973 1544
975 1464
977 1055
977 1226
978 1377
978 1790
979 1326
979 1545
980 1450
981 1157
982 1605
983 1130
986 1292
986 1769
987 1692
990 1788
994 1460
1000 1337
1003 1549
1008 1528
1009 1277
1009 1465
1013 1301
1013 1512
1013 1671
1014 1094
1017 1572
1025 1507
1025 1666
1032 1613
1033 1590
1035 1213
1037 1304
1039 1139
1040 1349
1044 1460
1046 1497
1058 1601
1067 1178
1067 1403
1069 1724
1070 1722
1071 1299
1072 1299
1073 1132
1079 1602
1081 1281
1082 1211
1088 1359
1091 1171
1092 1745
1107 1441
1109 1248
1109 1524
1109 1574
1113 1157
1116 1611
1116 1671
1117 1194
1119 1340
1120 1507
1120 1511
1124 1238
1127 1152
1127 1450
1129 1722
1133 1409
1133 1707
1135 1242
1135 1530
1137 1503
1139 1672
1142 1716
1143 1219
1144 1246
1149 1654
1153 1599
1156 1533
1157 1584
1174 1197
1174 1676
1175 1555
1176 1396
1176 1797
1182 1476
1185 1736
1192 1589
1198 1791
1199 1732
1206 1561
1206 1765
1218 1242
1219 1695
1220 1722
1223 1314
1224 1484
1235 1245
1237 1547
1239 1727
1239 1769
1251 1431
1251 1453
1253 1459
1255 1415
1255 1474
1255 1785
1256 1794
1258 1365
1260 1700
1265 1535
1271 1441
1281 1544
1291 1732
1302 1379
1308 1326
1308 1773
1313 1431
1318 1414
1323 1657
1324 1730
1333 1597
1337 1411
1337 1804
1343 1575
1345 1508
1353 1753
1356 1711
1358 1750
1359 1523
1361 1796
1368 1539
1369 1514
1378 1400
1382 1755
1383 1550
1389 1464
1391 1520
1398 1702
1399 1550
1404 1507
1410 1778
1412 1764
1417 1441
1421 1608
1422 1439
1427 1511
1431 1610
1438 1509
1441 1461
1442 1506
1445 1626
1445 1649
1446 1507
1446 1622
1450 1660
1453 1756
1459 1603
1462 1546
1463 1646
1463 1755
1475 1799
1478 1595
1484 1629
1488 1702
1493 1762
1494 1656
1497 1629
1497 1804
1520 1724
1523 1666
1528 1790
1529 1681
1530 1614
1533 1735
1549 1621
1550 1589
1553 1672
1556 1688
1569 1698
1570 1621
1570 1747
1582 1629
1584 1622
1600 1657
1611 1699
1621 1768
1632 1794
1634 1648
1640 1644
1653 1792
1656 1765
1671 1672
1684 1735
1686 1783
1711 1803
1728 1756
1738 1777
1761 1766
1772 1780
Well, assuming our goal is not to find optimal solution, but to solve problem using BFS...
Next code snippet:
Queue<Integer> q = new LinkedList<>();
q.add(0);
isVisited.put(0,true);
parentChild.put(0,-1);
suffers from the following issue: in case of BFS we need to traverse graph in very specific order, basically we need to find some concept of level and always traverse from one level to another. Think why you code fails following simple test cases (yep, both geeksforgeeks and hackerrank failed to provide reliable test cases):
4, {{}, {2,3}, {1,3}, {1,2}} - returns false, expected true
4, {{3}, {3}, {3}, {0,1,2}} - returns true, expected false
One BFS idea: if our graph has a cycle, disconnecting leaf nodes (removing corresponding edges and splitting single graph into two) does not affect that cycle, so if we iteratively disconnected all leaf nodes (that is our level) and our graph still has edges that means we got a cycle.
public static boolean isCycle(int V, List<List<Integer>> adj) {
// queue contains leaf nodes only
Set<Integer> queue = new HashSet<>();
List<Set<Integer>> graph = new ArrayList<>();
// preparing input data
for (int node = 0; node < V; node++) {
List<Integer> input = adj.get(node);
Set<Integer> neighbours = input != null ? new HashSet<>(input) : new HashSet<>();
neighbours.remove(node);
graph.add(node, neighbours);
if (neighbours.size() == 1) {
// leaf node found, adding to queue
queue.add(node);
}
}
while (!queue.isEmpty()) {
// next BFS level
Set<Integer> level = new HashSet<>();
for (int node : queue) {
for (int neighbour : graph.get(node)) {
// removing edge neighbour->node
Set<Integer> next = graph.get(neighbour);
next.remove(node);
if (next.size() == 1) {
// got another one leaf node among neighbours
level.add(neighbour);
}
}
// removing edges node->neighbours
graph.set(node, new HashSet<>());
}
queue = level;
}
return graph.stream().anyMatch(set -> set.size() > 0);
}
However, if your BFS idea was to traverse the single graph level by level, the code could be (I see no difference with DFS though):
public static boolean isCycle(int V, List<List<Integer>> adj) {
boolean[] seen = new boolean[V];
for (int i = 0; i < V; i++) {
if (seen[i]) {
continue;
}
Deque<int[]> q = new LinkedList<>();
// storing (node, parent)
q.add(new int[]{i, -1});
seen[i] = true;
while (!q.isEmpty()) {
int[] node = q.poll();
for (int next : adj.get(node[0])) {
// seen node on the previous level
if (next == node[1]) {
continue;
}
if (seen[next]) {
return true;
}
q.add(new int[]{next, node[0]});
seen[next] = true;
}
}
}
return false;
}
Yeah, I know this is repeated question. But still I need to understand a lot about horizontal parsing. I am expecting full clear answer here.
I have some example content stream like below:
Example1:
BT
/F33 20.665 Tf
72 633.8289 Td
[(Chapter)-375(12)]TJ
/F33 24.78709 Tf
0 51.30099 Td
[(P)31(arametric)-375(and)-375(P)32(olar)-375(Curv)31(es)]TJ
Example2:
BT
/C0_1 14 Tf
39.812999 681.73999 Td
[(\000"\000M\000U\000I\000P\000V\000H\000I\000\001)-82(\000$\000B\000S\000P\000V\000T\000F\000M\000\0001)-82(\000X\000B\000T\000\001)-82.07099........]TJ
The formula for parsing horizontally(tx) is
Now I want to substitute values behalf of example1 :
W0 = ?(here mkl mentioned w0 means width of the respective character from the width array. How can I get the lengths. what are the different values for above 3 examples. How can I get from existing pdf. How can I get character width from CMAP's.)
Tj = The numbers in TJ array.
Tfs = use the font size from the graphics state which is the font size parameter from the relevant Tf operation, e.g. 10.
Tc = use the value from the graphics state which is the parameter from the relevant Tc or " operation.
Tw = use 0 or (in case of a single-byte character code 32) the value from the graphics state which is the parameter from the relevant Tw or " operation.
Th = use the value from the graphics state which is the parameter from the relevant Tz operation divided by 100.
Please write step by step solution for each example and if possible, explain with all types of TJ arrays(what types we may see in content stream) PDF's are using. I read the concept from PDF32000_2008(9.4.4 Text Space Details) still I am in confusion sate. You can found the actual pdf's in below link
Example 1 file
Example2 file
It sounds like you most of all wonder where to retrieve the widths, the w0 values from.
This actually is easy, the widths arrays are in the PDF font objects! In case of simple fonts the width values are in the Widths array. The only exception are the standard 14 fonts. For them a PDF processor is expected to know the widths of the glyphs. In case of CID fonts the width values are in the W array defaulting to the DW value defaulting to 1000.
In case of Type 1, TrueType, and CID fonts the widths are measured in units in which 1000 units correspond to 1 unit in text space.
In case of Type 3 fonts these widths shall be interpreted in glyph space as specified by FontMatrix; but as a note there indicates a common practice is to define glyphs in terms of a 1000-unit glyph coordinate system, in which case the font matrix is [0.001 0 0 0.001 0 0] which gives rise to the same 1000:1 ratio as above.
Your first example
/F33 20.665 Tf
72 633.8289 Td
[(Chapter)-375(12)]TJ
Here the font F33 is selected with font size 20.665 in the first instruction. That font is defined in object 21:
24 0 obj
[656.2 625 625 937.5 937.5 312.5 343.7 562.5 562.5 562.5 562.5 562.5 849.5 500 574.1 812.5 875 562.5 1018.5 1143.5 875 312.5 342.6 581 937.5 562.5 937.5 875 312.5 437.5 437.5 562.5 875 312.5 375 312.5 562.5 562.5 562.5 562.5 562.5 562.5 562.5 562.5 562.5 562.5 562.5 312.5 312.5 342.6 875 531.2 531.2 875 849.5 799.8 812.5 862.3 738.4 707.2 884.3 879.6 419 581 880.8 675.9 1067.1 879.6 844.9 768.5 844.9 839.1 625 782.4 864.6 849.5 1162 849.5 849.5 687.5 312.5 581 312.5 562.5 312.5 312.5 546.9 625 500 625 513.3 343.7 562.5 625 312.5 343.7 593.7 312.5 937.5 625 562.5 625 593.7 459.5 443.8 437.5 625 593.7 812.5 593.7 593.7 500]
endobj
21 0 obj
<<
/BaseFont /HLFPHX+CMBX12
/FirstChar 11
/FontDescriptor 22 0 R
/LastChar 122
/Subtype /Type1
/Type /Font
/Widths 24 0 R
>>
Thus, the glyph with code 11 has a width of .6562, the one with code 12 is .625 units wide, etc.
So at the beginning the text matrix and text line matrix point to (0, 0). After 72 633.8289 Td they point to (72, 633.8289). This is where 'C' is drawn.
Drawing 'C' advances the position the text matrix points to by a tx value of
((w0 - Tj/1000) × Tfs + Tc + Tw) * Th
The 'C' we see in the instruction parameter actually is the byte 0x43 = 67. Thus in the Widths array we find the w0 value at 1000:1 at index 56 (0-based), 812.5. There are no numeric parameters immediately following the 'C', so Tj is 0. Tfs is 20.665. Tc and Tw both are 0. Th is 1.
Thus, tx is ((.8125 - 0) × 20.665 + 0 + 0) × 1 = 16.7903125 and drawing 'C' advances the position the text matrix points to to (88.7903125, 633.8289). This is where 'h' is drawn.
Similarly drawing 'h' advances the position by tx = ((.625 - 0) × 20.665 + 0 + 0) × 1 = 12.915625 to (101.7059375, 633.8289). This is where 'a' is drawn.
Drawing 'a' advances the position by tx = ((.5469 - 0) × 20.665 + 0 + 0) × 1 = 11.3016885 to (113.007626, 633.8289). This is where 'p' is drawn.
Drawing 'p' advances the position by tx = ((.625 - 0) × 20.665 + 0 + 0) × 1 = 12.915625 to (125.923251, 633.8289). This is where 't' is drawn.
Drawing 't' advances the position by tx = ((.4375 - 0) × 20.665 + 0 + 0) × 1 = 9.0409375 to (134.9641885, 633.8289). This is where 'e' is drawn.
Drawing 'e' advances the position by tx = ((.5133 - 0) × 20.665 + 0 + 0) × 1 = 10.6073445 to (145.571533, 633.8289). This is where 'r' is drawn.
Drawing 'r' considering the numeric parameter -375 advances the position by tx = ((.4595 - (-375/1000)) × 20.665 + 0 + 0) × 1 = 17.2449425 to (162.8164755, 633.8289). This is where '1' is drawn.
Drawing '1' advances the position by tx = ((.5625 - 0) × 20.665 + 0 + 0) × 1 = 11.6240625 to (174.440538, 633.8289). This is where '2' is drawn.
Drawing '2' advances the position by tx = ((.5625 - 0) × 20.665 + 0 + 0) × 1 = 11.6240625 to (186.0646005, 633.8289).
Following this the instruction /F33 24.78709 Tf changes the text font size to 24.78709 and the instruction 0 51.30099 Td advances the position of the text line matrix and text matrix to (72, 582.52791). This is where 'P' is drawn.
Drawing 'P' considering the numeric parameter 31 advances the position by tx = ((.7685 - (31/1000)) × 24.78709 + 0 + 0) × 1 = 18.280478875 to (90.280478875, 582.52791). This is where 'a' is drawn.
...
Your second example
/C0_1 14 Tf
39.812999 681.73999 Td
[(\000"\000M\000U\000I\000P\000V\000H\000I\000\001)-82(\000$\000B\000S\000P\000V\000T\000F\000M\000\001)-82(\000X\000B\000T\000\001)-82.07099........]TJ
Here the font C0_1 is selected with font size 14. This font is composite, its descendant font is defined in object 24:
24 0 obj
<<
/BaseFont /NFAHTB+MinionPro-Regular
/CIDSystemInfo 30 0 R
/DW 1000
/FontDescriptor 31 0 R
/Subtype /CIDFontType0
/Type /Font
/W [0 [500 227 276 318]
4 5 480 6 [756 711 223]
9 10 346
11 [404 580 228 356 228 331]
17 26 480 27 28 228 29 [552 580 552 379 753 691 588 665 735 568
529 715 766 341 329 673 538 891 743 747
563 745 621 474 617 736 703 971 654 634
603 345 333 345 566 500 224 439 508 423
528 425 296 468 534 268 256 496 253 819
547 510 524 511 371 367 305 531 463 685
472 459 420 347 263 347 580 276]
97 98 480 99 [159]
100 101 480 102 [477 480 169 398 444]
107 108 279 109 [535 533 520 490 489 226 497 390 239 429
401 445 970 1062 379]
124 136 400 137 [922 869 305 550 749 973 334 671 268 273
513 770 545 341 580 512 459 737 762 580
549 762 580 263 343 514 762 341 321 580
505 580 341 702]
171 176 691 177 [661]
178 181 568 182 185
341 186 [743]
187 191 747 192 [474]
193 196
736 197 198 634 199 [603]
200 205 439 206
[421]
207 210 425 211 214 268 215 [547]
216
220 510 221 [367]
222 225 531 226 227 459
228 [420 503 500 480 418]
233 238 762 239 [691 926 666 627 737 736 766 613 518 637
606 499 1029 763 493 267 526 541 533 525
547 303 385 669 1071 914 876 722 803 561
1071 1081 798 787 1045 801 852 814 535 520
778 533 582 522 856 664 804 814 533 777]
289 290 533
291 [578]
292 293 800 294 298 480 299 [828 439 790 565 511 531 584 482 456 565
621 306 297 558 460 709 580 584 484 585
528 408 510 582 567 761 551 511 493 611
621 306 582 510 579 611 481 431 815 723
776 268 606 603 622 242 235 345 346 530
340 446 406 486 403 499 437 466 486 473
468 529 486 481 489 528 483 481 519 710
1009 711 493 338 465 452 497 454 495 464
475 488 493 480 479 574 480 482 480 568
483 486 482]
392 411 486 412 [305 349 355]
415 416 292 417 [306 372 194 192 543 371 334 262 265 228]
427 436 341 437 [178 177]
439 440 341 441 [259]
442 443 245 444 453 341 454 [178 177]
456 457
341 458 [259]
459 460 245 461 470 341 471
[178 177]
473 474 341 475 [259]
476 477 245 478
487 341 488 [178 177]
490 491 341 492 [259]
493
494 245 495 497 606 498 [454 469 407 563]
502 507 691
508 [1058 813]
510 512 691 513 520 766 521 [566 766]
523 526 757 527 [640 757 598 681]
531 532 652 533 534
877 535 536 631 537 540 757 541 542 510
543 [256]
544 545 846 546 [753 922 520 276 444 445]
552 553 279
554 [356 379]
556 557 347 558 559 345 560 561
346 562 [226]
563 564 579 565 [586 587 760 556 375 490 718 561 536 641
757 531 568]
578 580
691 581 [722]
582 585 665 586 [735]
587 591
568 592 596 715 597 [766]
598 602 341 603
[329 673]
605 608 538 609 [891]
610 613 743 614
616 747 617 [749]
618 620 621 621 [474 477]
623
624 474 625 626 617 627 629 736 630 [733]
631 632 736 633 636 971 637 639 634 640
641 603 642 [869]
643 644 1071 645 647 439
648 [512]
649 652 423 653 [528]
654 657 425
658 [424]
659 663 468 664 [534]
665 668 268
669 [258 496]
671 673 253 674 [271 819]
676 679 547
680 682 510 683 [513]
684 686 371 687 [367 366]
689 690 367 691 692 305 693 698 531 699
702 685 703 705 459 706 707 420 708 [671 367]
710 711 492 712 724 400 725 728 565 729
[723]
730 731 565 732 [568]
733 734 565 735
[643]
736 737 531 738 [528]
739 740 531 741
[584]
742 749 482 750 [487]
751 755 565 756
[621]
757 760 306 761 [474]
762 763 306 764
[308 306 297 558]
768 771 460 772 [478 709]
774 778 580 779
785 584 786 [582 584]
788 790 528 791 792 408
793 [412]
794 795 408 796 797 510 798 804
582 805 [584]
806 807 582 808 811 761 812
816 511 817 819 493 820 [401 402 401 381 401 375 404 400 401 400
401 400 367 401 691 588 507 641 568 603
766 739 341 673 686 891 743 607 747 738
563 598 617 655 754 654 725 757 691 568
766]
861 862 341
863 [747]
864 865 655 866 [757]
867 873 691
874 882 910 883 887 691 888 895 568 896
901 766 902 910 972 911 914 766 915 926
341 927 932 757 933 941 1007 942 945 757
946 953 747 954 [563 341]
956 963 655 964 [341 329 889 959 776 650 653 741 691 580
588 512 649 568 954 518]
980 981 752 982 [650 645 891 766 747 735 563 665 617 523
510 495 497 403 381 509 490 245]
1000 1001 493 1002 [512 476 404 510 501 515 446 481 587 467
605 645 403 497 496 582 665 404 508 669
544 453 523 403 509]
1027 1028 245 1029 [510]
1030 1031 481 1032 [645 245 481]
1035 1042 523 1043 1048 403 1049 1056 509 1057
1064 245 1065 1070 510 1071 1078 481 1079 1086
645 1087 1088 523 1089 1090 403 1091 1092 509
1093 1094 245 1095 1096 510 1097 1098 481 1099
1100 645 1101 1108 523 1109 1116 509 1117 1124
645 1125 1130 523 1131 1135 509 1136 1141 245
1142 1145 481 1146 1147 501 1148 [481]
1149 1153
645 1154 [523 481]
1156 1159 230 1160 1171 400 1172
[353]
1173 1177 400 1178 1179 405 1180 [400 653 767 654 741 666 958 960 720 840
581 644 956 636 439 501 486 389 490 425
726 408]
1202
1203 555 1204 [500 494 640 553 510 552 524 423 441 459
672 472 556 507 771 775 566 681 468 440
707 500 425 500 389 449 367]
1231 1232 268 1233 [256 673 719 533 500 468 545 689 547 736
511 680 467 477 366 428 356 411 872 974
1124 1133 957 457 603 623 830 1006 806 1408
1744 1095 643 566 821 836 906 1602 1675 1584
427 892]
1275
1276 745 1277 [465 619 776 427 341 566 892]
1284 1287 400 1288 [747 736 525 547]
1292
1293 480 1294 1305 691 1306 1313 568 1314 1315
341 1316 1327 747 1328 1334 736 1335 1337 634
1338 1349 439 1350 1357 425 1358 1359 268 1360
1366 510 1367 1371 525 1372 1373 531 1374 1378
547 1379 1381 459 1382 1393 637 1394 1401 606
1402 1413 565 1414 1421 482 1422 1423 306 1424
1436 584 1437 1444 582 1445 1447 511 1448 1457
400 1458 [392]
1459 1480 400 1481 [565 511 531 584 482 456 565 621 306 297
558 460 709 580 584 484 585 528 408 510
582 567 761 551 511 493 611 621 582 510
579 611 481 431 723 776 603]
1518 1521
565 1522 [723]
1523 1524 565 1525 [568]
1526 1528
565 1529 1530 531 1531 [528]
1532 1533 531 1534
[584]
1535 1542 482 1543 [487]
1544 1548 565 1549
[621]
1550 1556 306 1557 [308 306 297 558]
1561 1564 460 1565
[478 709]
1567 1571 580 1572 1578 584 1579 [582 584]
1581
1583 528 1584 1585 408 1586 [412]
1587 1588 408
1589 1590 510 1591 1597 582 1598 [584]
1599 1600
582 1601 1604 761 1605 1609 511 1610 1612 493
1613 1624 565 1625 1632 482 1633 1634 306 1635
1647 584 1648 1655 582 1656 1658 511 1659 [477 366 617 305 356 227 400 159 226 306
159]
1670 1671 105 1672 [495 565 762 916 297 223 480 461 480 486
480 472 468 486]
]
>>
endobj
Thus, the glyph with code 0 has a width of 500, code 1 has 227, code 2 has 276, code 3 has 318, code 4 and 5 have 480, code 6 has 756, etc.
Furthermore, it is important that the font encoding is Identity-H which is a pure two-byte encoding.
Thus, at the beginning the text matrix and text line matrix point to (0, 0). After 39.812999 681.73999 Td they point to (39.812999, 681.73999). This is where '\000"' = 0x0022 is drawn.
Drawing 0x0022 advances the position by tx = ((.691 - 0) × 14 + 0 + 0) × 1 = 9.674 to (49.486999, 681.73999). This is where '\000M' = 0x004d is drawn.
...
As you can see, the cases in your example files are simple:
no use of character or word spacing, no horizontal scaling;
very simple text and text line matrices, plain translation;
no changes to the current transformation matrix;
no standard 14 fonts;
...
Nonetheless, the concept should have become clear.
And I hope I have not miscalculated too often... ;)
The following:
double x = .43;
BigDecimal bd = new BigDecimal(z);
System.out.println(bd);
outputs:
.4299999.....
why does
double u = z*100;
bd = new BigDecimal(u);
System.out.println(u);
output:
43?
more generally I'm trying to find a case where the double representation causes a truncation problem. Consider the following:
double d;
int j;
BigDecimal bd;
for(int i = 0; i<10000;i++){
d= (double)(i/100.);
j = (int)(d*100.);
bd = new BigDecimal(j);
System.out.println(bd);
}
In not a single case is truncating occurring
In not a single case is truncating occurring
That is incorrect. Truncation occurs many times, you just didn't look close enough.
E.g. for i = 29:
int i = 29;
double d= i / 100.;
int j = (int)(d * 100.);
BigDecimal bd = new BigDecimal(j);
System.out.println(bd);
Output
28
OOPS!!!
Though I don't know what the point of converting j to bd is, since new BigDecimal(int) is always exact.
If you want to see all the truncation errors, try this:
for (int i = 0; i < 10000; i++){
double d = i / 100.;
int j = (int)(d * 100.);
if (i != j)
System.out.println(i + ": " + j);
}
Output
29: 28
57: 56
58: 57
113: 112
114: 113
115: 114
116: 115
201: 200
203: 202
205: 204
207: 206
226: 225
228: 227
230: 229
232: 231
251: 250
253: 252
255: 254
402: 401
406: 405
410: 409
414: 413
427: 426
431: 430
435: 434
439: 438
452: 451
456: 455
460: 459
464: 463
477: 476
481: 480
485: 484
489: 488
502: 501
506: 505
510: 509
803: 802
804: 803
812: 811
820: 819
828: 827
829: 828
837: 836
845: 844
853: 852
854: 853
862: 861
870: 869
878: 877
879: 878
887: 886
895: 894
903: 902
904: 903
912: 911
920: 919
928: 927
929: 928
937: 936
945: 944
953: 952
954: 953
962: 961
970: 969
978: 977
979: 978
987: 986
995: 994
1003: 1002
1004: 1003
1012: 1011
1020: 1019
1606: 1605
1608: 1607
1615: 1614
1624: 1623
1631: 1630
1633: 1632
1640: 1639
1649: 1648
1656: 1655
1658: 1657
1665: 1664
1674: 1673
1681: 1680
1683: 1682
1690: 1689
1699: 1698
1706: 1705
1708: 1707
1715: 1714
1724: 1723
1731: 1730
1733: 1732
1740: 1739
1749: 1748
1756: 1755
1758: 1757
1765: 1764
1774: 1773
1781: 1780
1783: 1782
1790: 1789
1799: 1798
1806: 1805
1808: 1807
1815: 1814
1824: 1823
1831: 1830
1833: 1832
1840: 1839
1849: 1848
1856: 1855
1858: 1857
1865: 1864
1874: 1873
1881: 1880
1883: 1882
1890: 1889
1899: 1898
1906: 1905
1908: 1907
1915: 1914
1924: 1923
1931: 1930
1933: 1932
1940: 1939
1949: 1948
1956: 1955
1958: 1957
1965: 1964
1974: 1973
1981: 1980
1983: 1982
1990: 1989
1999: 1998
2006: 2005
2008: 2007
2015: 2014
2024: 2023
2031: 2030
2033: 2032
2040: 2039
3205: 3204
3212: 3211
3216: 3215
3223: 3222
3230: 3229
3237: 3236
3241: 3240
3248: 3247
3255: 3254
3262: 3261
3266: 3265
3273: 3272
3280: 3279
3287: 3286
3291: 3290
3298: 3297
3305: 3304
3312: 3311
3316: 3315
3323: 3322
3330: 3329
3337: 3336
3341: 3340
3348: 3347
3355: 3354
3362: 3361
3366: 3365
3373: 3372
3380: 3379
3387: 3386
3391: 3390
3398: 3397
3405: 3404
3412: 3411
3416: 3415
3423: 3422
3430: 3429
3437: 3436
3441: 3440
3448: 3447
3455: 3454
3462: 3461
3466: 3465
3473: 3472
3480: 3479
3487: 3486
3491: 3490
3498: 3497
3505: 3504
3512: 3511
3516: 3515
3523: 3522
3530: 3529
3537: 3536
3541: 3540
3548: 3547
3555: 3554
3562: 3561
3566: 3565
3573: 3572
3580: 3579
3587: 3586
3591: 3590
3598: 3597
3605: 3604
3612: 3611
3616: 3615
3623: 3622
3630: 3629
3637: 3636
3641: 3640
3648: 3647
3655: 3654
3662: 3661
3666: 3665
3673: 3672
3680: 3679
3687: 3686
3691: 3690
3698: 3697
3705: 3704
3712: 3711
3716: 3715
3723: 3722
3730: 3729
3737: 3736
3741: 3740
3748: 3747
3755: 3754
3762: 3761
3766: 3765
3773: 3772
3780: 3779
3787: 3786
3791: 3790
3798: 3797
3805: 3804
3812: 3811
3816: 3815
3823: 3822
3830: 3829
3837: 3836
3841: 3840
3848: 3847
3855: 3854
3862: 3861
3866: 3865
3873: 3872
3880: 3879
3887: 3886
3891: 3890
3898: 3897
3905: 3904
3912: 3911
3916: 3915
3923: 3922
3930: 3929
3937: 3936
3941: 3940
3948: 3947
3955: 3954
3962: 3961
3966: 3965
3973: 3972
3980: 3979
3987: 3986
3991: 3990
3998: 3997
4005: 4004
4012: 4011
4016: 4015
4023: 4022
4030: 4029
4037: 4036
4041: 4040
4048: 4047
4055: 4054
4062: 4061
4066: 4065
4073: 4072
4080: 4079
4087: 4086
4091: 4090
6407: 6406
6410: 6409
6421: 6420
6424: 6423
6432: 6431
6435: 6434
6446: 6445
6449: 6448
6457: 6456
6460: 6459
6471: 6470
6474: 6473
6482: 6481
6485: 6484
6496: 6495
6499: 6498
6507: 6506
6510: 6509
6521: 6520
6524: 6523
6532: 6531
6535: 6534
6546: 6545
6549: 6548
6557: 6556
6560: 6559
6571: 6570
6574: 6573
6582: 6581
6585: 6584
6596: 6595
6599: 6598
6607: 6606
6610: 6609
6621: 6620
6624: 6623
6632: 6631
6635: 6634
6646: 6645
6649: 6648
6657: 6656
6660: 6659
6671: 6670
6674: 6673
6682: 6681
6685: 6684
6696: 6695
6699: 6698
6707: 6706
6710: 6709
6721: 6720
6724: 6723
6732: 6731
6735: 6734
6746: 6745
6749: 6748
6757: 6756
6760: 6759
6771: 6770
6774: 6773
6782: 6781
6785: 6784
6796: 6795
6799: 6798
6807: 6806
6810: 6809
6821: 6820
6824: 6823
6832: 6831
6835: 6834
6846: 6845
6849: 6848
6857: 6856
6860: 6859
6871: 6870
6874: 6873
6882: 6881
6885: 6884
6896: 6895
6899: 6898
6907: 6906
6910: 6909
6921: 6920
6924: 6923
6932: 6931
6935: 6934
6946: 6945
6949: 6948
6957: 6956
6960: 6959
6971: 6970
6974: 6973
6982: 6981
6985: 6984
6996: 6995
6999: 6998
7007: 7006
7010: 7009
7021: 7020
7024: 7023
7032: 7031
7035: 7034
7046: 7045
7049: 7048
7057: 7056
7060: 7059
7071: 7070
7074: 7073
7082: 7081
7085: 7084
7096: 7095
7099: 7098
7107: 7106
7110: 7109
7121: 7120
7124: 7123
7132: 7131
7135: 7134
7146: 7145
7149: 7148
7157: 7156
7160: 7159
7171: 7170
7174: 7173
7182: 7181
7185: 7184
7196: 7195
7199: 7198
7207: 7206
7210: 7209
7221: 7220
7224: 7223
7232: 7231
7235: 7234
7246: 7245
7249: 7248
7257: 7256
7260: 7259
7271: 7270
7274: 7273
7282: 7281
7285: 7284
7296: 7295
7299: 7298
7307: 7306
7310: 7309
7321: 7320
7324: 7323
7332: 7331
7335: 7334
7346: 7345
7349: 7348
7357: 7356
7360: 7359
7371: 7370
7374: 7373
7382: 7381
7385: 7384
7396: 7395
7399: 7398
7407: 7406
7410: 7409
7421: 7420
7424: 7423
7432: 7431
7435: 7434
7446: 7445
7449: 7448
7457: 7456
7460: 7459
7471: 7470
7474: 7473
7482: 7481
7485: 7484
7496: 7495
7499: 7498
7507: 7506
7510: 7509
7521: 7520
7524: 7523
7532: 7531
7535: 7534
7546: 7545
7549: 7548
7557: 7556
7560: 7559
7571: 7570
7574: 7573
7582: 7581
7585: 7584
7596: 7595
7599: 7598
7607: 7606
7610: 7609
7621: 7620
7624: 7623
7632: 7631
7635: 7634
7646: 7645
7649: 7648
7657: 7656
7660: 7659
7671: 7670
7674: 7673
7682: 7681
7685: 7684
7696: 7695
7699: 7698
7707: 7706
7710: 7709
7721: 7720
7724: 7723
7732: 7731
7735: 7734
7746: 7745
7749: 7748
7757: 7756
7760: 7759
7771: 7770
7774: 7773
7782: 7781
7785: 7784
7796: 7795
7799: 7798
7807: 7806
7810: 7809
7821: 7820
7824: 7823
7832: 7831
7835: 7834
7846: 7845
7849: 7848
7857: 7856
7860: 7859
7871: 7870
7874: 7873
7882: 7881
7885: 7884
7896: 7895
7899: 7898
7907: 7906
7910: 7909
7921: 7920
7924: 7923
7932: 7931
7935: 7934
7946: 7945
7949: 7948
7957: 7956
7960: 7959
7971: 7970
7974: 7973
7982: 7981
7985: 7984
7996: 7995
7999: 7998
8007: 8006
8010: 8009
8021: 8020
8024: 8023
8032: 8031
8035: 8034
8046: 8045
8049: 8048
8057: 8056
8060: 8059
8071: 8070
8074: 8073
8082: 8081
8085: 8084
8096: 8095
8099: 8098
8107: 8106
8110: 8109
8121: 8120
8124: 8123
8132: 8131
8135: 8134
8146: 8145
8149: 8148
8157: 8156
8160: 8159
8171: 8170
8174: 8173
8182: 8181
8185: 8184
That is 573 times, i.e. 5.73% of the time.
My code converts a video into different formats (160p to 2160p). The codec of video with rotate tag 90 needs to be changed. below id the code for the same which works fine for all videos till date.
log_wfm.debug("Changing the codec video for file [" + orgFileName + "] and name of source file ["
+ srcFileName + "]");
String codecFlipCommand = "ffmpeg -i " + orgFileName + " -c:v libx264 -preset ultrafast " + srcFileName;
log_wfm.info("Executing command to change the codec of video!");
fp = Runtime.getRuntime().exec(codecFlipCommand);
inputStream = new BufferedReader(new InputStreamReader(fp.getInputStream()));
outputStream = new BufferedReader(new InputStreamReader(fp.getErrorStream()));
StringBuffer output = new StringBuffer();
String line;
while ((line = inputStream.readLine()) != null) {
output.append(line).append('\n');
}
log_wfm.debug("stdInputForCodecChange: " + output);
output = new StringBuffer();
while ((line = outputStream.readLine()) != null) {
output.append(line).append('\n');
}
log_wfm.debug("stdErrorForCodecChange: " + output);
now have a file of size 1.5Gb. The video does not create the file with changed codec. The file till size 848.5 MB is created and then the program hangs (with no exception or error logs)
Please suggest some way to finish this codec change process.The ffmpeg command converts the video, but via java application, the process hangs Logs While converting via commandLine :
ffmpeg version N-83433-ge87a4a8 Copyright (c) 2000-2017 the FFmpeg developers built with gcc 6.3.1 (GCC) 20161221 (Red Hat 6.3.1-1)
configuration: --prefix=/root/ffmpeg_build --extra-cflags=-I/root/ffmpeg_build/include --extra-ldflags='-L/root/ffmpeg_build/lib -ldl' --bindir=/root/bin --pkg-config-flags=--static --enable-gpl --enable-nonfree --enable-libfdk_aac --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265
libavutil 55. 46.100 / 55. 46.100
libavcodec 57. 75.100 / 57. 75.100
libavformat 57. 66.101 / 57. 66.101
libavdevice 57. 2.100 / 57. 2.100
libavfilter 6. 73.100 / 6. 73.100
libswscale 4. 3.101 / 4. 3.101
libswresample 2. 4.100 / 2. 4.100
libpostproc 54. 2.100 / 54. 2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/urvashi/test/SachinSagaGameLaunchEvent.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
creation_time : 2017-12-07T06:26:23.000000Z
com.android.version: 7.1.1
Duration: 00:14:16.02, start: 0.000000, bitrate: 14231 kb/s
Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080, 13971 kb/s, SAR 1:1 DAR 16:9, 29.94 fps, 29.92 tbr, 90k tbn, 180k tbc (default)
Metadata:
rotate : 90
creation_time : 2017-12-07T06:26:23.000000Z
handler_name : VideoHandle
Side data:
displaymatrix: rotation of -90.00 degrees
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default)
Metadata:
creation_time : 2017-12-07T06:26:23.000000Z
handler_name : SoundHandle
[libx264 # 0x4601a20] using SAR=1/1
[libx264 # 0x4601a20] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 AVX2 LZCNT BMI2
[libx264 # 0x4601a20] profile Constrained Baseline, level 4.0
[libx264 # 0x4601a20] 264 - core 148 - H.264/MPEG-4 AVC codec - Copyleft 2003-2017 - http://www.videolan.org/x264.html - options: cabac=0 ref=1 deblock=0:0:0 analyse=0:0 me=dia subme=0 psy=1 psy_rd=1.00:0.00 mixed_ref=0 me_range=16 chroma_me=1 trellis=0 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=0 threads=6 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=0 weightp=0 keyint=250 keyint_min=25 scenecut=0 intra_refresh=0 rc=crf mbtree=0 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=0
Output #0, mp4, to '/home/urvashi/test/Manual_SachinSagaGameLaunchEvent.mp4':
Metadata:
major_brand : mp42
minor_version : 0
compatible_brands: isommp42
com.android.version: 7.1.1
encoder : Lavf57.66.101
Stream #0:0(eng): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1080x1920 [SAR 1:1 DAR 9:16], q=-1--1, 29.92 fps, 11488 tbn, 29.92 tbc (default)
Metadata:
handler_name : VideoHandle
creation_time : 2017-12-07T06:26:23.000000Z
encoder : Lavc57.75.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream #0:1(eng): Audio: aac (LC) ([64][0][0][0] / 0x0040), 48000 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
creation_time : 2017-12-07T06:26:23.000000Z
handler_name : SoundHandle
encoder : Lavc57.75.100 aac
Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help Past duration 0.600212 too large 48193kB time=00:00:27.26 bitrate=14479.2kbits/s speed=2.07x
Past duration 0.998863 too large
frame=25611 fps= 55 q=-1.0 Lsize= 1721466kB time=00:14:16.04 bitrate=16473.7kbits/s dup=0 drop=17 speed=1.85x
video:1707295kB audio:13453kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.041757%
[libx264 # 0x4601a20] frame I:103 Avg QP:22.16 size:242916
[libx264 # 0x4601a20] frame P:25508 Avg QP:25.17 size: 67557
Thanks in Advance.
I am building simple java application to broadcast the webcam to the server listening on
localhost:8082
with following java program
private String frameRate = "60";
private String bitRate = "100k";
private String param = " -s 640x480 -f video4linux2 -i /dev/video0 -f mpeg1video -r "+frameRate+" -b:v "+bitRate+" http://localhost:8082/raj/640/480/";
private String command =" ffmpeg ";
//to execute
proc = rt.exec(command +" "+param);
proc.waitFor();
Following is the output from starting point to 5 sec.
frame= 27 fps= 0 q=5.4 size= 88kB time=0.43 bitrate=1655.4kbits/s dup=18 drop=0
frame= 59 fps= 55 q=11.6 size= 146kB time=0.97 bitrate=1238.3kbits/s dup=45 drop=0
frame= 94 fps= 60 q=26.1 size= 207kB time=1.55 bitrate=1095.6kbits/s dup=69 drop=0
frame= 125 fps= 60 q=14.3 size= 235kB time=2.07 bitrate= 929.9kbits/s dup=83 drop=0
frame= 156 fps= 60 q=16.1 size= 276kB time=2.58 bitrate= 875.5kbits/s dup=97 drop=0
frame= 187 fps= 60 q=15.4 size= 306kB time=3.10 bitrate= 807.9kbits/s dup=111 drop=0
frame= 218 fps= 60 q=22.1 size= 343kB time=3.62 bitrate= 777.8kbits/s dup=125 drop=0
frame= 249 fps= 60 q=24.8 size= 379kB time=4.13 bitrate= 751.7kbits/s dup=139 drop=0
frame= 280 fps= 60 q=31.0 size= 402kB time=4.65 bitrate= 708.8kbits/s dup=153 drop=0
frame= 311 fps= 60 q=31.0 size= 436kB time=5.17 bitrate= 690.7kbits/s dup=167 drop=0
frame= 342 fps= 60 q=31.0 size= 458kB time=5.68 bitrate= 659.5kbits/s dup=181 drop=0
Initially, bit rate is ok, but it is in decreasing order. Is this due to java program or ffmped. How to fix the bit rate?
If the image has low motion, ( like a security camera ) the encoder may not need any more than 700kbits to encode the image. If you look at the q value, it is at 31 (31 is the max) so the quantizer is encoding at the highest quality possible and still can hit the target bit rate. You can increase the resolution to give the encoder more work to go, or you can add padding. Or you can just enjoy the bandwidth savings :)