首页 > 解决方案 > /videos/create_video/ 的 TypeError 应为 str、bytes 或 os.PathLike 对象,而不是 int

问题描述

嘿伙计们,如何在 django 中创建视频文件以进行转码后提供正确的路径。我收到一个错误,指定路径中的错误。我对此很陌生,不知道如何提供正确的路径。这是我用于转码的代码。

def encode_video(video_id):
    try:
        video = VideoPost.objects.get(id = video_id)
        input_file_path = video.file.path
        input_file_name = video.title

        #get the filename (without extension)
        filename = os.path.basename(input_file_path)

        # path to the new file, change it according to where you want to put it
        output_file_name = os.path.join('videos', 'mp4', '{}.mp4'.format(video.file))
        output_file_path = os.path.join(settings.MEDIA_ROOT, 'videos', output_file_name)

        for i in range(1):
            subprocess.call([settings.FFMPEG_PATH, '-i', input_file_path, '-s', '{}'.format(16 /9), '-vcodec', 'mpeg4', '-acodec', 'libvo_aacenc', '-b', '10000k', '-pass', i, '-r', '30', output_file_path])

        video.file.name = output_file_name
        video.save(update_fields=['file'])
    except:
        raise ValidationError('Not converted')

任务.py

@task(name= 'task_video_encoding')
def task_video_encoding(video_id):
    logger.info('Video Processed Successfully')
    return encode_video(video_id)

模型.py

class VideoPost(models.Model):
    user                = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, blank=True, null=True)
    title               = models.TextField(max_length=1000)
    height              = models.PositiveIntegerField(editable=False, null=True, blank=True)
    width               = models.PositiveIntegerField( editable=False, null=True, blank=True)
    duration            = models.FloatField(editable=False, null=True, blank=True)
    post_date           = models.DateTimeField(auto_now_add=True, verbose_name="Date Posted")
    updated             = models.DateTimeField(auto_now_add=True, verbose_name="Date Updated")
    slug                = models.SlugField(blank=True, unique=True, max_length=255)
    file                = VideoField(width_field='width', height_field='height', duration_field='duration', upload_to='videos/')
    image               = models.ImageField(blank=True, upload_to='videos/thumbnails/', verbose_name='Thumbnail image')
    format_set          = GenericRelation(Format)

看法

class CreateVideoPostView(LoginRequiredMixin, BSModalCreateView):
    def get(self, *args, **kwargs):
        form = VideoPostForm()
        context = {
            'form':form,
        }
        return render(self.request, 'videos/video/create_video.html', context)

    def post(self, *args, **kwargs):
        form = VideoPostForm(self.request.POST or None, self.request.FILES or None)
        if form.is_valid():
            video = form.save(commit=False)
            video.user = self.request.user
            video.save()
            task_video_encoding(video.id)
            return redirect('videos:my_video_home')
        else:
            raise ValidationError('Check all form fields.')

追溯:

    C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\exception.py in inner
            response = get_response(request) …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py in _get_response
                response = self.process_exception_by_middleware(e, request) …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\core\handlers\base.py in _get_response
                response = wrapped_callback(request, *callback_args, **callback_kwargs) …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py in view
            return self.dispatch(request, *args, **kwargs) …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\contrib\auth\mixins.py in dispatch
        return super().dispatch(request, *args, **kwargs) …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\views\generic\base.py in dispatch
        return handler(request, *args, **kwargs) …
▶ Local vars
C:\danny\project\myproject\videos\views.py in post
            task_video_encoding(video.id) …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\celery\local.py in __call__
        return self._get_current_object()(*a, **kw) …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\site-packages\celery\app\task.py in __call__
            return self.run(*args, **kwargs) …
▶ Local vars
C:\danny\project\myproject\videos\tasks.py in task_video_encoding
    return encode_video(video_id) …
▶ Local vars
C:\danny\project\myproject\videos\encoding.py in encode_video
        subprocess.call([settings.VIDEO_ENCODING_FFMPEG_PATH, '-i', input_file_path, '-s', '{}'.format(16 /9), '-vcodec', 'mpeg4', '-acodec', 'libvo_aacenc', '-b', '10000k', '-pass', i, '-r', '30', output_file_path]) …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py in call
    with Popen(*popenargs, **kwargs) as p: …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py in __init__
            self._execute_child(args, executable, preexec_fn, close_fds, …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py in _execute_child
                args = list2cmdline(args) …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py in list2cmdline
    for arg in map(os.fsdecode, seq): …
▶ Local vars
C:\Users\danny\AppData\Local\Programs\Python\Python38-32\lib\os.py in fsdecode
        filename = fspath(filename)  # Does type-checking of `filename`. …
▶ Local vars

任何人都可以提供提示或正确的方法来实现这种转换吗?

谢谢

标签: djangodjango-modelsdjango-rest-frameworkdjango-formsdjango-views

解决方案


您在子流程调用中缺少 i


推荐阅读