def edit(request, video_id): Video = get_video_model() VideoForm = get_video_form(Video) video = get_object_or_404(Video, id=video_id) if request.POST: original_file = video.file form = VideoForm(request.POST, request.FILES, instance=video) if form.is_valid(): if 'file' in form.changed_data: # if providing a new video file, delete the old one and all renditions. # NB Doing this via original_file.delete() clears the file field, # which definitely isn't what we want... original_file.storage.delete(original_file.name) # Set new video file size video.file_size = video.file.size video = form.save() video.save() # Reindex the video to make sure all tags are indexed for backend in get_search_backends(): backend.add(video) messages.success(request, _("Video '{0}' updated.").format(video.title)) else: messages.error(request, _("The video could not be saved due to errors.")) else: form = VideoForm(instance=video) if not video._meta.get_field('file').storage.exists(video.file.name): # Give error if video file doesn't exist messages.error(request, _( "The source video file could not be found. Please change the source or delete the video." ).format(video.title), buttons=[ messages.button(reverse('wagtailvideos:delete', args=(video.id,)), _('Delete')) ]) if is_modeladmin_installed(): url_helper = AdminURLHelper(Video.get_track_listing_model()) if hasattr(video, 'track_listing'): action_url = url_helper.get_action_url('edit', instance_pk=video.track_listing.pk) else: action_url = url_helper.create_url else: action_url = '' return render(request, "wagtailvideos/videos/edit.html", { 'video': video, 'form': form, 'filesize': video.get_file_size(), 'can_transcode': ffmpeg.installed(), 'transcodes': video.transcodes.all(), 'transcode_form': VideoTranscodeAdminForm(video=video), 'tracks_action_url': action_url, 'user_can_delete': permission_policy.user_has_permission_for_instance(request.user, 'delete', video) })
def ffmpeg_check(app_configs, **kwargs): messages = [] if not ffmpeg.installed(): messages.append( Warning( 'ffmpeg could not be found on your system. Transcoding will be disabled', hint=None, id='wagtailvideos.W001', )) return messages
def schedule_default_transcode(object_pk, *args): Video = apps.get_model(app_label="wagtailvideos", model_name="Video") instance = Video.objects.get(pk=object_pk) log.debug('transcoding video for %s', instance) if not ffmpeg.installed(): raise ImproperlyConfigured( "ffmpeg could not be found on your system. Transcoding will be disabled" ) transcode, created = instance.transcodes.get_or_create() if transcode.processing is False: transcode.processing = True transcode.error_message = '' # Lock the transcode model transcode.save(update_fields=['processing', 'error_message']) transcode.run_transcoding()
def get_video_metadata(object_pk, *args): Video = apps.get_model(app_label="wagtailvideos", model_name="Video") instance = Video.objects.get(pk=object_pk) log.debug('getting video metadata for %s', instance) if not ffmpeg.installed(): raise ImproperlyConfigured( "ffmpeg could not be found on your system. Transcoding will be disabled" ) with get_local_file(instance.file) as file_path: instance.thumbnail = ffmpeg.get_thumbnail(file_path) instance.duration = ffmpeg.get_duration(file_path) instance.file_size = instance.file.size instance.save()
def video_saved(sender, instance, **kwargs): if not ffmpeg.installed(): return if hasattr(instance, '_from_signal'): return has_changed = instance._initial_file is not instance.file filled_out = instance.thumbnail is not None and instance.duration is not None 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
def video_saved(sender, instance, **kwargs): if not ffmpeg.installed(): return if hasattr(instance, '_from_signal'): return has_changed = instance._initial_file is not instance.file filled_out = instance.thumbnail is not None and instance.duration is not None 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