Skip to Content
Menu
This question has been flagged

I want to use the video preview widget in odoo 16.

I can't import video inside odoo.tools

Here is the code snippet by Bhavin Patel.


from odoo import models, 

fields, api from odoo.tools import video

class VideoExample(models.Model): 

_name = 'video.example' 

name = fields.Char(string='Name') 

video = fields.Binary(string='Video', attachment=True) 

video_preview = fields.Binary(string='Video Preview', compute='_compute_video_preview') 

@api.depends('video') 

def _compute_video_preview(self): 

for rec in self: 

rec.video_preview = video.Video.from_binary(rec.video).preview_image() if rec.video else None

Avatar
Discard

our code seems to be correct. It defines a new model VideoExample with three fields: name (a Char field), video (a Binary field for the video file), and video_preview (a computed Binary field for the preview image).

The video_preview field is computed based on the video field using a custom method _compute_video_preview, which generates the preview image for the video using the odoo.tools.video module.

The odoo.tools.video.Video.from_binary method is used to create a new Video object from the binary data of the video field. Then, the preview_image method is called on the Video object to generate the preview image, which is returned as a binary data and assigned to the video_preview field.

The if rec.video else None expression at the end of the line ensures that the video_preview field is set to None if there is no video file present. This is a good practice to avoid potential errors and unexpected behavior.

Overall, this code should work correctly to generate video preview images for the VideoExample model based on the uploaded video files.

Best Answer

In Odoo 16, you can use the video preview widget to display a preview image of a video file, along with a play button that allows the user to play the video. Here's how you can use the video preview widget in your custom module:

  1. Add a new field to your model that represents the video file:
Python:

In this example, we're adding a new Binary field for the video file, as well as a Char field for the filename and a computed Binary field for the video preview image. The video_preview field is computed based on the video_file field using a custom method _compute_video_preview, which generates the preview image for the video.

  1. Add a new view for your model that includes the video_preview field and uses the video preview widget:
    XML:

In this example, we're adding a new view for the form of the MyModel model that includes a tag with two tags. The first tag is for the video_preview field, and uses the video_preview widget to display the preview image and play button for the video. We're also passing an options dictionary that includes the filename field (video_filename) to use for the video. The second tag is for the video_file field, and includes the filename attribute to use for the filename.

  1. Implement the _generate_video_preview method to generate the preview image for the video. You can use a third-party library such as ffmpeg or moviepy to extract a frame from the video and save it as an image. Here's an example implementation using ffmpeg:
python:

In this example, we're using ffmpeg

Avatar
Discard
Related Posts Replies Views Activity
3
Apr 23
6244
2
Apr 23
3431
1
Apr 24
1877
1
May 24
1412
0
Nov 23
667