Compare commits
10 Commits
5b05972d40
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f9c3ea08b | ||
|
|
1e6167a1ae | ||
|
|
31f8cf2047 | ||
|
|
f5393cda3b | ||
|
|
3e8f868d87 | ||
|
|
6b9999a414 | ||
|
|
de952cc314 | ||
|
|
bded5e8990 | ||
|
|
2ea2cf0fc6 | ||
|
|
dc7c8acb00 |
@@ -1,2 +1,9 @@
|
||||
# panasonic
|
||||
# Panasonic SMART TV download recordings
|
||||
|
||||
This project allows to download recordings from a Panasonic smart TV (model ...).
|
||||
This TV model is DLNA compatible. To download a recordings it is possible to request
|
||||
the TV to stream a MPEG-TS file through a HTTP get request.
|
||||
|
||||
# Usage
|
||||
|
||||
# Progress bar
|
||||
|
||||
49
panasonic.py
49
panasonic.py
@@ -150,12 +150,15 @@ def dumpFilms(films):
|
||||
logger.info('%d - %s enregistré le %s sur %s. Durée: %s. %s' % (fid, film['title'], film['date'], film['channel'], film['duration'], film['url']))
|
||||
fid = fid + 1
|
||||
|
||||
def getInitialTSFromBuffer(content):
|
||||
def getInitialTSFromBuffer(content, delta=0.1):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
with Popen(['ffprobe','/dev/stdin','-loglevel', 'quiet', '-read_intervals', '0%+0.000001', '-show_entries', 'frame=frame_no,best_effort_timestamp_time', '-of','json'], stdout=PIPE, stdin=PIPE) as ffprobe:
|
||||
out, _ = ffprobe.communicate(input=content)
|
||||
logger.debug('Determining initial timestamp from content')
|
||||
length = len(content)
|
||||
with Popen(['ffprobe','/dev/stdin','-loglevel', 'quiet', '-read_intervals', ('0%%+%f' % delta), '-show_entries', 'frame=frame_no,best_effort_timestamp_time', '-of','json'], stdout=PIPE, stdin=PIPE) as ffprobe:
|
||||
out, _ = ffprobe.communicate(input=content[0:min(length,200000)])
|
||||
frames = json.load(BytesIO(out))
|
||||
logger.debug('Frames: %s' % frames)
|
||||
if 'frames' in frames:
|
||||
frames = frames['frames']
|
||||
if len(frames) > 0:
|
||||
@@ -173,10 +176,12 @@ def getInitialTSFromBuffer(content):
|
||||
def getLastTSFromBuffer(content):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
logger.debug('Determining last timestamp from content.')
|
||||
length = len(content)
|
||||
with Popen(['ffprobe', '/dev/stdin', '-loglevel', 'quiet', '-show_entries', 'frame=frame_no,best_effort_timestamp_time', '-of','json'], stdout=PIPE, stdin=PIPE) as ffprobe:
|
||||
out, _ = ffprobe.communicate(input=content[max(0,length-100000):])
|
||||
out, _ = ffprobe.communicate(input=content[max(0,length-200000):])
|
||||
frames = json.load(BytesIO(out))
|
||||
logger.debug('Frames: %s' % frames)
|
||||
if 'frames' in frames:
|
||||
frames = frames['frames']
|
||||
if len(frames) > 0:
|
||||
@@ -220,7 +225,7 @@ def getLastTSFromFD(fd):
|
||||
else:
|
||||
return None
|
||||
|
||||
def downloadMovie(url, outputFileName, duration):
|
||||
def downloadMovie(url, outputFileName):
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
try:
|
||||
@@ -230,10 +235,11 @@ def downloadMovie(url, outputFileName, duration):
|
||||
exit(-1)
|
||||
|
||||
outputFD = output.fileno()
|
||||
session = requests.Session()
|
||||
|
||||
sample = 2.
|
||||
headers = {'TimeSeekRange.dlna.org': 'npt=0.000-%.03f' % sample}
|
||||
r = requests.get(url, headers=headers)
|
||||
r = session.get(url, headers=headers)
|
||||
if r.status_code != 200:
|
||||
logger.error('Impossible to download first %f seconds: %d' % (sample,r.status_code))
|
||||
exit(-1)
|
||||
@@ -242,7 +248,7 @@ def downloadMovie(url, outputFileName, duration):
|
||||
logger.error('TimeSeekRange.dlna.org is not in header: %s' % r.headers)
|
||||
exit(-1)
|
||||
|
||||
p = re.compile('^.*/(?P<totalsecs>[0-9]+)\.(?P<totalms>[0-9]+)$')
|
||||
p = re.compile(r'^.*/(?P<totalsecs>[0-9]+)\.(?P<totalms>[0-9]+)$')
|
||||
m = p.match(r.headers['TimeSeekRange.dlna.org'])
|
||||
if m == None:
|
||||
logger.error('Impossible to parse timestamps' % r.headers['TimeSeekRange.dlna.org'])
|
||||
@@ -250,9 +256,20 @@ def downloadMovie(url, outputFileName, duration):
|
||||
|
||||
total = int(m.group('totalsecs'))+(int(m.group('totalms')))/1000
|
||||
total = timedelta(seconds=total)
|
||||
logger.debug('Header: %s' % r.headers)
|
||||
|
||||
initialTS = getInitialTSFromBuffer(r.content)
|
||||
initialTS = None
|
||||
nbRetries = 0
|
||||
maxRetries = 10
|
||||
while initialTS == None and nbRetries < maxRetries:
|
||||
initialTS = getInitialTSFromBuffer(r.content, delta=0.1*nbRetries)
|
||||
nbRetries = nbRetries+1
|
||||
logger.debug('Initial timestamp: %s' % initialTS)
|
||||
if initialTS == None:
|
||||
logger.error('Impossible to retrieve initial timestamp')
|
||||
exit(-1)
|
||||
lastTS = getLastTSFromBuffer(r.content)
|
||||
logger.debug('Last timestamp: %s' % lastTS)
|
||||
delta = lastTS-initialTS
|
||||
ratio = delta/total
|
||||
estimatedLength = ceil(len(r.content)/ratio)
|
||||
@@ -262,17 +279,19 @@ def downloadMovie(url, outputFileName, duration):
|
||||
pb = tqdm(total=estimatedLength, unit='bytes', desc='Download', unit_scale=True, unit_divisor=1024)
|
||||
|
||||
nbiters = 0
|
||||
with requests.get(url, stream=True) as r:
|
||||
|
||||
with session.get(url, stream=True) as r:
|
||||
chunks = r.iter_content(chunk_size = 100000)
|
||||
for chunk in chunks:
|
||||
output.write(chunk)
|
||||
if nbiters == 1000:
|
||||
lastTS = getLastTSFromFD(outputFD)
|
||||
delta = lastTS-initialTS
|
||||
ratio = delta/total
|
||||
length = fstat(outputFD).st_size
|
||||
estimatedLength = ceil(length/ratio)
|
||||
pb.total = estimatedLength
|
||||
if lastTS != None:
|
||||
delta = lastTS-initialTS
|
||||
ratio = delta/total
|
||||
length = fstat(outputFD).st_size
|
||||
estimatedLength = ceil(length/ratio)
|
||||
pb.total = estimatedLength
|
||||
lseek(outputFD, 0, SEEK_END)
|
||||
nbiters = 0
|
||||
pb.update(len(chunk))
|
||||
@@ -350,7 +369,7 @@ def main():
|
||||
t = timedelta(seconds=duration)
|
||||
|
||||
logger.info('Downloading movie "%s" whose duration is %s' % (title, t))
|
||||
downloadMovie(url=films[mid]['url'], outputFileName=args.output, duration=duration)
|
||||
downloadMovie(url=films[mid]['url'], outputFileName=args.output)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user