import requests
import struct

class Mp4info:
	#获取video 信息
	def __init__(self, file):
		self.file = file
		self.seek = 0
		self.duration = 0
		self.s = requests.session()
		self.timeout = 6
		self.s.headers = {
			'Connection': 'keep-alive',
			'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
			'Accept-Encoding': 'gzip, deflate',
			'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
			'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'
		}

	# 设置请求头
	def _set_headers(self, seek):
		self.s.headers['Range'] = 'bytes={}-{}'.format(seek, seek + 7)

	def _send_request(self):
		try:
			data = self.s.get(url=self.file, stream=True,
							  timeout=self.timeout).raw.read()
		except requests.Timeout:
			raise self.file + '连接超时:超过6秒(默认)服务器没有响应任何数据！'
		return data

	def _find_moov_request(self):
		self._set_headers(self.seek)
		data = self._send_request()
		size = int(struct.unpack('>I', data[:4])[0])
		flag = data[-4:].decode('ascii')
		return size, flag

	def _find_duration_request(self):
		# 4+4是moov的大小和标识,跳过20个字符，直接读到time_scale，duration
		self._set_headers(seek=self.seek+4+4+20)
		data = self._send_request()
		time_scale = int(struct.unpack('>I', data[:4])[0])
		duration = int(struct.unpack('>I', data[-4:])[0])
		return time_scale, duration

	def get_duration(self):
		i=30
		while i:
			size, flag = self._find_moov_request()
			if flag == 'moov':
				time_scale, duration = self._find_duration_request()
				self.duration = duration/time_scale
				return self.duration
			else:
				self.seek += size
			i -=1
import requests
import struct

class Mp4info:
	#获取video 信息
	def __init__(self, file):
		self.file = file
		self.seek = 0
		self.duration = 0
		self.s = requests.session()
		self.timeout = 6
		self.s.headers = {
			'Connection': 'keep-alive',
			'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
			'Accept-Encoding': 'gzip, deflate',
			'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
			'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'
		}

	# 设置请求头
	def _set_headers(self, seek):
		self.s.headers['Range'] = 'bytes={}-{}'.format(seek, seek + 7)

	def _send_request(self):
		try:
			data = self.s.get(url=self.file, stream=True,
							  timeout=self.timeout).raw.read()
		except requests.Timeout:
			raise self.file + '连接超时:超过6秒(默认)服务器没有响应任何数据！'
		return data

	def _find_moov_request(self):
		self._set_headers(self.seek)
		data = self._send_request()
		size = int(struct.unpack('>I', data[:4])[0])
		flag = data[-4:].decode('ascii')
		return size, flag

	def _find_duration_request(self):
		# 4+4是moov的大小和标识,跳过20个字符，直接读到time_scale，duration
		self._set_headers(seek=self.seek+4+4+20)
		data = self._send_request()
		time_scale = int(struct.unpack('>I', data[:4])[0])
		duration = int(struct.unpack('>I', data[-4:])[0])
		return time_scale, duration

	def get_duration(self):
		i=30
		while i:
			size, flag = self._find_moov_request()
			if flag == 'moov':
				time_scale, duration = self._find_duration_request()
				self.duration = duration/time_scale
				return self.duration
			else:
				self.seek += size
			i -=1
#获取video 信息 # 设置请求头 # 4+4是moov的大小和标识,跳过20个字符，直接读到time_scale，duration