由于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
答案 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]