示例#1
0
文件: song.py 项目: shr1k/zmusic-ng
def song(id, ext):
    song = (
        db.session.query(
            Song.id, Song.filename, Song.length, Song.mimetype, Song.artist, Song.album, Song.title, Song.track
        )
        .filter(Song.id == id)
        .first()
    )
    if song == None or not os.path.exists(song.filename):
        return abort(404)
    headers = Headers()
    headers.add("X-Content-Duration", song.length)
    headers.add("X-Accel-Buffering", "no")
    range_header = request.headers.get("Range", None)
    if range_header is not None:
        range_header = range_header.split("-")
        try:
            range_header = intval(range_header[0])
        except:
            range_header = None
    if range_header is None or range_header == 0:
        db.session.add(Download(song, request))
        db.session.commit()

    ext = ext.lower()

    for extension, mimeset in mimetypes.items():
        if song.mimetype in mimeset and extension == ext:
            return send_file_partial(
                song.filename,
                mimetype=song.mimetype,
                attachment_filename=generate_download_filename(song, ext),
                headers=headers,
            )

    if ext not in ["ogg", "mp3", "flac", "wav", "webm"]:
        return abort(404)
    transcode_options = ["ffmpeg", "-loglevel", "quiet", "-i", song.filename, "-f", ext, "-y"]
    if ext == "ogg" or ext == "webm":
        transcode_options.extend(["-acodec", "libvorbis", "-aq", "5"])
    elif ext == "mp3":
        transcode_options.extend(["-ab", "160k"])
    transcode_options.append("-")
    return send_process(
        transcode_options,
        mimetype=mimetypes[ext][0],
        attachment_filename=generate_download_filename(song, ext),
        headers=headers,
    )
示例#2
0
文件: zip.py 项目: Hadryan/zmusic-ng
	def do_zip():
		central_directory = ""
		offset = 0
		count = 0
		for song in songs:
			if not os.path.exists(song.filename):
				continue
			count += 1
			filename, ext = os.path.splitext(song.filename)
			if ext[0] == '.' and len(ext) > 1:
				ext = ext[1:]
			filename = generate_download_filename(song, ext)
			file = open(song.filename, 'rb')
			yield "\x50\x4b\x03\x04"			#local file header signature
			yield "\x14\x00"				#version needed to extract
			yield "\x00\x00"				#general purpose bit flag
			yield "\x00\x00"				#compression method
			yield "\x00\x00"				#last mod file time
			yield "\x00\x00"				#last mod file date
			crc32 = 0
			while True:
				data = file.read(8192)
				if not data:
					break
				crc32 = crc32sum(data, crc32)
			file.seek(0)
			crc32 = pack("<I", crc32 & 0xffffffff)
			yield crc32					#crc-32
			size = os.path.getsize(song.filename)
			yield pack("<I", size)				#compressed size
			yield pack("<I", size)				#uncompressed size
			yield pack("<H", len(filename))			#file name length
			yield "\x00\x00"				#extra field length
			yield filename					#file name
			while True:
				data = file.read(8192)
				if not data:
					break
				yield data				#file data
			central_directory += "\x50\x4b\x01\x02"
			central_directory += "\x00\x00"			#version made by
			central_directory += "\x14\x00"			#version needed to extract
			central_directory += "\x00\x00"			#gen purpose bit flag
			central_directory += "\x00\x00"			#compression method
			central_directory += "\x00\x00"			#last mod file time
			central_directory += "\x00\x00"			#last mod file date
			central_directory += crc32			#crc-32
			central_directory += pack("<I", size)		#compressed filesize
			central_directory += pack("<I", size)		#uncompressed filesize
			central_directory += pack("<H", len(filename))	#length of filename
			central_directory += "\x00\x00"			#extra field length
			central_directory += "\x00\x00"			#file comment length
			central_directory += "\x00\x00"			#disk number start
			central_directory += "\x00\x00"			#internal file attributes
			central_directory += "\x20\x00\x00\x00"		#external file attributes - 'archive' bit set (32)
			central_directory += pack("<I", offset)		#relative offset of local header
			offset += 30 + len(filename) + size
			central_directory += filename
			file.close()
		yield central_directory					#central directory
		yield "\x50\x4b\x05\x06"				#end of central directory signature	
		yield "\x00\x00"					#number of this disk
		yield "\x00\x00"					#number of the disk with the start of the central directory
		yield pack("<H", count)					#number of entries on disk
		yield pack("<H", count) 				#number of entries
		yield pack("<I", len(central_directory))		#size of central directory
		yield pack("<I", offset)				#offset to start of central directory
		yield "\x00\x00"					#zip comment size
示例#3
0
文件: song.py 项目: Hadryan/zmusic-ng
def song(id, ext):
	song = db.session.query(Song.id, Song.filename, Song.length, Song.mimetype, Song.artist, Song.album, Song.title, Song.track).filter(Song.id == id).first()
	if song == None or not os.path.exists(song.filename):
		return abort(404)
	headers = Headers()
	headers.add("X-Content-Duration", song.length)
	headers.add("X-Accel-Buffering", "no")
	range_header = request.headers.get('Range', None)
	if range_header is not None:
		range_header = range_header.split("-")
		try:
			range_header = intval(range_header[0])
		except:
			range_header = None
	if range_header is None or range_header == 0:
		db.session.add(Download(song, request))
		db.session.commit()
	
	ext = ext.lower()

	for extension, mimeset in mimetypes.items():
		if song.mimetype in mimeset and extension == ext:
			return send_file_partial(song.filename, mimetype=song.mimetype, attachment_filename=generate_download_filename(song, ext), headers=headers)

	if ext not in [ "ogg", "mp3", "flac", "wav", "webm" ]:
		return abort(404)
	transcode_options = [ 'avconv', '-loglevel', 'quiet', '-i', song.filename, '-f', ext, '-y', '-fflags', 'nobuffer' ];
	if ext == "ogg" or ext == "webm":
		transcode_options.extend([ '-acodec', 'libvorbis', '-aq', '5' ])
	elif ext == "mp3":
		transcode_options.extend([ '-ab', '160k' ])
	transcode_options.append('-')
	return send_process(transcode_options, mimetype=mimetypes[ext][0], attachment_filename=generate_download_filename(song, ext), headers=headers)