{"id":69,"date":"2021-07-05T17:18:16","date_gmt":"2021-07-05T09:18:16","guid":{"rendered":"https:\/\/www.tysql.com\/?p=69"},"modified":"2021-07-05T17:18:16","modified_gmt":"2021-07-05T09:18:16","slug":"python%e8%8e%b7%e5%be%97%e5%9c%a8%e7%ba%bf%e8%a7%86%e9%a2%91%e6%97%b6%e9%95%bf","status":"publish","type":"post","link":"https:\/\/www.tysql.com\/index.php\/2021\/07\/05\/python%e8%8e%b7%e5%be%97%e5%9c%a8%e7%ba%bf%e8%a7%86%e9%a2%91%e6%97%b6%e9%95%bf\/","title":{"rendered":"python\u83b7\u5f97\u5728\u7ebf\u89c6\u9891\u65f6\u957f"},"content":{"rendered":"\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:false,&quot;languageLabel&quot;:false,&quot;fullScreenButton&quot;:false,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;apl&quot;,&quot;mime&quot;:&quot;text\/apl&quot;,&quot;theme&quot;:&quot;darcula&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;APL&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;apl&quot;}\"># coding:utf-8\n\nimport struct\nimport requests\n\nclass Mp4info:\n    def __init__(self, file):\n        self.file = file\n        self.seek = 0\n        self.duration = 0\n        self.s = requests.session()\n        self.timeout = 6\n        self.s.headers = {\n            'Connection': 'keep-alive',\n            'Accept': 'text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8',\n            'Accept-Encoding': 'gzip, deflate',\n            'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',\n            'User-Agent': 'Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/66.0.3359.139 Safari\/537.36'\n        }\n\n    # \u8bbe\u7f6e\u8bf7\u6c42\u5934  set request header\n    # \u4f20\u5165\u7684seek\u8868\u793a\u4ee3\u8868\u9700\u8981\u8df3\u8fc7\u7684\u5b57\u8282\u6570\u91cf  use seek to skip initial data\n    # \u5728\u8fd9\u91cc\u8fdb\u884c\u5224\u65ad\u662f\u4e3a\u4e86\u540e\u7eed\u83b7\u53d6\u89c6\u9891\u7684\u5bbd\u9ad8\u4fe1\u606f\u9884\u7559\u7684  the condition here is for reserving space for getting the media data\n    def _set_headers(self, seek, type):\n        if type in ['moov', 'duration']:\n            self.s.headers['Range'] = 'bytes={}-{}'.format(seek, seek + 7)\n\n    def _send_request(self):\n        try:\n            data = self.s.get(url=self.file, stream=True,\n                              timeout=self.timeout).raw.read()\n        except requests.Timeout:\n            raise '\u8fde\u63a5\u8d85\u65f6:\u8d85\u8fc76\u79d2(\u9ed8\u8ba4)\u670d\u52a1\u5668\u6ca1\u6709\u54cd\u5e94\u4efb\u4f55\u6570\u636e\uff01'  # timeout 6 seconds, the server fails to respond and assumes there is no data\n        return data\n\n    def _find_moov_request(self):\n        self._set_headers(self.seek, type='moov')\n        data = self._send_request()\n        size = int(struct.unpack('&gt;I', data[:4])[0])\n        flag = data[-4:].decode('ascii')\n        return size, flag\n\n    def _find_duration_request(self):\n        # 4+4\u662fmoov\u7684\u5927\u5c0f\u548c\u6807\u8bc6,\u8df3\u8fc720\u4e2a\u5b57\u7b26\uff0c\u76f4\u63a5\u8bfb\u5230time_scale\uff0cduration  # 4+4 is the first 8 characters denoting charset, skip the next 20 to time_scale and duration\n        self._set_headers(seek=self.seek+4+4+20, type='duration')\n        data = self._send_request()\n        time_scale = int(struct.unpack('&gt;I', data[:4])[0])\n        duration = int(struct.unpack('&gt;I', data[-4:])[0])\n        return time_scale, duration\n\n    def get_duration(self):\n        while True:\n            size, flag = self._find_moov_request()\n            if flag == 'moov':\n                time_scale, duration = self._find_duration_request()\n                self.duration = duration\/time_scale\n                return self.duration\n            else:\n                self.seek += size\n\n\nif __name__ == '__main__':\n    url = 'https:\/\/vd2.bdstatic.com\/mda-mfqszq3mzd845q23\/1080p\/cae_h264\/1624620686306577424\/mda-mfqszq3mzd845q23.mp4?v_from_s=hkapp-haokan-tucheng&amp;auth_key=1625478247-0-0-6baa0019344f9a31db76ddb4be3909e5&amp;bcevod_channel=searchbox_feed&amp;pd=1&amp;pt=3&amp;abtest=3000165_1'\n    file = Mp4info(url)\n    a = file.get_duration()\n    print(a)\n<\/pre><\/div>\n\n\n\n<p>\u539f\u6587\u94fe\u63a5: <a href=\"https:\/\/blog.csdn.net\/longjuanfengzc\/article\/details\/103006691\">https:\/\/blog.csdn.net\/longjuanfengzc\/article\/details\/103006691<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u94fe\u63a5: https:\/\/blog.csdn.net\/longjuanfengzc\/article\/deta &hellip; <a href=\"https:\/\/www.tysql.com\/index.php\/2021\/07\/05\/python%e8%8e%b7%e5%be%97%e5%9c%a8%e7%ba%bf%e8%a7%86%e9%a2%91%e6%97%b6%e9%95%bf\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">python\u83b7\u5f97\u5728\u7ebf\u89c6\u9891\u65f6\u957f<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-69","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/comments?post=69"}],"version-history":[{"count":1,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":70,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/posts\/69\/revisions\/70"}],"wp:attachment":[{"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/media?parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/categories?post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tysql.com\/index.php\/wp-json\/wp\/v2\/tags?post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}