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 get_program(): cf = configparser.ConfigParser() config_file = Path(settings.BASE_DIR) / 'conf' / 'auto_record.conf' cf.read(str(config_file), encoding="utf-8") title = [] channel_id = [] if len(cf.sections()) == 0: return title, channel_id else: for obj in cf.sections(): title.append(cf.get(obj, 'title')) channel_id.append(cf.get(obj,'channel_id')) update_logger.info('get auto record program success') return title, channel_id
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 ff(obj): video_path = obj.video.name recent_path = os.getcwd() os.chdir('/') video_abspath = obj.video.path transcode = ffmpy.FFmpeg(inputs={str(obj.video.path): '-y'}, outputs={ str(Path(obj.video.path).with_suffix('.mp4')): '-vcodec h264 -acodec aac -threads 2' }) os.chdir(recent_path) if transcode.run() == 0: os.remove(str(video_abspath)) video_name_new = Path(video_path).with_suffix('.mp4') obj.video.name = str(video_name_new) obj.save() update_logger.info('transcode ' + str(obj.title) + ' success')
def save(self, without_valid=False, *args, **kwargs): p = Pinyin() full_pinyin = p.get_pinyin(smart_str(self.title), '') first_pinyin = p.get_initials(smart_str(self.title), '').lower() self.search_word = " ".join([full_pinyin, first_pinyin]) update_logger.info('video path: ' + str(self.video)) if self.description is None or self.description == "": self.description = default_description(self) if self.local_video != '' and self.local_video is not None: basename = Path(self.local_video).relative_to( Path(settings.LOCAL_MEDIA_ROOT)) self.video.name = str(Path(settings.LOCAL_MEDIA_URL) / basename) update_logger.debug("save local_video to filefield done") if without_valid: ret = super(Vod, self).save(*args, **kwargs) return ret super(Vod, self).save(*args, **kwargs) if os.path.splitext(str(self.video))[1] not in ['.mp4', '.m3u8']: p = threading.Thread(target=ff, args=(self, )) p.start() try: if self.video != None and self.video != '': relative_path = Path(self.video.name).relative_to( settings.MEDIA_URL) # Djan%20go.mp4 rel_name = uri_to_iri(relative_path) # Djan go.mp4 # Make sure the self.video.name is not in the LOCAL_FOLDER if not self.video.name.startswith(settings.LOCAL_FOLDER_NAME) and \ not self.video.name.startswith(settings.RECORD_MEDIA_FOLDER): self.video.name = str(rel_name) update_logger.info('save path: ' + self.save_path) update_logger.info('video name: ' + str(self.video.name)) update_logger.info('size: ' + self.video.file.size) self.file_size = humanfriendly.format_size( self.video.file.size) # duration = VideoFileClip(self.video.path).duration # self.duration = time_formate(duration) else: print("video file is None") except: pass try: if self.image: self.image.name = str( uri_to_iri( Path(self.image.name).relative_to(settings.MEDIA_URL))) except: pass return super(Vod, self).save(*args, **kwargs)
def get_record_info(title, channel_id): url = [] program_title = [] for i in range(0,len(title)): close_old_connections() obj = Program.objects.filter( title=title[i], finished='1', channel_id=channel_id[i], start_time__startswith=datetime.date.today() ) if len(obj) == 0: raise Exception('No matched program') else: for i in obj: url.append(i.url) program_title.append(i.title) update_logger.info('get porgrams url success') return url, program_title
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()