0

I use Django and ffprobe to get duration of video thanks to this instruction but it have some issues. What can I try to fix it?

My code:

def getLength(filename):
    result = subprocess.Popen(['ffprobe', filename], stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
    return [x for x in result.stdout.readlines() if "Duration" in x]

class VideoListSerializer(ModelSerializer):
    class Meta:
        model = Video
        fields = [
            'id',
            'user',
            'video_file',
            'video_name',
        ]

    def get_duration(self, obj):
        // print video_file --> '/users/2/videos/9.mp4'
        result = getLength(obj.video_file)
        return result

Error I get:

CreateProcess() argument 2 must be string without null bytes or None, not str

Traceback error:

Traceback:

File "C:\Python27\lib\site-packages\django\core\handlers\exception.py" in inner
  41.             response = get_response(request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python27\lib\site-packages\django\views\decorators\csrf.py" in wrapped_view
  58.         return view_func(*args, **kwargs)

File "C:\Python27\lib\site-packages\django\views\generic\base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  489.             response = self.handle_exception(exc)

File "C:\Python27\lib\site-packages\rest_framework\views.py" in handle_exception
  449.             self.raise_uncaught_exception(exc)

File "C:\Python27\lib\site-packages\rest_framework\views.py" in dispatch
  486.             response = handler(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\rest_framework\generics.py" in get
  201.         return self.list(request, *args, **kwargs)

File "C:\Python27\lib\site-packages\rest_framework\mixins.py" in list
  45.             return self.get_paginated_response(serializer.data)

File "C:\Python27\lib\site-packages\rest_framework\serializers.py" in data
  739.         ret = super(ListSerializer, self).data

File "C:\Python27\lib\site-packages\rest_framework\serializers.py" in data
  263.                 self._data = self.to_representation(self.instance)

File "C:\Python27\lib\site-packages\rest_framework\serializers.py" in to_representation
  657.             self.child.to_representation(item) for item in iterable

File "C:\Python27\lib\site-packages\rest_framework\serializers.py" in to_representation
  501.                 ret[field.field_name] = field.to_representation(attribute)

File "C:\Python27\lib\site-packages\rest_framework\fields.py" in to_representation
  1755.         return method(value)

File "C:\Users\User\Desktop\Feed\backend\api\media\video.py" in get_duration
  61.         result = getLength(obj.video_file)

File "C:\Users\User\Desktop\Feed\backend\api\media\video.py" in getLength
  33.     result = subprocess.Popen(['ffprobe', filename], stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

File "C:\Python27\lib\subprocess.py" in __init__
  390.                                 errread, errwrite)

File "C:\Python27\lib\subprocess.py" in _execute_child
  640.                                          startupinfo)

Exception Type: TypeError at /api/v1/media/video/feed/
Exception Value: CreateProcess() argument 2 must be string without null bytes or None, not str
halfer
  • 19,824
  • 17
  • 99
  • 186
KitKit
  • 8,549
  • 12
  • 56
  • 82

1 Answers1

1

Try to decode the data to Unicode strings by setting the encoding argument in subprocess.Popen().

result = subprocess.Popen(
    ["ffprobe", filename],
    stdout=subprocess.PIPE, stderr = subprocess.STDOUT,
    encoding='utf-8' # here
)

Instead of this we can check if b"Duration" in x because subprocess return bytes

result = [x for x in result.stdout.readlines() if b"Duration" in x]
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38