Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import wagtailvideos.models
class Migration(migrations.Migration):
dependencies = [
('wagtailvideos', '0002_auto_20160321_1610'),
]
operations = [
migrations.CreateModel(
name='VideoTranscode',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('media_format', enumchoicefield.fields.EnumChoiceField(enum_class=wagtailvideos.models.MediaFormats, max_length=4)),
('processing', models.BooleanField(default=False)),
('file', models.FileField(null=True, upload_to=wagtailvideos.models.get_upload_to, verbose_name='file')),
('video', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='transcodes', to='wagtailvideos.Video')),
],
),
migrations.AlterUniqueTogether(
name='videotranscode',
unique_together=set([('video', 'media_format')]),
),
if has_changed or not filled_out:
with get_local_file(instance.file) as file_path:
if has_changed or instance.thumbnail is None:
instance.thumbnail = ffmpeg.get_thumbnail(file_path)
if has_changed or instance.duration is None:
instance.duration = ffmpeg.get_duration(file_path)
instance.file_size = instance.file.size
instance._from_signal = True
instance.save()
del instance._from_signal
class AbstractVideoTranscode(models.Model):
media_format = EnumChoiceField(MediaFormats)
quality = EnumChoiceField(VideoQuality, default=VideoQuality.default)
processing = models.BooleanField(default=False)
file = models.FileField(null=True, blank=True, verbose_name=_('file'),
upload_to=get_upload_to)
error_message = models.TextField(blank=True)
@property
def url(self):
return self.file.url
def get_upload_to(self, filename):
folder_name = 'video_transcodes'
filename = self.file.field.storage.get_valid_name(filename)
return os.path.join(folder_name, filename)
class Meta:
def get_quality_param(self, quality):
if self is MediaFormats.webm:
return {
VideoQuality.lowest: '50',
VideoQuality.default: '22',
VideoQuality.highest: '4'
}[quality]
elif self is MediaFormats.mp4:
return {
VideoQuality.lowest: '28',
VideoQuality.default: '24',
VideoQuality.highest: '18'
}[quality]
elif self is MediaFormats.ogg:
return {
VideoQuality.lowest: '5',
VideoQuality.default: '7',
VideoQuality.highest: '9'
}[quality]
model,
form=BaseVideoForm,
fields=fields,
formfield_callback=formfield_for_dbfield,
# set the 'file' widget to a FileInput rather than the default ClearableFileInput
# so that when editing, we don't get the 'currently: ...' banner which is
# a bit pointless here
widgets={
'tags': widgets.AdminTagWidget,
'file': forms.FileInput(),
'thumbnail': forms.FileInput(),
})
class VideoTranscodeAdminForm(forms.Form):
media_format = EnumField(MediaFormats)
quality = EnumField(VideoQuality)
def __init__(self, video, data=None, **kwargs):
super(VideoTranscodeAdminForm, self).__init__(data=data, **kwargs)
self.video = video
def save(self):
media_format = self.cleaned_data['media_format']
quality = self.cleaned_data['quality']
self.video.do_transcode(media_format, quality)
GroupVideoPermissionFormSet = collection_member_permission_formset_factory(
Video,
[
('add_video', _("Add"), _("Add/edit images you own")),
def get_quality_param(self, quality):
if self is MediaFormats.webm:
return {
VideoQuality.lowest: '50',
VideoQuality.default: '22',
VideoQuality.highest: '4'
}[quality]
elif self is MediaFormats.mp4:
return {
VideoQuality.lowest: '28',
VideoQuality.default: '24',
VideoQuality.highest: '18'
}[quality]
elif self is MediaFormats.ogg:
return {
VideoQuality.lowest: '5',
VideoQuality.default: '7',
VideoQuality.highest: '9'
}[quality]
subprocess.check_output(args + [
'-codec:v', 'libtheora',
'-qscale:v', quality_param,
'-codec:a', 'libvorbis',
'-qscale:a', '5',
output_file,
], stdin=FNULL, stderr=subprocess.STDOUT)
elif media_format is MediaFormats.mp4:
subprocess.check_output(args + [
'-codec:v', 'libx264',
'-preset', 'slow', # TODO Checkout other presets
'-crf', quality_param,
'-codec:a', 'copy',
output_file,
], stdin=FNULL, stderr=subprocess.STDOUT)
elif media_format is MediaFormats.webm:
subprocess.check_output(args + [
'-codec:v', 'libvpx',
'-crf', quality_param,
'-codec:a', 'libvorbis',
output_file,
], stdin=FNULL, stderr=subprocess.STDOUT)
self.transcode.file = ContentFile(
open(output_file, 'rb').read(), transcode_name)
self.transcode.error_message = ''
except subprocess.CalledProcessError as error:
self.transcode.error_message = error.output
finally:
self.transcode.processing = False
self.transcode.save()
shutil.rmtree(output_dir, ignore_errors=True)