This question has been flagged
3 Replies
5461 Views

Hi,

In Odoo13 we have video preview widget(video_preview)​ to preview the video.But this is not available in Odoo12, is there any other way to preview the video like in Odoo12(with or without widget).

Thanks

Avatar
Discard
Best Answer

Hi Irfan,

By using following code you can create a new video preview widget in Odoo 12.

In .js file

odoo.define('website_sale.video_field_preview', function (require) {
"use strict";

var AbstractField = require('web.AbstractField');
var core = require('web.core');
var fieldRegistry = require('web.field_registry');

var QWeb = core.qweb;
/**
* Displays preview of the video showcasing product.
*/
var FieldVideoPreview = AbstractField.extend({
className: 'd-block o_field_video_preview',

_render: function () {
this.$el.html(QWeb.render('productVideo', {
embedCode: this.value,
}));
},
});

fieldRegistry.add('video_preview', FieldVideoPreview);

return FieldVideoPreview;

});

In .xml file

<?xml version="1.0" encoding="UTF-8"?>
<templates xml:space="preserve">
    <t t-name="productVideo">
        <div class="embed-responsive embed-responsive-16by9 " t-if="embedCode">
            <t t-raw="embedCode"/>
        </div>
    </t>
</templates>

After placing this code you can use the widget video_preview.

Hope it helps

Avatar
Discard
Author

Thanks for the response i will try this code

Best Answer

Hi Dunghn

In Odoo 16, you can use the video_preview widget in your custom module to display a preview of a video file. Here is an example of how you can use it:

from odoo import models, fields

class MyModel(models.Model):
_name = 'my.model'

video_file = fields.Binary(string='Video File')
video_preview = fields.Binary(string='Video Preview', compute='_compute_video_preview', store=True)

def _compute_video_preview(self):
for record in self:
if record.video_file:
video_preview = self.env['ir.attachment'].search([('res_model', '=', 'my.model'), ('res_id', '=', record.id), ('name', 'ilike', 'video_preview')], limit=1)
if not video_preview:
video_preview = self.env['ir.attachment'].create({
'name': 'video_preview',
'datas': self.env['video.converter'].convert(record.video_file),
'res_model': 'my.model',
'res_id': record.id,
})
record.video_preview = video_preview.datas if video_preview else False

In this example, we have a model called MyModel which has a video_file field that stores the video file as binary data, and a video_preview field that will display the video preview.

The _compute_video_preview method is used to convert the video file to a preview and store it in the video_preview field. It first searches for an existing video preview attachment for the current record, and if one is not found, it uses the video.converter service to convert the video file to a preview and creates a new attachment for the preview.

To display the video preview in the form view, you can use the widget attribute in the field definition:

xml
field name="video_preview" widget="video_preview" readonly="1"/



This will display the video preview in the form view when the record is opened. Note that the readonly attribute is set to 1 to prevent the user from editing the preview.



Avatar
Discard
Best Answer

Please tell me how to do with version 16

Avatar
Discard

@Bhavin Patel how do you have video.converter service