def record(self, request, queryset): legal_program_cnt = 0 for program in queryset: try: m3u8_file_path = parse.urlparse( program.url).path # /CCTV1/20180124/123456.m3u8 # mp4_file_path = Path(m3u8_file_path).parent / Path(program.title).with_suffix('.mp4') # /CCTV1/20180124/<title>.mp4 urlopen(program.url, timeout=5) except Exception as e: update_logger.error(e) self.message_user(request, '%s 转点播失败 请检查录播的网址是否可以访问' % (program.title), messages.ERROR) continue new_record = Vod(title=program.title, video=settings.RECORD_MEDIA_FOLDER + m3u8_file_path) new_record.save() p = threading.Thread(target=download_m3u8_files, args=(new_record.id, program.url, settings.RECORD_MEDIA_ROOT)) p.start() legal_program_cnt += 1 record_url = reverse('admin:vodmanagement_vod_changelist') self.message_user( request, mark_safe('%s/%s 个节目正在转成点播,转换进度请到<a href="%s">录制节目</a>处查看。' % (legal_program_cnt, queryset.count(), record_url)), messages.SUCCESS)
def download_m3u8_files(id, url_str, dest_dir): try: instance = Vod.objects.get(id=id) url = urlparse(url_str) m3u8_root = url.path m3u8_host_url = url.scheme + '://' + url.netloc m3u8_full_path = Path(dest_dir) / Path(m3u8_root[1:]) m3u8_full_path.parent.mkdir(parents=True, exist_ok=True) file_path, message = urlretrieve(url_str, str(m3u8_full_path)) with m3u8_full_path.open() as m3u8_file: m3u8_obj = m3u8.loads(m3u8_file.read()) total_files = len(m3u8_obj.files) for index, ts_file in enumerate(m3u8_obj.files): ts_url = urljoin( m3u8_host_url, pathname2url(str(Path(m3u8_root).parent / Path(ts_file)))) ts_full_path = m3u8_full_path.parent / Path(ts_file) ts_full_path.parent.mkdir(parents=True, exist_ok=True) status = download_ts_file(ts_url, str(ts_full_path)) if status is not None: instance.progress = int(index / total_files * 100) print(instance.progress) instance.save() instance.active = 1 instance.save() update_logger.info('record ' + str(instance.title) + ' success') except Exception as e: update_logger.error('Download m3u8(%s) files failed' % (url_str, )) update_logger.error(e)
def record_video(url, program_title): for i in range(0,len(url)): try: m3u8_file_path = parse.urlparse(url[i]).path # /CCTV1/20180124/123456.m3u8 video_path = settings.RECORD_MEDIA_FOLDER + m3u8_file_path if not os.path.exists(category_id_file): get_category_id() with open(category_id_file,'r', encoding='utf-8') as f: category_id = f.read() close_old_connections() new_record = Vod( title = str(datetime.date.today()) + program_title[i], video = video_path, category_id = category_id, image = str(Path(video_path).parents[0] / 'xinwenlianbo.jpg'), ) new_record.save() download_m3u8_files(new_record.id, url[i], settings.RECORD_MEDIA_ROOT) source = settings.STATIC_ROOT +'/xinwenlianbo.jpg' target = str(Path(settings.RECORD_MEDIA_ROOT + m3u8_file_path).parent) + '/xinwenlianbo.jpg' Path(target).parent.mkdir(exist_ok=True, parents=True) os.system('cp -f %s %s'%(source,target)) except Exception as e: update_logger.error(e) raise e
def result(*args, **kwargs): ret = 'Error' try: ret = func(*args, **kwargs) except Exception as e: update_logger.error('%sField is not available' % func) update_logger.error(e) return ret
def get_media_folder(): folders = [] try: folders = [ str(item) for item in Path(settings.SYSTEM_MEDIA_ROOT).iterdir() if item.is_dir() ] except Exception as e: update_logger.error("无法列出 SYSTEM_MEDIA_ROOT:%s 目录下的所有文件夹,请检查该目录是否存在" % settings.SYSTEM_MEDIA_ROOT) update_logger.error(e) return folders
def download_ts_file(url, dest_path): retry = 5 while retry: try: urlretrieve(url, str(dest_path)) return url except Exception as e: msg = url + ' download failed! ' update_logger.error(msg) update_logger.error(e) sleep(1) retry -= 1 return None
def auto_del(): try: if not os.path.exists(category_id_file): get_category_id() with open(category_id_file,'r', encoding='utf-8') as f: auto_record_id = f.read() now = datetime.datetime.now() delta = datetime.timedelta(days=7) close_old_connections() obj = Vod.objects.filter(category_id=int(auto_record_id), timestamp__lt=(now - delta)) for i in obj: i.delete() except Exception as e: update_logger.error(e) raise e update_logger.info('auto delete vod program success ')
def delete_vod(obj): try: delete_hard(obj.image.path) except Exception as e: update_logger.error(e) try: if os.path.splitext(str(obj.video))[1] == '.m3u8': obj_ts = (m3u8.load(str(obj.video.path))).files for ts_file in obj_ts: try: ts_path = Path(os.path.dirname( obj.video.path)) / Path(ts_file) os.remove(str(ts_path)) except Exception as e: update_logger.error(e) delete_hard(obj.video.path) except Exception as e: update_logger.error(e) update_logger.info('delete vod object ' + str(obj.title)) obj.delete()