将ffprobe与Django一起使用时出错-Python2

时间:2018-06-22 11:19:58

标签: python django ffprobe

由于ffprobe,我使用Django和this instruction来获得视频的时长,但是它存在一些问题。希望你们能帮助我修复它。

我的代码:

==

我得到的错误:

{} == {} // false

跟踪错误:

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

1 个答案:

答案 0 :(得分:1)

通过在encoding中设置subprocess.Popen()参数,尝试将数据解码为Unicode字符串。

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

相反,我们可以检查是否b"Duration" in x因为subprocess返回bytes

result = [x for x in result.stdout.readlines() if b"Duration" in x]