Ejemplo n.º 1
0
def test_check():
    assert FFmpegBackend.check() == []

    path = os.environ['PATH']
    os.environ['PATH'] = ''
    assert len(FFmpegBackend.check()) == 1
    os.environ['PATH'] = path
def test_check():
    field = Video._meta.get_field('file')
    assert field.check() == FFmpegBackend.check()

    path = os.environ['PATH']
    os.environ['PATH'] = ''
    assert field.check() == FFmpegBackend.check()
    os.environ['PATH'] = path
Ejemplo n.º 3
0
def ffmpeg():
    return FFmpegBackend()
Ejemplo n.º 4
0
def uploadVideo(request):
    if request.method == 'POST':
        if request.is_ajax():
            if request.POST.get('get_thumbnail'):
                video = Video.objects.get(
                    watch_id__exact=request.POST.get('watch_id'))
                response = {'thumbnails': []}
                for i in list(range(3)):
                    timestamp = min(((video.duration / 3) / 0.5) + i,
                                    video.duration - 0.5)
                    tmp_thumbnail = FFmpegBackend().get_thumbnail(
                        video_path=video.file.path, at_time=timestamp)
                    filename = 'thumbnail_{}_{}.jpg'.format(video.watch_id, i)
                    server_path = os.path.join(settings.MEDIA_ROOT, filename)
                    shutil.move(tmp_thumbnail, server_path)
                    url = settings.MEDIA_URL + filename
                    response['thumbnails'].append(url)
                return JsonResponse(response)
            else:
                form = VideoForm(request.POST, request.FILES)
                if not request.FILES.get('file').content_type.startswith(
                        'video/'):
                    return JsonResponse({'is_valid': False})
                if form.is_valid():
                    try:
                        video = form.save()
                    except FFmpegError as e:
                        os.remove(
                            os.path.join(settings.MEDIA_ROOT,
                                         form.files['file'].name))
                        return JsonResponse({'is_valid': False})
                    video.channel = Channel.objects.get(
                        owner__exact=request.user.id)
                    video.status = 'drafting'
                    video.save()

                    logger.info('VALIDATING MIME-TYPE: {}'.format(
                        video.file.path))
                    f = os.path.join(settings.MEDIA_ROOT, video.file.name)
                    if not magic.from_file(f, mime=True).startswith('video/'):
                        logger.info('FILE INVALID: {}'.format(video.file.path))
                        video.delete()
                    else:
                        logger.info('FILE VALID: {}'.format(video.file.path))

                    try:
                        Video.objects.get(pk=video.pk)
                        data = {
                            'is_valid': True,
                            'name': video.file.name,
                            'url': video.file.url,
                            'watch_id': video.watch_id
                        }
                    except Exception as e:
                        data = {'is_valid': False}
                else:
                    data = {'is_valid': False}
                return JsonResponse(data)
        else:
            video = Video.objects.get(
                watch_id__exact=request.POST.get('watch_id', ''))
            if request.POST.get('selected_thumbnail') == "custom":
                pass
            else:
                filename = os.path.basename(
                    request.POST.get('selected_thumbnail'))
                full_path = os.path.join(settings.MEDIA_ROOT, filename)
                f = open(full_path, 'rb')
                video.thumbnail.save(filename, ImageFile(f), save=False)
                f.close()
                for i in range(3):
                    name = '{}{}.jpg'.format(filename.split('.')[0][:-1], i)
                    path = os.path.join(settings.MEDIA_ROOT, name)
                    if os.path.isfile(path):
                        os.remove(path)
            video.title = request.POST.get('title')
            video.description = request.POST.get('description')
            video.status = 'public'
            video.category = Category.objects.get(
                pk=request.POST.get('category'))
            video.save()
            logger.info('START CONVERTING' + video.file.path)
            enqueue(tasks.convert_all_videos, video._meta.app_label,
                    video._meta.model_name, video.pk)
            return redirect("/")
    else:
        form = VideoForm()
        categories = Category.objects.all()
        return render(request, 'streamer/upload.html', {
            'form': form,
            'initial_upload': True,
            'categories': categories
        })