tanersener / mobile-ffmpeg

FFmpeg for Android, iOS and tvOS. Not maintained anymore. Superseded by FFmpegKit.
https://tanersener.github.io/mobile-ffmpeg
GNU General Public License v3.0
3.85k stars 787 forks source link

Other FFmpeg.execute() are automatically canceled when first execute is completed. #656

Closed Chickenchaser-Kwak closed 3 years ago

Chickenchaser-Kwak commented 3 years ago

Description In android, i run multiple coroutines in parallel and each coroutines are calling FFmpeg.execute command to compress mp4 video. If there is a single coroutine, It works fine. But if i run multiple tasks, as soon as first execution is complted(successfully), other executions are canceled. and log is below.

Expected behavior Parallel tasks are completed in parallel, and videos are compressed well.

Current behavior Coroutine that I used is like below.

scope.launch(Dispatcher.IO) {    
    // ... get inPath and outPath ..      
    val result = FFmpeg.execute("-y -i ${inPath} -s hd720 -crt 28 ${outPath}")  
    if(result != 0)  
        Config.printLastCommandOutput(Log.ERROR)  
    // ..  
}

I start a coroutine for each item of ViewPager

Logs Below logs are generated when first coroutine have been completed.

E/mobile-ffmpeg: ffmpeg version v4.4-dev-416 Copyright (c) 2000-2020 the FFmpeg developers built with Android (6454773 based on r365631c2) clang version 9.0.8 (https://android.googlesource.com/toolchain/llvm-project 98c855489587874b2a325e7a516b99d838599c6f) (based on LLVM 9.0.8svn) configuration: --cross-prefix=aarch64-linux-android- --sysroot=/files/android-sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/linux-x86_64/sysroot --prefix=/home/taner/Projects/mobile-ffmpeg/prebuilt/android-arm64/ffmpeg --pkg-config=/usr/bin/pkg-config --enable-version3 --arch=aarch64 --cpu=armv8-a --cc=aarch64-linux-android21-clang --cxx=aarch64-linux-android21-clang++ --extra-libs='-L/home/taner/Projects/mobile-ffmpeg/prebuilt/android-arm64/cpu-features/lib -lndk_compat' --target-os=android --enable-neon --enable-asm --enable-inline-asm --enable-cross-compile --enable-pic --enable-jni --enable-optimizations --enable-swscale --enable-shared --enable-v4l2-m2m --disable-outdev=fbdev --disable-indev=fbdev --enable-small --disable-openssl --disable-xmm-clobber-test --disable-debug --enable-lto --disable-neon-clobber-test --disable-programs --disable-postproc --disable-doc --disable-htmlpages --disable-manpages --disable-podpages --disable-txtpages --disable-static --disable-sndio --disable-schannel --disable-securetransport --disable-xlib --disable-cuda --disable-cuvid --disable-nvenc --disable-vaapi --disable-vdpau --disable-videotoolbox --disable-audiotoolbox --disable-appkit --disable-alsa --disable-cuda --disable-cuvid --disable-nvenc --disable-vaapi --disable-vdpau --enable-libx264 --enable-gpl --enable-libxvid --enable-gpl --enable-libx265 --enable-gpl --enable-libvidstab --enable-gpl --disable-sdl2 --enable-zlib --enable-mediacodec libavutil 56. 55.100 / 56. 55.100 libavcodec 58. 96.100 / 58. 96.100 libavformat 58. 48.100 / 58. 48.100 libavdevice 58. 11.101 / 58. 11.101 libavfilter 7. 87.100 / 7. 87.100 libswscale 5. 8.100 / 5. 8.100 libswresample 3. 8.100 / 3. 8.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/storage/emulated/0/DCIM/Camera/20200727_103152.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: isommp42 creation_time : 2020-07-27T01:31:56.000000Z location : +35.8920+128.6139/ location-eng : +35.8920+128.6139/ com.android.version: 9 Duration: 00:00:02.62, start: 0.000000, bitrate: 48878 kb/s Stream #0:0(eng): Video: h264 (avc1 / 0x31637661), yuv420p(tv, bt709), 3840x2160, 48620 kb/s, SAR 1:1 DAR 16:9, 29.76 fps, 30 tbr, 90k tbn, 180k tbc (default) Metadata: creation_time : 2020-07-27T01:31:56.000000Z handler_name : VideoHandle Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 256 kb/s (default) Metadata: creation_time : 2020-07-27T01:31:56.000000Z handler_name : SoundHandle 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 frame= 0 fps=0.0 q=0.0 size= 0kB time=-577014:32:22.77 bitrate= -0.0kbits/s speed=N/A
[graph 0 input from stream 0:0 @ 0x78821a8400] sws_param option is deprecated and ignored [libx264 @ 0x783f3cd700] using SAR=1/1 [libx264 @ 0x783f3cd700] using cpu capabilities: ARMv8 NEON [libx264 @ 0x783f3cd700] profile High, level 3.1, 4:2:0, 8-bit [libx264 @ 0x783f3cd700] 264 - core 160 - H.264/MPEG-4 AVC codec - Copyleft 2003-2020 - 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=12 lookahead_threads=2 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=25 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 '/data/user/0/team.doubledragon.missionchallenge/files/UJYylgddN2rfST12j7r1I_compressed.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: isommp42 com.android.version: 9 location : +35.8920+128.6139/ location-eng : +35.8920+128.6139/ encoder : Lavf58.48.100 Stream #0:0(eng): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], q=-1--1, 30 fps, 15360 tbn, 30 tbc (default) Metadata: creation_time : 2020-07-27T01:31:56.000000Z handler_name : VideoHandle encoder : Lavc58.96.100 libx264 Side data: cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: N/A Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default) Metadata: creation_time : 2020-07-27T01:31:56.000000Z handler_name : SoundHandle encoder : Lavc58.96.100 aac frame= 3 fps=0.0 q=0.0 size= 0kB time=00:00:01.00 bitrate= 0.4kbits/s dup=1 drop=0 speed=1.12x
frame= 14 fps=9.8 q=0.0 size= 0kB time=00:00:01.00 bitrate= 0.4kbits/s dup=1 drop=0 speed=0.705x
frame= 23 fps= 12 q=0.0 size= 0kB time=00:00:01.00 bitrate= 0.4kbits/s dup=1 drop=0 speed=0.521x
frame= 26 fps= 10 q=0.0 size= 0kB time=00:00:01.02 bitrate= 0.4kbits/s dup=1 drop=0 speed=0.412x
frame= 34 fps= 11 q=0.0 size= 0kB time=00:00:02.34 bitrate= 0.2kbits/s dup=1 drop=0 speed=0.774x
frame= 44 fps= 12 q=0.0 size= 0kB time=00:00:02.56 bitrate= 0.1kbits/s dup=1 drop=0 speed=0.721x
frame= 47 fps= 11 q=0.0 size= 0kB time=00:00:02.56 bitrate= 0.1kbits/s dup=1 drop=0 speed=0.619x
frame= 56 fps= 12 q=0.0 size= 0kB time=00:00:02.56 bitrate= 0.1kbits/s dup=1 drop=0 speed=0.548x
frame= 37 fps=3.1 q=-1.0 Lsize= 1410kB time=00:00:02.04 bitrate=5638.4kbits/s speed=0.169x
video:1358kB audio:49kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.202359% [libx264 @ 0x7854f7a000] frame I:1 Avg QP:23.85 size:199656 [libx264 @ 0x7854f7a000] frame P:9 Avg QP:25.42 size: 58736 [libx264 @ 0x7854f7a000] frame B:27 Avg QP:30.22 size: 24508 [libx264 @ 0x7854f7a000] consecutive B-frames: 2.7% 0.0% 0.0% 97.3% [libx264 @ 0x7854f7a000] mb I I16..4: 7.1% 55.2% 37.8% [libx264 @ 0x7854f7a000] mb P I16..4: 0.2% 6.4% 3.3% P16..4: 36.8% 15.1% 15.3% 0.0% 0.0% skip:22.8% [libx264 @ 0x7854f7a000] mb B I16..4: 0.0% 1.9% 1.7% B16..8: 27.4% 3.3% 1.9% direct: 5.3% skip:58.6% L0:43.1% L1:42.2% BI:14.7% [libx264 @ 0x7854f7a000] 8x8 transform intra:57.2% inter:42.2% [libx264 @ 0x7854f7a000] coded y,uvDC,uvAC intra: 95.5% 30.3% 3.6% inter: 20.4% 2.4% 0.0% [libx264 @ 0x7854f7a000] i16 v,h,dc,p: 13% 21% 39% 26% [libx264 @ 0x7854f7a000] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 8% 21% 29% 6% 7% 7% 7% 6% 9% [libx264 @ 0x7854f7a000] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 10% 14% 21% 8% 9% 10% 9% 8% 11% [libx264 @ 0x7854f7a000] i8c dc,h,v,p: 67% 27% 4% 1% [libx264 @ 0x7854f7a000] Weighted P-Frames: Y:0.0% UV:0.0% [libx264 @ 0x7854f7a000] ref P L0: 60.2% 15.0% 18.7% 6.1% [libx264 @ 0x7854f7a000] ref B L0: 81.3% 15.1% 3.6% [libx264 @ 0x7854f7a000] ref B L1: 92.0% 8.0% [libx264 @ 0x7854f7a000] kb/s:9016.22 [aac @ 0x78642d1d00] Qavg: 6336.895 frame= 58 fps= 10 q=29.0 size= 256kB time=00:00:02.56 bitrate= 819.4kbits/s dup=1 drop=0 speed=0.459x
frame= 58 fps=5.7 q=-1.0 Lsize= 3782kB time=00:00:02.60 bitrate=11904.7kbits/s dup=1 drop=0 speed=0.254x
video:3734kB audio:45kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.094771% [libx264 @ 0x783f3cd700] frame I:1 Avg QP:26.93 size:300250 [libx264 @ 0x783f3cd700] frame P:15 Avg QP:28.27 size:129506 [libx264 @ 0x783f3cd700] frame B:42 Avg QP:31.81 size: 37614 [libx264 @ 0x783f3cd700] consecutive B-frames: 3.4% 0.0% 0.0% 96.6% [libx264 @ 0x783f3cd700] mb I I16..4: 0.2% 69.0% 30.9% [libx264 @ 0x783f3cd700] mb P I16..4: 0.0% 6.5% 2.8% P16..4: 28.5% 30.5% 30.1% 0.0% 0.0% skip: 1.6% [libx264 @ 0x783f3cd700] mb B I16..4: 0.0% 0.3% 0.4% B16..8: 34.5% 17.8% 13.4% direct: 7.2% skip:26.5% L0:31.6% L1:34.5% BI:33.9% [libx264 @ 0x783f3cd700] 8x8 transform intra:66.7% inter:40.1% [libx264 @ 0x783f3cd700] coded y,uvDC,uvAC intra: 99.8% 89.1% 71.8% inter: 45.3% 11.2% 2.3% [libx264 @ 0x783f3cd700] i16 v,h,dc,p: 0% 83% 0% 17% [libx264 @ 0x783f3cd700] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 7% 10% 26% 10% 8% 7% 8% 10% 14% [libx264 @ 0x783f3cd700] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 9% 10% 15% 11% 10% 9% 10% 12% 14% [libx264 @ 0x783f3cd700] i8c dc,h,v,p: 54% 26% 10% 11% [libx264 @ 0x783f3cd700] Weighted P-Frames: Y:13.3% UV:0.0% [libx264 @ 0x783f3cd700] ref P L0: 64.8% 14.0% 12.0% 8.7% 0.4% [libx264 @ 0x783f3cd700] ref B L0: 88.6% 8.7% 2.7% [libx264 @ 0x783f3cd700] ref B L1: 94.2% 5.8% [libx264 @ 0x783f3cd700] kb/s:15817.71 [aac @ 0x786057ab00] Qavg: 1298.334 Exiting normally, received cancel signal.

Environment

tanersener commented 3 years ago

Exiting normally, received cancel signal.

Having this log line means either you're calling FFmpeg.cancel() or your app is sending one of the signals defined in Signal class. I'm not familiar with coroutines, maybe they are sending this signal at the end of processing. I suggest you have a look at coroutines documentation as well.

Chickenchaser-Kwak commented 3 years ago

In my code, There is no explicit FFmpeg.cancel() call.. As you said, I think It's something about how coroutine working. I will struggle to figure out why it happened.

buntupana commented 3 years ago

This is happening to me as well.

I launch a coroutine with FFmpeg.execute to encode a video and while is encoding I launch a another FFmpeg.execute without coroutine to get a thumbnail. When thumbnail process finished, the encoding process will be cancelled.

The only solution I found is executing the encoding process with FFmpeg.executeAsync inside the coroutine.

Several coroutines can be executed in the same thread, so maybe the problem is related to this. I guess FFmpeg will cancel all process in the same thread when it finished. Coroutines is the future of Android so it would be nice to have this library working along with them.

tanersener commented 3 years ago

This project will be retired. Please consider switching to FFmpegKit.