Example #1
0
 def _make_archive(file_list, file_name, keep_existing=True, recursive=False):
     print_debug('Creating archive: {}'.format(file_name))
     if os.path.isfile(file_name):
         if keep_existing:
             print '{} is already an archive'.format(file_name)
             return
         else:
             os.remove(file_name)
     with tarfile.open(file_name, 'w:gz') as tar:
         print_debug('Creating tar: {}'.format(file_name))
         if recursive:
             [tar.add(f, arcname=os.path.join('distributed_cv', os.path.relpath(f, module_dir))) for f in file_list]
         else:
             [tar.add(f, arcname=os.path.basename(f)) for f in file_list]
def process_youtube(youtube_url):
    """Downloads and splits a YouTube video. Returns title and file list."""

    def get_youtube_filename(youtube_url):
        """Gets the name of a youtube video."""

        filename = ''
        try:
            cmd = ['youtube-dl', '--get-filename', youtube_url]
            filename = subprocess.check_output(cmd).rstrip()
            filename = filename.replace(' ', '')
        except OSError as e:
            if e.errno == os.errno.ENOENT:
                sys.exit('Must install youtube-dl: pip install youtube-dl')
            else:
                raise
        return ''.join(c for c in filename if c.isalnum() or c == '.')

    video_file = get_youtube_filename(youtube_url)
    video_stem = video_file.split('.')[0]
    video_dir_full = os.path.join('input', 'videos', video_stem)
    video_file_full = os.path.join('input', 'videos', video_file)
    if not os.path.isdir(video_dir_full):
        os.makedirs(video_dir_full)
    if os.path.isfile(video_file_full):
        print_debug('Video {} already downloaded'.format(video_file_full))
    else:
        print_debug('Downloading video {} to {}'.format(video_stem,
                                                        video_file_full))
        subprocess.call(['youtube-dl', '-q', '-o', video_file_full,
                                                   youtube_url])
    print_debug('Splitting frames to {}'.format(video_dir_full))
    splitter = VideoSplitter(video_file_full, video_dir_full, 'jpg')
    file_list, list_txt = splitter.split()

    return file_list, list_txt