I'm trying to use a VideoPlayer component, with a URL source and a RenderTexture as the target, to show a video in my Unity mobile game. The video is loaded and starts playing, however the resulting texture is only 1 color. The color does change every frame to something matching what the video would look like that frame, but it's just the 1 color. Audio is working fine. On the VideoPlayer component, the Aspect Ratio is set to "Fit Inside", but I have tried all options here with the same result. As for the RenderTexture, it's set to the same resolution as the input video, and the Color Format is set to RGB565 (which both Android and iOS should support according to SystemInfo.SupportRenderTextureFormat()). I'm all out of ideas, any help would be appreciated.
EDIT: A workaround could be using "material override" instead of rendering to a texture. This doesn't work though if you want to use the texture specifically instead of only showing the video on a material, plus the fact that Material Override doesn't support objects with multiple renderers/materials. Not really a fix, but a workaround for those who find this question before a solution has been found.
I just had this myself and fixed it.
In the Raw Image, search UV Rect and set its W and H to 1. I had changed that, which made it only sample 1 pixel.
Related
I am trying to use some video resources. And I wanna get rid of the background and change the color as I want.
I used VideoView for easy implementation. However, it gave black background. I don't want that. So, I am trying to use TextureVideoView.
implementation 'com.sprylab.android.texturevideoview:texturevideoview:1.2.1'
However, when I play the webm files, it seems it's not transparency.
What's the reason???
It's weird because I also exported and played a few clips to Webm with alpha channel. And it gives white or broken color background. (VideoView also gives gray background like this.)
Source Video:
https://simpl.info/videoalpha/video/dancer1.webm,
https://simpl.info/videoalpha/video/soccer1.webm
(It's totally fine here: https://simpl.info/videoalpha/)
The goal is to crop a preview on the surface for frames that come from Camera2 api capture session, but not to crop the video that will be created itself.
For example, I have a streaming resolution of 1920x1080 (16:9), and the screen size (just for instance) 2000x3000 (2:3 -> 6:9 or 16:24, lets pick the second variant for example), so I'd like to have my video in the original streaming resolution - 1920x1080, but my preview to fill all the available space without View resizing - so it should be 5333x3000 (just bumping the size up to fill the rectangle area using the same aspect ratio as the streaming resolution's one), and then to "cut" the "frame data that corresponds to this surface's resolution (which is, I suppose, 5333x2000)" to 2000x3000 (just remove (5333 - 2000) / 2 from both top and bottom).
Is it possible?
P.S.: the bad thing is that google sample for camera2 api resizes the view itself, and these "blank areas" are undesired for me. I haven't found anything that even closely matches my problem.
P.S.S.: AFAIU this SO solution crops the frame that comes from the camera itself, but I need my video to be in the original resolution.
If you're using a TextureView, you can probably adjust its transform matrix to scale up the preview (and cut off the edges in the process). Read the existing matrix, fix up the aspect ratio and scale it up, and then save the new matrix.
But please note that saving a different field of view than what you're showing to the user is probably going to get you negative reactions - please use the preview to frame what they want to record, and if what you're saving has extra stuff, the recorded video won't match expectations. Of course, maybe this isn't a concern for your use case.
I want to display a preview in fullscreen using camera2 api. The camera size I selected is 4160x3120 (16:9) and my screen is 1080x1920 (9:16). Therefore if I want the preview to be right scaled I have to crop the camera output :
Rect zoomCrop = new Rect(0, 0, 1755, 3120);
captureRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION, zoomCrop);
The cropped preview should be of size 1755x3120 which has a ratio of 9:16 as well. Still the preview is streched.
Question :
Am I using SCALER_CROP_REGION correctly ? If not, how am I supposed to use it ?
Thanks in advance
I've been struggling with the same sort of problem, as I want to implement a camera preview stream into a square TextureView.
Working with the SCALER_CROP_REGION seemed like the obvious place to resolve issues with incompatible aspect ratios, but I don't think that operation will ever actually change the aspect ratio of the result, hence it doesn't prevent stretching/warping behaviour. (the docs talk about this)
I've made progress by implementing a similar transform to the one in the Camera2Basic example project - see the function configureTransform in Camera2BasicFragment).
Using this sort of view-level scaling matrix, you can manage the scaling/cropping of the camera preview without altering the size of your view.
It sounds like you are getting a landscape image and displaying it in a portrait view - this means that you will have 'pillar box' lines above and below the image if you want to avoid distortion.
If you did want to fill the screen, and not have distortion or the pillar box lines then you would have to accept that part of your image will not be visible, as its outside the shape of the viewing area.
SCALER_CROP_REGION is tricky to understand without diagrams - I found the best explanation to be in the Andoir source documentation here: https://source.android.com/devices/camera/camera3_crop_reprocess
One diagram reproduced from that helps as an example here - the reason for highlight this is to show how the co-ordinates are being used. If you look at your crop region it will be biased towards the top left as you start at (0,0), which I am not sure is what you actually want.
I know that I can use RenderScript on Android to blur images, but does anybody know if I can apply the same to video views so that my complete video is gaussian blurred?
VideoView, which extends SurfaceView, does not utilize the drawing cache due to being hardware accelerated. This means you won't be able to get stills. I was forced to scrap the design I had using the paused video still.
Check out: VideoView getDrawingCache is returning black
Edit: As I look into this more, there might be a way through https://github.com/google/grafika, but I haven't seen anyone verify it as a performant workaround.
You should use a thumbnail of the video in front of it. You can then, blur the image using this lib: https://github.com/jrvansuita/GaussianBlur
Using an ImageView, I can set a square height and width (say 100dip x 100dip). Then using android:scaleType="centerCrop" gives me an image which is cropped to square regardless of aspect ratio.
Can we do this with a VideoView?
I've tried just setting a square height and width, but it just re-sizes to fill the square as best as it can while maintaining the aspect ratio, which I guess is completely expected.
It doesn't seem to have any scale or crop properties / methods unlike ImageView, but this in the VideoView documentation makes me think I'm missing something:
[VideoView] ...provides various display options
such as scaling and tinting.
Any ideas would be much appreciated.
You achive video crop effect using TextureView which require Android API 14 and hardware acceleration. I described it in my article here.
You can also use my TextureVideoView - custom view based on android TextureView which gives you ability to easy play and crop video. This very similar to ImageView#setScaleType
Layout
<com.dd.crop.TextureVideoView
android:id="#+id/cropTextureView"
android:layout_width="fill_parent"
android:layout_height="100dp"/>
Code
TextureVideoView cropTextureView = (TextureVideoView) findViewById(R.id.cropTextureView);
// Use `setScaleType` method to crop video
cropTextureView.setScaleType(TextureVideoView.ScaleType.TOP);
// Use `setDataSource` method to set data source, this could be url, assets folder or path
cropTextureView.setDataSource("http://www.w3schools.com/html/mov_bbb.mp4");
cropTextureView.play();
Hope this will help someone!
Source implies that it isn't natively supported in the VideoView view. You could copy the source and make it work though. :) Most likely by adding an else at line 255, modifying onMeasure to prevent it from expanding to meet the size, etc.
Edit:
Don't be surprised that the Reference documentation is slightly off or just plain wrong.