
Recherche avancée
Autres articles (45)
-
Submit bugs and patches
13 avril 2011Unfortunately a software is never perfect.
If you think you have found a bug, report it using our ticket system. Please to help us to fix it by providing the following information : the browser you are using, including the exact version as precise an explanation as possible of the problem if possible, the steps taken resulting in the problem a link to the site / page in question
If you think you have solved the bug, fill in a ticket and attach to it a corrective patch.
You may also (...) -
De l’upload à la vidéo finale [version standalone]
31 janvier 2010, parLe chemin d’un document audio ou vidéo dans SPIPMotion est divisé en trois étapes distinctes.
Upload et récupération d’informations de la vidéo source
Dans un premier temps, il est nécessaire de créer un article SPIP et de lui joindre le document vidéo "source".
Au moment où ce document est joint à l’article, deux actions supplémentaires au comportement normal sont exécutées : La récupération des informations techniques des flux audio et video du fichier ; La génération d’une vignette : extraction d’une (...) -
Demande de création d’un canal
12 mars 2010, parEn fonction de la configuration de la plateforme, l’utilisateur peu avoir à sa disposition deux méthodes différentes de demande de création de canal. La première est au moment de son inscription, la seconde, après son inscription en remplissant un formulaire de demande.
Les deux manières demandent les mêmes choses fonctionnent à peu près de la même manière, le futur utilisateur doit remplir une série de champ de formulaire permettant tout d’abord aux administrateurs d’avoir des informations quant à (...)
Sur d’autres sites (6572)
-
FFMpeg is slow on Android [duplicate]
28 juin 2017, par Oleg FilimonovThis question already has an answer here :
I’m using this library : https://github.com/WritingMinds/ffmpeg-android
Executing this command :
ffmpeg -y -i video.mp4 -ignore_loop 0 -i overlay.gif -filter_complex `
>> "[0][1]overlay=x=mod((1080/4 + sin(t)*1080/4)\,1920):y=mod(t*300\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*1 + 1080/4 - sin(2*t)*1080/4)\,1920):y=mod(100 + t*300\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*2 + 1080/4 - sin(-90 + t/2)*1080/4)\,1920):y=mod(150 + t*250\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*3 + 1080/4 - sin(90 + 3*t)*1080/4)\,1920):y=mod(200 + t*100\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*4 + 1080/4 - sin(180 + t)*1080/4)\,1920):y=mod(t*250\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*5 + 1080/4 - sin(-180 + t)*1080/4)\,1920):y=mod(t*100\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*6 + 1080/4 - sin(2*t)*1080/4)\,1920):y=100 + mod(100 + t*300\,1080):shortest=1[res]`
>> " -map "[res]" -preset ultrafast out.mp4(Basically add a bunch of animated overlays to the video)
On PC this command takes about a second to execute (render speed is about 278 fps). But on Android device with Snapdragon 821 it takes about 10 seconds (about 30 fps).
The curious thing is that whenever command is executing on PC, CPU load is around 90%, but on Android it’s less than 1% (see screenshot : initial spike is UI stuff plus loading binaries, afterwards ffmpeg was executing until 10 second mark)
I tried executing the same command, but writing to /dev/null
instead of the file :ffmpeg -y -i video.mp4 -ignore_loop 0 -i overlay.gif -filter_complex `
>> "[0][1]overlay=x=mod((1080/4 + sin(t)*1080/4)\,1920):y=mod(t*300\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*1 + 1080/4 - sin(2*t)*1080/4)\,1920):y=mod(100 + t*300\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*2 + 1080/4 - sin(-90 + t/2)*1080/4)\,1920):y=mod(150 + t*250\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*3 + 1080/4 - sin(90 + 3*t)*1080/4)\,1920):y=mod(200 + t*100\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*4 + 1080/4 - sin(180 + t)*1080/4)\,1920):y=mod(t*250\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*5 + 1080/4 - sin(-180 + t)*1080/4)\,1920):y=mod(t*100\,1080):shortest=1[res];`
>> [res][1]overlay=x=mod((200*6 + 1080/4 - sin(2*t)*1080/4)\,1920):y=100 + mod(100 + t*300\,1080):shortest=1[res]`
>> " -map "[res]" -preset ultrafast -f null /dev/nullThis reduced time by 3 seconds.
I also tried replacing gif overlay with png overlay, but speed didn’t improve significantly.
Here’s log of the command execution on Android :
ffmpeg version n3.0.1 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.8 (GCC)
configuration: --target-os=linux --cross-prefix=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-libmp3lame --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/vagrant/SourceCode/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/vagrant/SourceCode/ffmpeg-android/build/armeabi-v7a --extra-cflags='-I/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all' --extra-ldflags='-L/home/vagrant/SourceCode/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
libavutil 55. 17.103 / 55. 17.103
libavcodec 57. 24.102 / 57. 24.102
libavformat 57. 25.100 / 57. 25.100
libavdevice 57. 0.101 / 57. 0.101
libavfilter 6. 31.100 / 6. 31.100
libswscale 4. 0.100 / 4. 0.100
libswresample 2. 0.101 / 2. 0.101
libpostproc 54. 0.100 / 54. 0.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/data/user/0/com.example.user.proofofconcept/files/in_10s.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 2017-06-27 18:07:54
Duration: 00:00:10.47, start: 0.000000, bitrate: 14043 kb/s
Stream #0:0(eng): Video: h264 (Baseline) (avc1 / 0x31637661), yuvj420p(pc, smpte170m), 1280x720, 13971 kb/s, 29.86 fps, 30.01 tbr, 90k tbn, 180k tbc (default)
Metadata:
creation_time : 2017-06-27 18:07:53
handler_name : VideoHandle
Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 96 kb/s (default)
Metadata:
creation_time : 2017-06-27 18:07:54
handler_name : SoundHandle
Input #1, gif, from '/data/user/0/com.example.user.proofofconcept/files/overlay.gif':
Duration: N/A, bitrate: N/A
Stream #1:0: Video: gif, bgra, 130x98, 6.17 fps, 4.92 tbr, 100 tbn, 100 tbc
[swscaler @ 0xeddb3000] deprecated pixel format used, make sure you did set range correctly
[libx264 @ 0xee1fdc00] using cpu capabilities: none!
[libx264 @ 0xee1fdc00] profile Constrained Baseline, level 3.2
[libx264 @ 0xee1fdc00] 264 - core 148 - H.264/MPEG-4 AVC codec - Copyleft 2003-2015 - 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 '/storage/emulated/0/temp/out.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf57.25.100
Stream #0:0: Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q=-1--1, 30.01 fps, 90k tbn, 30.01 tbc (default)
Metadata:
encoder : Lavc57.24.102 libx264
Side data:
unknown side data type 10 (24 bytes)
Stream mapping:
Stream #0:0 (h264) -> overlay:main
Stream #1:0 (gif) -> overlay:overlay
Stream #1:0 (gif) -> overlay:overlay
Stream #1:0 (gif) -> overlay:overlay
Stream #1:0 (gif) -> overlay:overlay
Stream #1:0 (gif) -> overlay:overlay
Stream #1:0 (gif) -> overlay:overlay
Stream #1:0 (gif) -> overlay:overlay
overlay -> Stream #0:0 (libx264)
Press [q] to stop, [?] for help
frame= 16 fps=0.0 q=24.0 size= 227kB time=00:00:00.29 bitrate=6210.2kbits/s dup=2 drop=0 speed=0.56x
frame= 29 fps= 28 q=24.0 size= 451kB time=00:00:00.73 bitrate=5042.1kbits/s dup=2 drop=0 speed=0.707x
frame= 40 fps= 26 q=24.0 size= 637kB time=00:00:01.09 bitrate=4744.5kbits/s dup=2 drop=0 speed=0.71x
frame= 57 fps= 28 q=24.0 size= 903kB time=00:00:01.66 bitrate=4441.3kbits/s dup=2 drop=0 speed=0.812x
frame= 75 fps= 29 q=24.0 size= 1186kB time=00:00:02.26 bitrate=4288.0kbits/s dup=2 drop=0 speed=0.885x
frame= 94 fps= 31 q=24.0 size= 1456kB time=00:00:02.89 bitrate=4113.4kbits/s dup=2 drop=0 speed=0.945x
frame= 113 fps= 32 q=25.0 size= 1780kB time=00:00:03.53 bitrate=4127.3kbits/s dup=2 drop=0 speed=0.985x
frame= 131 fps= 32 q=24.0 size= 2101kB time=00:00:04.13 bitrate=4165.9kbits/s dup=2 drop=0 speed=1.01x
frame= 150 fps= 33 q=24.0 size= 2461kB time=00:00:04.76 bitrate=4231.2kbits/s dup=2 drop=0 speed=1.03x
frame= 165 fps= 32 q=24.0 size= 2721kB time=00:00:05.26 bitrate=4234.1kbits/s dup=2 drop=0 speed=1.03x
frame= 185 fps= 33 q=24.0 size= 3028kB time=00:00:05.93 bitrate=4182.4kbits/s dup=2 drop=0 speed=1.05x
frame= 205 fps= 33 q=24.0 size= 3329kB time=00:00:06.59 bitrate=4133.3kbits/s dup=2 drop=0 speed=1.07x
frame= 224 fps= 34 q=24.0 size= 3610kB time=00:00:07.23 bitrate=4089.7kbits/s dup=2 drop=0 speed=1.08x
frame= 243 fps= 34 q=24.0 size= 3901kB time=00:00:07.86 bitrate=4063.3kbits/s dup=2 drop=0 speed=1.09x
frame= 264 fps= 34 q=24.0 size= 4297kB time=00:00:08.56 bitrate=4110.6kbits/s dup=2 drop=0 speed=1.11x
frame= 284 fps= 35 q=24.0 size= 4601kB time=00:00:09.23 bitrate=4083.5kbits/s dup=2 drop=0 speed=1.12x
frame= 304 fps= 35 q=24.0 size= 4886kB time=00:00:09.89 bitrate=4044.4kbits/s dup=2 drop=0 speed=1.14x
frame= 314 fps= 35 q=-1.0 Lsize= 5138kB time=00:00:10.46 bitrate=4022.4kbits/s dup=2 drop=0 speed=1.16x
video:5135kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.040961%
[libx264 @ 0xee1fdc00] frame I:2 Avg QP:20.50 size: 84700
[libx264 @ 0xee1fdc00] frame P:312 Avg QP:24.06 size: 16310
[libx264 @ 0xee1fdc00] mb I I16..4: 100.0% 0.0% 0.0%
[libx264 @ 0xee1fdc00] mb P I16..4: 7.7% 0.0% 0.0% P16..4: 54.9% 0.0% 0.0% 0.0% 0.0% skip:37.4%
[libx264 @ 0xee1fdc00] coded y,uvDC,uvAC intra: 62.7% 22.9% 3.4% inter: 26.1% 8.4% 0.9%
[libx264 @ 0xee1fdc00] i16 v,h,dc,p: 13% 14% 59% 13%
[libx264 @ 0xee1fdc00] i8c dc,h,v,p: 47% 24% 22% 6%
[libx264 @ 0xee1fdc00] kb/s:4020.26
Success
time used: 9843So what could be the reason of such low CPU load ?
-
FFmpeg fontcolor_expr to dynamically change fontcolor over time
26 juin 2017, par MatthewI’m using Fluent-FFmpeg with Node.JS to create videos with text overlaid on them and I’m wanting to change the color of the text as the video progresses.
My filters follow this pattern :
drawtext=enable='between(t,18.93,20.28)':fontfile=fonts/cousine-bold.ttf:fontsize=144:fontcolor_expr=%{expr\\\: if(between(t\\, 0\\, 20)\\, 888888\\, 111111)}:x=82:y=288:text='PROGRAMMING'
And it prints out this stuff :
ffmpeg version 3.3.2 Copyright (c) 2000-2017 the FFmpeg developers
built with Apple LLVM version 8.1.0 (clang-802.0.42)
configuration: --prefix=/usr/local/Cellar/ffmpeg/3.3.2 --enable-shared --enable-pthreads --enable-gpl --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-libfreetype --enable-libmp3lame --enable-libx264 --enable-libxvid --enable-opencl --disable-lzma --enable-vda
libavutil 55. 58.100 / 55. 58.100
libavcodec 57. 89.100 / 57. 89.100
libavformat 57. 71.100 / 57. 71.100
libavdevice 57. 6.100 / 57. 6.100
libavfilter 6. 82.100 / 6. 82.100
libavresample 3. 5. 0 / 3. 5. 0
libswscale 4. 6.100 / 4. 6.100
libswresample 2. 7.100 / 2. 7.100
libpostproc 54. 5.100 / 54. 5.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'uploads/0c719e39820366bc62c8fd66a6327828':
Metadata:
major_brand : qt
minor_version : 0
compatible_brands: qt
creation_time : 2017-06-23T00:19:46.000000Z
com.apple.quicktime.make: Apple
com.apple.quicktime.model: MacBookPro9,2
com.apple.quicktime.software: Mac OS X 10.12.5 (16F73)
com.apple.quicktime.creationdate: 2017-06-22T17:18:42-0700
Duration: 00:00:22.00, start: 0.000000, bitrate: 3408 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 1280x720 [SAR 1:1 DAR 16:9], 3091 kb/s, 15 fps, 15 tbr, 30k tbn, 50 tbc (default)
Metadata:
creation_time : 2017-06-23T00:19:46.000000Z
handler_name : Core Media Data Handler
encoder : H.264
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 316 kb/s (default)
Metadata:
creation_time : 2017-06-23T00:19:46.000000Z
handler_name : Core Media Data Handler
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
[libx264 @ 0x7ff72e810a00] using SAR=1/1
[libx264 @ 0x7ff72e810a00] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX
[libx264 @ 0x7ff72e810a00] profile High, level 3.1
[libx264 @ 0x7ff72e810a00] 264 - core 148 r2748 97eaef2 - H.264/MPEG-4 AVC codec - Copyleft 2003-2016 - 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=6 lookahead_threads=1 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=2 keyint=250 keyint_min=15 scenecut=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 './outputFile.mp4':
Metadata:
major_brand : qt
minor_version : 0
compatible_brands: qt
com.apple.quicktime.creationdate: 2017-06-22T17:18:42-0700
com.apple.quicktime.make: Apple
com.apple.quicktime.model: MacBookPro9,2
com.apple.quicktime.software: Mac OS X 10.12.5 (16F73)
encoder : Lavf57.71.100
Stream #0:0(und): Video: h264 (libx264) ([33][0][0][0] / 0x0021), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 15 fps, 15360 tbn, 15 tbc (default)
Metadata:
creation_time : 2017-06-23T00:19:46.000000Z
handler_name : Core Media Data Handler
encoder : Lavc57.89.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) ([64][0][0][0] / 0x0040), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
creation_time : 2017-06-23T00:19:46.000000Z
handler_name : Core Media Data Handler
encoder : Lavc57.89.100 aac
frame= 52 fps=0.0 q=27.0 size= 37kB time=00:00:04.66 bitrate= 65.7kbits/s speed=8.74x
frame= 72 fps= 70 q=27.0 size= 187kB time=00:00:06.03 bitrate= 253.4kbits/s speed=5.83x
frame= 95 fps= 61 q=27.0 size= 358kB time=00:00:07.52 bitrate= 390.4kbits/s speed=4.82x
frame= 117 fps= 57 q=27.0 size= 569kB time=00:00:08.98 bitrate= 518.9kbits/s speed=4.35x
frame= 137 fps= 53 q=27.0 size= 732kB time=00:00:10.32 bitrate= 580.2kbits/s speed=4.03x
frame= 159 fps= 52 q=27.0 size= 942kB time=00:00:11.79 bitrate= 654.7kbits/s speed=3.84x
frame= 180 fps= 50 q=27.0 size= 1110kB time=00:00:13.18 bitrate= 689.5kbits/s speed=3.67x
frame= 204 fps= 50 q=27.0 size= 1331kB time=00:00:14.78 bitrate= 737.2kbits/s speed= 3.6x
frame= 223 fps= 48 q=27.0 size= 1486kB time=00:00:16.06 bitrate= 757.9kbits/s speed=3.49x
frame= 248 fps= 48 q=27.0 size= 1708kB time=00:00:17.71 bitrate= 789.7kbits/s speed=3.46x
frame= 266 fps= 47 q=27.0 size= 1859kB time=00:00:18.92 bitrate= 805.0kbits/s speed=3.35x
frame= 285 fps= 46 q=27.0 size= 2013kB time=00:00:20.19 bitrate= 816.4kbits/s speed=3.28x
[drawtext @ 0x7ff72d84bc00] Cannot find color '888888.000000'
Last message repeated 9 times
frame= 308 fps= 46 q=27.0 size= 2224kB time=00:00:21.75 bitrate= 837.5kbits/s speed=3.27x
frame= 330 fps= 46 q=27.0 size= 2401kB time=00:00:21.96 bitrate= 895.5kbits/s speed=3.06x
frame= 330 fps= 41 q=-1.0 Lsize= 2834kB time=00:00:22.00 bitrate=1055.0kbits/s speed=2.73x
video:2484kB audio:338kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.441003%
[libx264 @ 0x7ff72e810a00] frame I:22 Avg QP:13.64 size: 47604
[libx264 @ 0x7ff72e810a00] frame P:154 Avg QP:16.76 size: 8574
[libx264 @ 0x7ff72e810a00] frame B:154 Avg QP:19.42 size: 1138
[libx264 @ 0x7ff72e810a00] consecutive B-frames: 6.7% 93.3% 0.0% 0.0%
[libx264 @ 0x7ff72e810a00] mb I I16..4: 85.1% 13.3% 1.6%
[libx264 @ 0x7ff72e810a00] mb P I16..4: 5.5% 3.1% 0.0% P16..4: 41.5% 4.0% 5.5% 0.0% 0.0% skip:40.4%
[libx264 @ 0x7ff72e810a00] mb B I16..4: 0.0% 0.0% 0.0% B16..8: 14.7% 0.1% 0.0% direct: 4.7% skip:80.4% L0:28.0% L1:71.4% BI: 0.7%
[libx264 @ 0x7ff72e810a00] 8x8 transform intra:21.7% inter:84.8%
[libx264 @ 0x7ff72e810a00] coded y,uvDC,uvAC intra: 60.5% 98.6% 92.9% inter: 6.0% 24.3% 3.1%
[libx264 @ 0x7ff72e810a00] i16 v,h,dc,p: 35% 19% 43% 3%
[libx264 @ 0x7ff72e810a00] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 10% 7% 80% 1% 1% 1% 1% 0% 0%
[libx264 @ 0x7ff72e810a00] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 33% 20% 45% 0% 0% 0% 0% 0% 0%
[libx264 @ 0x7ff72e810a00] i8c dc,h,v,p: 77% 9% 10% 4%
[libx264 @ 0x7ff72e810a00] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x7ff72e810a00] ref P L0: 75.3% 0.3% 17.1% 7.3%
[libx264 @ 0x7ff72e810a00] ref B L0: 80.0% 20.0%
[libx264 @ 0x7ff72e810a00] kb/s:924.66
[aac @ 0x7ff72e812200] Qavg: 2159.960In particular this annoying bit :
[drawtext @ 0x7ff72d84bc00] Cannot find color '888888.000000'
However, it would be really nice to do something like this :
fontcolor_expr=%{expr\\\\: if(between(t\\, 0\\, 20)\\, pink\\, white)}
Which prints out this error :
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
frame= 287 fps= 46 q=27.0 size= 2023kB time=00:00:20.31 bitrate= 815.9kbits/s speed=3.27x
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not valid
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Undefined constant or missing '(' in 'pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] [Eval @ 0x7fff5f7760c0] Missing ')' or too many args in 'if(between(t,0,20),pink,white)'
[Parsed_drawtext_0 @ 0x7ff3cf523300] Expression 'if(between(t, 0, 20), pink, white)' for the expr text expansion function is not validThere aren’t a lot of examples of fontcolor_expr, so I wanted to see if anyone had experience with this. Any tips on changing fonrcolors dynamically without creating multiple filters ?
Please note that
fontcolor_expr=white
andfontcolor_expr=888888
are valid and create videos with a fixed font color. -
Scale filter crashes with error when used from transcoding example
27 juin 2017, par ValiI’ve modified a bit (just to compile in c++) this code example :
https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/transcoding.c.What works : as is (null filter), a number of other filters like framerate, drawtext, ...
What doesn’t work : scale filter when scaling down.
I use the following syntax for scale ( I’ve tried many others also, same effect) :
"scale=w=iw/2 :-1"The error is : "Input picture width (240) is greater than stride (128)" where the values for width and stride depend on the input.
Misc environment info : windows, VS 2017, input example : rtsp ://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov
Any clue as to what I’m doing wrong ?
Thanks !
EDITED to add working code sample
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avfilter.lib")
/*
* Copyright (c) 2010 Nicolas George
* Copyright (c) 2011 Stefano Sabatini
* Copyright (c) 2014 Andrey Utkin
*
**** EDITED 2017 for testing (see original here: https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/transcoding.c)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* @file
* API example for demuxing, decoding, filtering, encoding and muxing
* @example transcoding.c
*/
extern "C"
{
#include <libavcodec></libavcodec>avcodec.h>
#include <libavformat></libavformat>avformat.h>
#include <libavfilter></libavfilter>avfiltergraph.h>
#include <libavfilter></libavfilter>buffersink.h>
#include <libavfilter></libavfilter>buffersrc.h>
#include <libavutil></libavutil>opt.h>
#include <libavutil></libavutil>pixdesc.h>
}
static AVFormatContext *ifmt_ctx;
static AVFormatContext *ofmt_ctx;
typedef struct FilteringContext {
AVFilterContext *buffersink_ctx;
AVFilterContext *buffersrc_ctx;
AVFilterGraph *filter_graph;
} FilteringContext;
static FilteringContext *filter_ctx;
typedef struct StreamContext {
AVCodecContext *dec_ctx;
AVCodecContext *enc_ctx;
} StreamContext;
static StreamContext *stream_ctx;
static int open_input_file(const char *filename, int& videoStreamIndex)
{
int ret;
unsigned int i;
ifmt_ctx = NULL;
if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
return ret;
}
if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");
return ret;
}
// Just need video
videoStreamIndex = -1;
for (unsigned int i = 0; i < ifmt_ctx->nb_streams; i++)
{
if (ifmt_ctx->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
continue;
videoStreamIndex = i;
break;
}
if (videoStreamIndex < 0)
{
av_log(NULL, AV_LOG_ERROR, "Cannot find video stream\n");
return videoStreamIndex;
}
stream_ctx = (StreamContext*)av_mallocz_array(ifmt_ctx->nb_streams, sizeof(*stream_ctx));
if (!stream_ctx)
return AVERROR(ENOMEM);
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
// Just need video
if (i != videoStreamIndex)
continue;
AVStream *stream = ifmt_ctx->streams[i];
AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);
AVCodecContext *codec_ctx;
if (!dec) {
av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i);
return AVERROR_DECODER_NOT_FOUND;
}
codec_ctx = avcodec_alloc_context3(dec);
if (!codec_ctx) {
av_log(NULL, AV_LOG_ERROR, "Failed to allocate the decoder context for stream #%u\n", i);
return AVERROR(ENOMEM);
}
ret = avcodec_parameters_to_context(codec_ctx, stream->codecpar);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to copy decoder parameters to input decoder context "
"for stream #%u\n", i);
return ret;
}
/* Reencode video & audio and remux subtitles etc. */
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO
|| codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO)
codec_ctx->framerate = av_guess_frame_rate(ifmt_ctx, stream, NULL);
/* Open decoder */
ret = avcodec_open2(codec_ctx, dec, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i);
return ret;
}
}
stream_ctx[i].dec_ctx = codec_ctx;
}
av_dump_format(ifmt_ctx, 0, filename, 0);
return 0;
}
static int open_output_file(const char *filename, const int videoStreamIndex)
{
AVStream *out_stream;
AVStream *in_stream;
AVCodecContext *dec_ctx, *enc_ctx;
AVCodec *encoder;
int ret;
unsigned int i;
ofmt_ctx = NULL;
avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename);
if (!ofmt_ctx) {
av_log(NULL, AV_LOG_ERROR, "Could not create output context\n");
return AVERROR_UNKNOWN;
}
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
// Just need video
if (i != videoStreamIndex)
continue;
out_stream = avformat_new_stream(ofmt_ctx, NULL);
if (!out_stream) {
av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n");
return AVERROR_UNKNOWN;
}
in_stream = ifmt_ctx->streams[i];
dec_ctx = stream_ctx[i].dec_ctx;
if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
/* in this example, we choose transcoding to same codec */
encoder = avcodec_find_encoder(dec_ctx->codec_id);
if (!encoder) {
av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n");
return AVERROR_INVALIDDATA;
}
enc_ctx = avcodec_alloc_context3(encoder);
if (!enc_ctx) {
av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n");
return AVERROR(ENOMEM);
}
/* In this example, we transcode to same properties (picture size,
* sample rate etc.). These properties can be changed for output
* streams easily using filters */
enc_ctx->height = dec_ctx->height;
enc_ctx->width = dec_ctx->width;
enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio;
/* take first format from list of supported formats */
if (encoder->pix_fmts)
enc_ctx->pix_fmt = encoder->pix_fmts[0];
else
enc_ctx->pix_fmt = dec_ctx->pix_fmt;
/* video time_base can be set to whatever is handy and supported by encoder */
//enc_ctx->time_base = av_inv_q(dec_ctx->framerate);
enc_ctx->time_base = dec_ctx->time_base;
/* Third parameter can be used to pass settings to encoder */
ret = avcodec_open2(enc_ctx, encoder, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open video encoder for stream #%u\n", i);
return ret;
}
ret = avcodec_parameters_from_context(out_stream->codecpar, enc_ctx);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Failed to copy encoder parameters to output stream #%u\n", i);
return ret;
}
if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER)
enc_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
out_stream->time_base = enc_ctx->time_base;
stream_ctx[i].enc_ctx = enc_ctx;
}
else if (dec_ctx->codec_type == AVMEDIA_TYPE_UNKNOWN) {
av_log(NULL, AV_LOG_FATAL, "Elementary stream #%d is of unknown type, cannot proceed\n", i);
return AVERROR_INVALIDDATA;
}
else {
/* if this stream must be remuxed */
ret = avcodec_parameters_copy(out_stream->codecpar, in_stream->codecpar);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Copying parameters for stream #%u failed\n", i);
return ret;
}
out_stream->time_base = in_stream->time_base;
}
}
av_dump_format(ofmt_ctx, 0, filename, 1);
if (!(ofmt_ctx->oformat->flags & AVFMT_NOFILE)) {
ret = avio_open(&ofmt_ctx->pb, filename, AVIO_FLAG_WRITE);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Could not open output file '%s'", filename);
return ret;
}
}
/* init muxer, write output file header */
ret = avformat_write_header(ofmt_ctx, NULL);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error occurred when opening output file\n");
return ret;
}
return 0;
}
static int init_filter(FilteringContext* fctx, AVCodecContext *dec_ctx,
AVCodecContext *enc_ctx, const char *filter_spec)
{
char args[512];
int ret = 0;
AVFilter *buffersrc = NULL;
AVFilter *buffersink = NULL;
AVFilterContext *buffersrc_ctx = NULL;
AVFilterContext *buffersink_ctx = NULL;
AVFilterInOut *outputs = avfilter_inout_alloc();
AVFilterInOut *inputs = avfilter_inout_alloc();
AVFilterGraph *filter_graph = avfilter_graph_alloc();
if (!outputs || !inputs || !filter_graph) {
ret = AVERROR(ENOMEM);
goto end;
}
if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
buffersrc = avfilter_get_by_name("buffer");
buffersink = avfilter_get_by_name("buffersink");
if (!buffersrc || !buffersink) {
av_log(NULL, AV_LOG_ERROR, "filtering source or sink element not found\n");
ret = AVERROR_UNKNOWN;
goto end;
}
snprintf(args, sizeof(args),
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
dec_ctx->time_base.num, dec_ctx->time_base.den,
dec_ctx->sample_aspect_ratio.num,
dec_ctx->sample_aspect_ratio.den);
ret = avfilter_graph_create_filter(&buffersrc_ctx, buffersrc, "in",
args, NULL, filter_graph);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot create buffer source\n");
goto end;
}
ret = avfilter_graph_create_filter(&buffersink_ctx, buffersink, "out",
NULL, NULL, filter_graph);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot create buffer sink\n");
goto end;
}
ret = av_opt_set_bin(buffersink_ctx, "pix_fmts",
(uint8_t*)&enc_ctx->pix_fmt, sizeof(enc_ctx->pix_fmt),
AV_OPT_SEARCH_CHILDREN);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot set output pixel format\n");
goto end;
}
}
else {
ret = AVERROR_UNKNOWN;
goto end;
}
/* 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;
if (!outputs->name || !inputs->name) {
ret = AVERROR(ENOMEM);
goto end;
}
if ((ret = avfilter_graph_parse_ptr(filter_graph, filter_spec,
&inputs, &outputs, NULL)) < 0)
goto end;
if ((ret = avfilter_graph_config(filter_graph, NULL)) < 0)
goto end;
/* Fill FilteringContext */
fctx->buffersrc_ctx = buffersrc_ctx;
fctx->buffersink_ctx = buffersink_ctx;
fctx->filter_graph = filter_graph;
end:
avfilter_inout_free(&inputs);
avfilter_inout_free(&outputs);
return ret;
}
static int init_filters(const int videoStreamIndex)
{
const char *filter_spec;
unsigned int i;
int ret;
filter_ctx = (FilteringContext*)av_malloc_array(ifmt_ctx->nb_streams, sizeof(*filter_ctx));
if (!filter_ctx)
return AVERROR(ENOMEM);
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
// Just video
if (i != videoStreamIndex)
continue;
filter_ctx[i].buffersrc_ctx = NULL;
filter_ctx[i].buffersink_ctx = NULL;
filter_ctx[i].filter_graph = NULL;
if (!(ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO
|| ifmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO))
continue;
filter_spec = "null"; /* passthrough (dummy) filter for video */
//filter_spec = "scale=w=iw/2:-1";
// filter_spec = "drawtext=fontfile=FreeSerif.ttf: text='%{localtime}': x=w-text_w: y=0: fontsize=24: fontcolor=yellow@1.0: box=1: boxcolor=red@1.0";
// filter_spec = "drawtext=fontfile=FreeSerif.ttf :text='test': x=w-text_w: y=text_h: fontsize=24: fontcolor=yellow@1.0: box=1: boxcolor=red@1.0";
ret = init_filter(&filter_ctx[i], stream_ctx[i].dec_ctx,
stream_ctx[i].enc_ctx, filter_spec);
if (ret)
return ret;
}
return 0;
}
static int encode_write_frame(AVFrame *filt_frame, unsigned int stream_index, int *got_frame, const int videoStreamIndex) {
// Just video
if (stream_index != videoStreamIndex)
return 0;
int ret;
int got_frame_local;
AVPacket enc_pkt;
int(*enc_func)(AVCodecContext *, AVPacket *, const AVFrame *, int *) =
(ifmt_ctx->streams[stream_index]->codecpar->codec_type ==
AVMEDIA_TYPE_VIDEO) ? avcodec_encode_video2 : avcodec_encode_audio2;
if (!got_frame)
got_frame = &got_frame_local;
// av_log(NULL, AV_LOG_INFO, "Encoding frame\n");
/* encode filtered frame */
enc_pkt.data = NULL;
enc_pkt.size = 0;
av_init_packet(&enc_pkt);
ret = enc_func(stream_ctx[stream_index].enc_ctx, &enc_pkt,
filt_frame, got_frame);
av_frame_free(&filt_frame);
if (ret < 0)
return ret;
if (!(*got_frame))
return 0;
/* prepare packet for muxing */
/*enc_pkt.stream_index = stream_index;
av_packet_rescale_ts(&enc_pkt, stream_ctx[stream_index].enc_ctx->time_base, ofmt_ctx->streams[stream_index]->time_base);*/
enc_pkt.stream_index = 0;
av_packet_rescale_ts(&enc_pkt, stream_ctx[stream_index].enc_ctx->time_base, ofmt_ctx->streams[0]->time_base);
av_log(NULL, AV_LOG_DEBUG, "Muxing frame\n");
/* mux encoded frame */
ret = av_interleaved_write_frame(ofmt_ctx, &enc_pkt);
return ret;
}
static int filter_encode_write_frame(AVFrame *frame, unsigned int stream_index, const int videoStreamIndex)
{
// Just video, all else crashes
if (stream_index != videoStreamIndex)
return 0;
int ret;
AVFrame *filt_frame;
// av_log(NULL, AV_LOG_INFO, "Pushing decoded frame to filters\n");
/* push the decoded frame into the filtergraph */
ret = av_buffersrc_add_frame_flags(filter_ctx[stream_index].buffersrc_ctx,
frame, 0);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Error while feeding the filtergraph\n");
return ret;
}
/* pull filtered frames from the filtergraph */
while (1) {
filt_frame = av_frame_alloc();
if (!filt_frame) {
ret = AVERROR(ENOMEM);
break;
}
// av_log(NULL, AV_LOG_INFO, "Pulling filtered frame from filters\n");
ret = av_buffersink_get_frame(filter_ctx[stream_index].buffersink_ctx,
filt_frame);
if (ret < 0) {
/* if no more frames for output - returns AVERROR(EAGAIN)
* if flushed and no more frames for output - returns AVERROR_EOF
* rewrite retcode to 0 to show it as normal procedure completion
*/
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
ret = 0;
av_frame_free(&filt_frame);
break;
}
filt_frame->pict_type = AV_PICTURE_TYPE_NONE;
ret = encode_write_frame(filt_frame, stream_index, NULL, videoStreamIndex);
if (ret < 0)
break;
}
return ret;
}
static int flush_encoder(unsigned int stream_index, const int videoStreamIndex)
{
int ret;
int got_frame;
// Just video
if (stream_index != videoStreamIndex)
return 0;
if (!(stream_ctx[stream_index].enc_ctx->codec->capabilities &
AV_CODEC_CAP_DELAY))
return 0;
while (1) {
av_log(NULL, AV_LOG_INFO, "Flushing stream #%u encoder\n", stream_index);
ret = encode_write_frame(NULL, stream_index, &got_frame, videoStreamIndex);
if (ret < 0)
break;
if (!got_frame)
return 0;
}
return ret;
}
#include <vector>
int main(int argc, char **argv)
{
int ret;
AVPacket packet;
packet.data = NULL;
packet.size = 0;
AVFrame *frame = NULL;
enum AVMediaType type;
unsigned int stream_index;
unsigned int i;
int got_frame;
int(*dec_func)(AVCodecContext *, AVFrame *, int *, const AVPacket *);
#ifdef _DEBUG
// Hardcoded arguments
std::vector varguments;
{
varguments.push_back(argv[0]);
// Source
varguments.push_back("./big_buck_bunny_short.mp4 ");
// Destination
varguments.push_back("./big_buck_bunny_short-processed.mp4");
}
char** arguments = new char*[varguments.size()];
for (unsigned int i = 0; i < varguments.size(); i++)
{
arguments[i] = varguments[i];
}
argc = varguments.size();
argv = arguments;
#endif // _DEBUG
if (argc != 3) {
av_log(NULL, AV_LOG_ERROR, "Usage: %s <input file="file" /> <output file="file">\n", argv[0]);
return 1;
}
av_register_all();
avfilter_register_all();
int videoStreamIndex = -1;
if ((ret = open_input_file(argv[1], videoStreamIndex)) < 0)
goto end;
if ((ret = open_output_file(argv[2], videoStreamIndex)) < 0)
goto end;
if ((ret = init_filters(videoStreamIndex)) < 0)
goto end;
// Stop after a couple of frames
int framesToGet = 100;
/* read all packets */
//while (framesToGet--)
while(1)
{
if ((ret = av_read_frame(ifmt_ctx, &packet)) < 0)
break;
stream_index = packet.stream_index;
// I just need video
if (stream_index != videoStreamIndex) {
av_packet_unref(&packet);
continue;
}
type = ifmt_ctx->streams[packet.stream_index]->codecpar->codec_type;
av_log(NULL, AV_LOG_DEBUG, "Demuxer gave frame of stream_index %u\n",
stream_index);
if (filter_ctx[stream_index].filter_graph) {
av_log(NULL, AV_LOG_DEBUG, "Going to reencode&filter the frame\n");
frame = av_frame_alloc();
if (!frame) {
ret = AVERROR(ENOMEM);
break;
}
av_packet_rescale_ts(&packet,
ifmt_ctx->streams[stream_index]->time_base,
stream_ctx[stream_index].dec_ctx->time_base);
dec_func = (type == AVMEDIA_TYPE_VIDEO) ? avcodec_decode_video2 :
avcodec_decode_audio4;
ret = dec_func(stream_ctx[stream_index].dec_ctx, frame,
&got_frame, &packet);
if (ret < 0) {
av_frame_free(&frame);
av_log(NULL, AV_LOG_ERROR, "Decoding failed\n");
break;
}
if (got_frame) {
frame->pts = frame->best_effort_timestamp;
ret = filter_encode_write_frame(frame, stream_index, videoStreamIndex);
av_frame_free(&frame);
if (ret < 0)
goto end;
}
else {
av_frame_free(&frame);
}
}
else {
/* remux this frame without reencoding */
av_packet_rescale_ts(&packet,
ifmt_ctx->streams[stream_index]->time_base,
ofmt_ctx->streams[stream_index]->time_base);
ret = av_interleaved_write_frame(ofmt_ctx, &packet);
if (ret < 0)
goto end;
}
av_packet_unref(&packet);
}
/* flush filters and encoders */
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
/* flush filter */
if (!filter_ctx[i].filter_graph)
continue;
ret = filter_encode_write_frame(NULL, i, videoStreamIndex);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Flushing filter failed\n");
goto end;
}
/* flush encoder */
ret = flush_encoder(i, videoStreamIndex);
if (ret < 0) {
av_log(NULL, AV_LOG_ERROR, "Flushing encoder failed\n");
goto end;
}
}
av_write_trailer(ofmt_ctx);
end:
av_packet_unref(&packet);
av_frame_free(&frame);
for (i = 0; i < ifmt_ctx->nb_streams; i++) {
// Just video
if (i != videoStreamIndex)
continue;
avcodec_free_context(&stream_ctx[i].dec_ctx);
if (ofmt_ctx && ofmt_ctx->nb_streams > i && ofmt_ctx->streams[i] && stream_ctx[i].enc_ctx)
avcodec_free_context(&stream_ctx[i].enc_ctx);
if (filter_ctx && filter_ctx[i].filter_graph)
avfilter_graph_free(&filter_ctx[i].filter_graph);
}
av_free(filter_ctx);
av_free(stream_ctx);
avformat_close_input(&ifmt_ctx);
if (ofmt_ctx && !(ofmt_ctx->oformat->flags & AVFMT_NOFILE))
avio_closep(&ofmt_ctx->pb);
avformat_free_context(ofmt_ctx);
/*if (ret < 0)
av_log(NULL, AV_LOG_ERROR, "Error occurred: %s\n", av_err2str(ret));*/
return ret ? 1 : 0;
}
</output></vector>