def download_list(l, options, logger: Logger, index=1, list_count=1): """downloads list l with options""" videos_without_format_avail = [] keep_file = options.get("keep_file", False) options.pop("keep_file", None) try: with youtube_dl.YoutubeDL(options) as ydl: for i in l: logger.debug( f"[{str(l.index(i) + 1)}/{str(index + 1)}/{str(list_count)}] downloading {i}", options.get("verbose", False)) ydl.download([i]) except DownloadError: videos_without_format_avail.append(i) video_format = options["format"] del options["format"] finally: with youtube_dl.YoutubeDL(options) as ydl: for i in videos_without_format_avail: video_name = "notafile.txt" ydl.download([i]) files = os.listdir() files.sort() for i in files: if not utils.is_video(i): files.pop(files.index(i)) else: video_name = i os.system("ffmpeg -i '" + video_name + "' '" + removeExtension(video_name) + "." + video_format + "'") if not keep_file and video_name != "notafile.txt": os.remove(video_name)
def to_python(self, data): """ Checks that the file-upload field data contains a valid image (GIF, JPG, PNG, possibly others -- whatever the Python Imaging Library supports) or video. """ f = super(ImageField, self).to_python(data) if f is None: return None file = None buffer = None # We need to get a file object. We might have a path or we might # have to read the data into memory. if hasattr(data, 'temporary_file_path'): file = data.temporary_file_path() else: if hasattr(data, 'read'): buffer = data.read() else: buffer = data['content'] # If file is not vide, check if its an Image file if (file and not is_video(file)) or (buffer and not is_video_buffer(buffer)): f = super(VideoOrImageField, self).to_python(data) if f is None: return None if hasattr(f, 'seek') and callable(f.seek): f.seek(0) return f
def main(options, logger=None): if logger == None: logger = Logger("download.log", "download.py", True, True) else: logger.context = "download.py" try: query = utils.getQuery() logger.context = "download.py" if __name__ == "__main__": utils.log_header(logger, DESCRIPTION, options.get("verbose", False)) logger.debug("Options:", options.get("verbose", False)) for i in options: logger.debug(i + ": " + str(options[i]), options.get("verbose", False)) download_query(query, options, logger) # move all downloaded videos in videos directory repoDir = utils.repoPath() fs = os.listdir(repoDir) for f in fs: if utils.is_video(f): os.rename(f, "videos/" + f) logger.success("All files downloaded.", not options.get("silent", False)) except Exception as e: logger.handle_exception(e) raise e
def test_is_video(): v1 = is_video("dkfgkjrkes.mp4") v2 = is_video("swhdui43i83h.mov") v3 = is_video("heldfhflsdhfw4u38lo.webm") v4 = is_video("dehai3hafhleakö.mkv") v5 = is_video("d3ug7qk3u,jd.mp3") v6 = is_video("u2qkd3qkfugqsabf.flaaac") v7 = is_video("FWLFJHaEljhdfjahf.stl") assert v1 and v2 and v3 and v4 and not (v5 or v6 or v7)
def save(self, name, content, save=True): super(ThumbnailVideoOrImageFieldFile, self).save(name, content, save) file = self.path if not is_video(file): if self.field.thumb_aspect: img = Image.open(self.path) img.thumbnail( (self.field.thumb_width, self.field.thumb_height), Image.ANTIALIAS ) img.save(self.thumb_path, 'JPEG') else: img = Image.open(self.path) img = img.resize( (self.field.thumb_width, self.field.thumb_height), Image.ANTIALIAS ) img.save(self.thumb_path, 'JPEG') else: ffmpeg = "ffmpeg -i %s -vcodec libvpx -level 216 -qmax 42 -qmin 10 -vb 2M -maxrate 24M -minrate 100k -b 3900k -pass 1 -an -f webm -y %s" % (self.path, self.webm_path) grabimage = "ffmpeg -y -i %s -vframes 1 -ss 00:00:02 -an -vcodec png -f rawvideo -s 200x150 %s" % (self.path, self.thumb_path) try: grab = commands.getoutput(grabimage) except: raise Exception("FFmpeg polegl na wyciaganiu obrazka") if not is_webm(self.path): try: ffmpegresult = commands.getoutput(ffmpeg) try: s = os.stat(self.webm_path) fsize = s.st_size if fsize == 0: os.remove(self.webm_path) raise Exception("Target file size is %s" % fsize) else: os.remove(self.path) except: raise Exception(ffmpegresult) except: raise Exception("FFmpeg polegl na konwertowaniu")
def main(): args = get_args() manifest_directory = os.path.abspath(args.output_dir) if not os.path.exists(manifest_directory): os.makedirs(manifest_directory) source = os.path.abspath(os.path.expanduser(args.source)) sources = [] if not os.path.isfile(source): # directory/pattern with images data_dir = None if os.path.isdir(source): data_dir = source for root, _, files in os.walk(source): sources.extend( [os.path.join(root, f) for f in files if is_image(f)]) else: items = source.lstrip('/').split('/') position = 0 try: for item in items: if set(item) & {'*', '?', '[', ']'}: break position += 1 else: raise Exception('Wrong positional argument') assert position != 0, 'Wrong pattern: there must be a common root' data_dir = source.split(items[position])[0] except Exception as ex: sys.exit(str(ex)) sources = list(filter(is_image, glob(source, recursive=True))) sources = list( filter(lambda x: 'related_images{}'.format(os.sep) not in x, sources)) # If the source is a glob expression, we need additional processing abs_root = source while abs_root and re.search('[*?\[\]]', abs_root): abs_root = os.path.split(abs_root)[0] related_images = detect_related_images(sources, abs_root) meta = { k: { 'related_images': related_images[k] } for k in related_images } try: assert len(sources), 'A images was not found' manifest = ImageManifestManager(manifest_path=manifest_directory) manifest.link(sources=sources, meta=meta, sorting_method=args.sorting, use_image_hash=True, data_dir=data_dir) manifest.create(_tqdm=tqdm) except Exception as ex: sys.exit(str(ex)) else: # video try: assert is_video( source ), 'You can specify a video path or a directory/pattern with images' manifest = VideoManifestManager(manifest_path=manifest_directory) manifest.link(media_file=source, force=args.force) try: manifest.create(_tqdm=tqdm) except AssertionError as ex: if str(ex) == 'Too few keyframes': msg = 'NOTE: prepared manifest file contains too few key frames for smooth decoding.\n' \ 'Use --force flag if you still want to prepare a manifest file.' print(msg) sys.exit(2) else: raise except Exception as ex: sys.exit(str(ex)) print('The manifest file has been prepared')
def _get_is_video(self): return is_video(self.path)
def _get_webm_url(self): if(is_video(self.path)): return _add_webm(self.url) else: return ""