Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scoped Storage - Could not open file #39

Closed
blue-peacock opened this issue May 3, 2021 · 8 comments
Closed

Scoped Storage - Could not open file #39

blue-peacock opened this issue May 3, 2021 · 8 comments
Assignees
Labels
android Affect Android platform enhancement New feature or request fixed-in-v4.5 Fixed in release v4.5 library Affects the library saf Issue about storage access framework v4.4 Affects v4.4 release

Comments

@blue-peacock
Copy link

Description
I'm trying to write a ffmpeg generated output image to the MediaStore and not to an app-specific-directory on Android 10 using the code below. In this case I'm always getting an error Could not open file : saf:173/image.jpg av_interleaved_write_frame(): I/O error
Using an app-specific-directory and not the MediaStore as output works just fine.

Expected behavior
Writing the image to MediaStore successfully.

Current behavior
An empty file image file gets generated in the MediaStore. Ffmpeg-kit error is:
Could not open file : saf:173/image.jpg av_interleaved_write_frame(): I/O error

To Reproduce
The following Kotlin code is used to generate the output safUri used for the ffmpeg command.

        val filePrefix = "image"
        val fileExtn = ".jpg"
        val contentValues = ContentValues()
        contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/Test")
        contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, filePrefix + fileExtn)
        contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
        val uri = context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)

        val safUri = getSafParameterForWrite(context, uri)

Logs
2021-05-03 22:22:24.125 27125-28059/com.app.name I/ffmpeg-kit: ffmpeg version v4.4-dev-3015-gc0d0b1c4f6
2021-05-03 22:22:24.125 27125-28059/com.app.name I/ffmpeg-kit: Copyright (c) 2000-2021 the FFmpeg developers
2021-05-03 22:22:24.126 27125-28059/com.app.name I/ffmpeg-kit: built with Android (6875598, based on r399163b) clang version 11.0.5 (https://android.googlesource.com/toolchain/llvm-project 87f1315dfbea7c137aa2e6d362dbb457e388158d)
2021-05-03 22:22:24.126 27125-28059/com.app.name I/ffmpeg-kit: configuration: --cross-prefix=aarch64-linux-android- --sysroot=/files/android-sdk/ndk/22.0.7026061/toolchains/llvm/prebuilt/linux-x86_64/sysroot --prefix=/storage/dark/projects/ffmpeg-kit/prebuilt/android-arm64-lts/ffmpeg --pkg-config=/usr/bin/pkg-config --enable-version3 --arch=aarch64 --cpu=armv8-a --cc=aarch64-linux-android21-clang --cxx=aarch64-linux-android21-clang++ --ranlib=aarch64-linux-android-ranlib --strip=aarch64-linux-android-strip --nm=aarch64-linux-android-nm --extra-libs='-L/storage/dark/projects/ffmpeg-kit/prebuilt/android-arm64-lts/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 --disable-static --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-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-libfontconfig --enable-libfreetype --enable-libfribidi --enable-gmp --enable-gnutls --enable-libmp3lame --enable-libass --enable-iconv --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libxml2 --enable-libopencore-amrnb --enable-libshine --enable-libspeex --enable-libdav1d --enable-libkvazaar --enable-libilbc --enable-libopus --enable-libsnappy --enable-libsoxr --enable-libtwolame --enable-libvo-amrwbenc --enable-zlib --enable-mediacodec
2021-05-03 22:22:24.126 27125-28059/com.app.name I/ffmpeg-kit: libavutil 56. 65.100 / 56. 65.100
2021-05-03 22:22:24.126 27125-28059/com.app.name I/ffmpeg-kit: libavcodec 58.123.100 / 58.123.100
2021-05-03 22:22:24.126 27125-28059/com.app.name I/ffmpeg-kit: libavformat 58. 67.100 / 58. 67.100
2021-05-03 22:22:24.126 27125-28059/com.app.name I/ffmpeg-kit: libavdevice 58. 12.100 / 58. 12.100
2021-05-03 22:22:24.126 27125-28059/com.app.name I/ffmpeg-kit: libavfilter 7.106.100 / 7.106.100
2021-05-03 22:22:24.126 27125-28059/com.app.name I/ffmpeg-kit: libswscale 5. 8.100 / 5. 8.100
2021-05-03 22:22:24.126 27125-28059/com.app.name I/ffmpeg-kit: libswresample 3. 8.100 / 3. 8.100
2021-05-03 22:22:24.152 27125-28059/com.app.name I/ffmpeg-kit: Input #0, image2, from 'file:///storage/emulated/0/Android/data/com.app.name/files/Pictures/03-05-2021-22-22-22.tmp.jpg':
2021-05-03 22:22:24.152 27125-28059/com.app.name I/ffmpeg-kit: Duration:
2021-05-03 22:22:24.153 27125-28059/com.app.name I/ffmpeg-kit: 00:00:00.04
2021-05-03 22:22:24.153 27125-28059/com.app.name I/ffmpeg-kit: , start:
2021-05-03 22:22:24.153 27125-28059/com.app.name I/ffmpeg-kit: 0.000000
2021-05-03 22:22:24.154 27125-28059/com.app.name I/ffmpeg-kit: , bitrate:
2021-05-03 22:22:24.154 27125-28059/com.app.name I/ffmpeg-kit: 1123439 kb/s
2021-05-03 22:22:24.155 27125-28059/com.app.name I/ffmpeg-kit: Stream #0:0
2021-05-03 22:22:24.155 27125-28059/com.app.name I/ffmpeg-kit: : Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 4000x2250
2021-05-03 22:22:24.156 27125-28059/com.app.name I/ffmpeg-kit: ,
2021-05-03 22:22:24.156 27125-28059/com.app.name I/ffmpeg-kit: 25 fps,
2021-05-03 22:22:24.156 27125-28059/com.app.name I/ffmpeg-kit: 25 tbr,
2021-05-03 22:22:24.156 27125-28059/com.app.name I/ffmpeg-kit: 25 tbn,
2021-05-03 22:22:24.156 27125-28059/com.app.name I/ffmpeg-kit: 25 tbc
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: Input #1, png_pipe, from '/data/user/0/com.app.name/files/OverlayImg.png':
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: Duration:
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: N/A
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: , bitrate:
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: N/A
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: Stream #1:0
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: : Video: png, rgba(pc), 1080x2021
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: ,
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: 25 fps,
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: 25 tbr,
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: 25 tbn,
2021-05-03 22:22:24.195 27125-28059/com.app.name I/ffmpeg-kit: 25 tbc
2021-05-03 22:22:24.212 27125-28059/com.app.name I/ffmpeg-kit: Stream mapping:
2021-05-03 22:22:24.212 27125-28059/com.app.name I/ffmpeg-kit: Stream #0:0 (mjpeg) -> transpose
2021-05-03 22:22:24.213 27125-28059/com.app.name I/ffmpeg-kit: Stream #1:0 (png) -> scale
2021-05-03 22:22:24.213 27125-28059/com.app.name I/ffmpeg-kit: drawtext
2021-05-03 22:22:24.214 27125-28059/com.app.name I/ffmpeg-kit: -> Stream #0:0 (mjpeg)
2021-05-03 22:22:24.214 27125-28059/com.app.name I/ffmpeg-kit: Press [q] to stop, [?] for help
2021-05-03 22:22:24.592 27125-28059/com.app.name I/ffmpeg-kit: Output #0, image2, to 'saf:163/image.jpg':
2021-05-03 22:22:24.592 27125-28059/com.app.name I/ffmpeg-kit: Metadata:
2021-05-03 22:22:24.592 27125-28059/com.app.name I/ffmpeg-kit: encoder :
2021-05-03 22:22:24.593 27125-28059/com.app.name I/ffmpeg-kit: Lavf58.67.100
2021-05-03 22:22:24.593 27125-28059/com.app.name I/ffmpeg-kit: Stream #0:0
2021-05-03 22:22:24.593 27125-28059/com.app.name I/ffmpeg-kit: : Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown, progressive), 2250x4000, q=2-31, 200 kb/s
2021-05-03 22:22:24.594 27125-28059/com.app.name I/ffmpeg-kit: ,
2021-05-03 22:22:24.594 27125-28059/com.app.name I/ffmpeg-kit: 25 fps,
2021-05-03 22:22:24.594 27125-28059/com.app.name I/ffmpeg-kit: 25 tbn
2021-05-03 22:22:24.595 27125-28059/com.app.name I/ffmpeg-kit: (default)
2021-05-03 22:22:24.595 27125-28059/com.app.name I/ffmpeg-kit: Metadata:
2021-05-03 22:22:24.596 27125-28059/com.app.name I/ffmpeg-kit: encoder :
2021-05-03 22:22:24.596 27125-28059/com.app.name I/ffmpeg-kit: Lavc58.123.100 mjpeg
2021-05-03 22:22:24.596 27125-28059/com.app.name I/ffmpeg-kit: Side data:
2021-05-03 22:22:24.596 27125-28059/com.app.name I/ffmpeg-kit:
2021-05-03 22:22:24.604 27125-28059/com.app.name I/ffmpeg-kit: cpb:
2021-05-03 22:22:24.604 27125-28059/com.app.name I/ffmpeg-kit: bitrate max/min/avg: 0/0/200000 buffer size: 0
2021-05-03 22:22:24.604 27125-28059/com.app.name I/ffmpeg-kit: vbv_delay: N/A
2021-05-03 22:22:24.605 27125-28059/com.app.name I/ffmpeg-kit: frame= 1 fps=0.0 q=0.0 size=N/A time=00:00:00.00 bitrate=N/A speed= 0x
2021-05-03 22:22:24.929 27125-28059/com.app.name E/ffmpeg-kit: [image2 @ 0x7e88243000] Could not open file : saf:163/image.jpg
2021-05-03 22:22:24.930 27125-28059/com.app.name E/ffmpeg-kit: av_interleaved_write_frame(): I/O error
2021-05-03 22:22:24.932 27125-28059/com.app.name I/ffmpeg-kit: frame= 1 fps=0.0 q=0.0 Lsize=N/A time=00:00:00.04 bitrate=N/A speed=0.0557x
2021-05-03 22:22:24.934 27125-28059/com.app.name I/ffmpeg-kit: video:1144kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead:
2021-05-03 22:22:24.934 27125-28059/com.app.name I/ffmpeg-kit: unknown
2021-05-03 22:22:24.940 27125-28056/com.app.name D/MyFfmpegHelper: Command failed with state COMPLETED and rc 1.null
2021-05-03 22:22:24.944 27125-28059/com.app.name I/ffmpeg-kit: Conversion failed!

Environment

  • Android10
  • ffmpeg-kit: 4.4.LTS
  • Android Studio: 4.1.2
@tanersener tanersener added needs-analysis We don't know that this is. It must be investigated further android Affect Android platform library Affects the library saf Issue about storage access framework question Further information is requested and removed needs-analysis We don't know that this is. It must be investigated further labels May 4, 2021
@tanersener
Copy link
Collaborator

tanersener commented May 6, 2021

I tested your code using test applications under the ffmpeg-kit-test repository but didn't get any errors. I suggest doing the same and testing your issue with them. There is an SAF tab inside those apps. You can modify and use it to reproduce this issue.

@tanersener tanersener added the v4.4 Affects v4.4 release label May 6, 2021
@blue-peacock
Copy link
Author

Have you tried with an image?
When I try to create a video it works without an error using the following code:

                String filePrefix = "video";
                String fileExtn = ".mp4";
                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Movies/Test");
                contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, filePrefix + fileExtn);
                contentValues.put(MediaStore.Images.Media.MIME_TYPE, "video/mp4");
                outUri = requireContext().getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);

With the following code to create an image I'm able to reproduce the Could not open file error in your SAF Test application:

                String filePrefix = "image";
                String fileExtn = ".jpeg";
                ContentValues contentValues = new ContentValues();
                contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/Test");
                contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, filePrefix + fileExtn);
                contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
                outUri = requireContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

I was using the following ffmpeg command for testing the video and image code. In the video case it works.

            final String ffmpegCommand = "-i " + image1File.getAbsolutePath() + " -i " + image2File.getAbsolutePath() + " -filter_complex \"[0]transpose=1[a]; [1]scale=100:100[2]; [a][2]overlay=300:300[b]; " +
                    "[b]drawtext=fontfile=/system/fonts/DroidSans.ttf: text='Text': fontcolor=black: fontsize=100: x=100: y=90: box=1: boxcolor=white@0.5: boxborderw=20:\" -qscale:v 0 " + videoPath;

@tanersener tanersener self-assigned this May 7, 2021
@tanersener tanersener added enhancement New feature or request and removed question Further information is requested labels May 7, 2021
@tanersener
Copy link
Collaborator

It seems like this case is not handled by the SAF support implemented in ffmpeg-kit. As far as I understand, your command uses image2 muxer to encode the output jpeg file. But, image2 muxer doesn't know how to open those SAF urls. That's why your command fails.

SAF wrapper approach taken by ffmpeg-kit covers most of the cases. But it doesn't cover this one. And, it is not easy to use the same approach to handle your case. Because image2 muxer is an ffmpeg module. And, modifying its behaviour means modifying ffmpeg source code. Which is something we avoid.

We need to revisit the design of SAF support in ffmpeg-kit to handle this case.

@blue-peacock
Copy link
Author

Ok thanks. Interesting that the same command works in the video case but not in the image case. So there is no other (SAF compatible) way to overlay 2 images than this command?

@tanersener
Copy link
Collaborator

Most probably no. But I'm not % 100 sure.

@abkoradiya
Copy link

@tanersener Is this issue fixed?

@tanersener
Copy link
Collaborator

I fixed it on the development branch.

@tanersener
Copy link
Collaborator

Fixed in v4.5 release.

@tanersener tanersener added fixed-in-v4.5 Fixed in release v4.5 and removed fixed-on-development Fixed on the development branch. Not released yet. labels Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Affect Android platform enhancement New feature or request fixed-in-v4.5 Fixed in release v4.5 library Affects the library saf Issue about storage access framework v4.4 Affects v4.4 release
Projects
None yet
Development

No branches or pull requests

3 participants