Get Video-Duration with avconv via Python

前端 未结 1 572
清歌不尽
清歌不尽 2021-01-28 01:37

I need to get the duration of an Video for an application for Django. So I\'ll have to do this in python. But I\'m really a beginner in this. So it would be nice, if you can hel

相关标签:
1条回答
  • 2021-01-28 01:48

    From python documentation:

    Warning

    Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

    So you should really user communicate for that:

    import subprocess
    task = subprocess.Popen("avconv -i video.mp4 2>&1 | grep Duration | cut -d ' ' -f 4 | sed -r 's/([^\.]*)\..*/\1/'", shell=True, stdout=subprocess.PIPE)
    time = task.communicate()[0]
    print time
    

    That way you can also catch stderr message, if any.

    0 讨论(0)
提交回复
热议问题