Пример #1
0
def combine_images(fol,
                   movie_name,
                   frame_rate=10,
                   start_number=-1,
                   images_prefix='',
                   images_format='',
                   images_type='',
                   ffmpeg_cmd='',
                   movie_name_full_path=False,
                   debug=False,
                   copy_files=False,
                   add_reverse_frames=False,
                   **kwargs):
    if ffmpeg_cmd == '':
        ffmpeg_dir = utils.get_link_dir(utils.get_links_dir(), 'ffmpeg')
        ffmpeg_dir = op.join(ffmpeg_dir,
                             'bin') if utils.is_windows() else ffmpeg_dir
        ffmpeg_cmd = op.join(ffmpeg_dir,
                             'ffmpeg') if op.isdir(ffmpeg_dir) else 'ffmpeg'
    print('ffmpeg_cmd: {}'.format(ffmpeg_cmd))
    images_type, images_prefix, images_format, images_format_len, start_number = find_images_props(
        fol, start_number, images_prefix, images_format, images_type)
    if movie_name == '' and images_prefix != '':
        movie_name = images_prefix
    elif movie_name == '':
        movie_name = 'output_video'
    org_fol = fol
    if utils.is_windows() or copy_files:
        fol = change_frames_names(fol, images_prefix, images_type,
                                  images_format_len)
    if add_reverse_frames:
        add_reverse_frames_fol(fol, images_prefix, images_type)
    images_prefix = op.join(fol, images_prefix)
    if not movie_name_full_path:
        movie_name = op.join(fol, movie_name)
    combine_images_cmd = '{ffmpeg_cmd} -framerate {frame_rate} '
    if start_number != 1:
        # You might want to use a static ffmpeg if your ffmepg version doesn't support the start_number flag, like:
        # ffmpeg_cmd = '~/space1/Downloads/ffmpeg-git-static/ffmpeg'
        combine_images_cmd += '-start_number {start_number} '
    # Not working in windows:
    # combine_images_cmd += '-pattern_type glob -i "*.{images_type}" '
    combine_images_cmd += '-i {images_prefix}{images_format}.{images_type} '
    # http://stackoverflow.com/questions/20847674/ffmpeg-libx264-height-not-divisible-by-2
    combine_images_cmd += '-vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" '
    combine_images_cmd += '-c:v libx264 -r 30 -pix_fmt yuv420p {movie_name}.mp4'
    if debug:
        combine_images_cmd += ' -loglevel debug'
    rs = utils.partial_run_script(locals())
    rs(combine_images_cmd)
    with open(op.join(org_fol, 'combine_images_cmd.txt'), 'w') as f:
        f.write(combine_images_cmd.format(**locals()))
    return '{}.mp4'.format(movie_name)
Пример #2
0
import os.path as op
import glob
from src.utils import utils

FFMPEG_DIR = utils.get_link_dir(utils.get_links_dir(), 'ffmpeg')
FFMPEG_DIR = op.join(FFMPEG_DIR, 'bin') if utils.is_windows() else FFMPEG_DIR
FFMPEG_CMD = op.join(FFMPEG_DIR,
                     'ffmpeg') if op.isdir(FFMPEG_DIR) else 'ffmpeg'

# from moviepy.config import change_settings
# change_settings({"IMAGEMAGICK_BINARY": r"/usr/bin/convert"})

# https://www.vultr.com/docs/install-imagemagick-on-centos-6
# https://github.com/BVLC/caffe/issues/3884


def check_movipy():
    try:
        import moviepy.config as conf
        if conf.try_cmd([conf.FFMPEG_BINARY])[0]:
            print("MoviePy : ffmpeg successfully found.")
        else:
            print("MoviePy : can't find or access ffmpeg.")

        if conf.try_cmd([conf.IMAGEMAGICK_BINARY])[0]:
            print("MoviePy : ImageMagick successfully found.")
        else:
            print("MoviePy : can't find or access ImageMagick.")
    except:
        print("Can't import moviepy")
Пример #3
0
def create_links(links_fol_name='links', gui=True):
    links_fol = utils.get_links_dir(links_fol_name)
    utils.make_dir(links_fol)
    links_names = ['mmvt', 'subjects', 'blender', 'meg', 'fMRI', 'electrodes', 'freesurfer']
    all_links_exist = np.all([op.islink(op.join(links_fol, link_name)) for link_name in links_names])
    if all_links_exist:
        return True
    if not utils.is_link(op.join(links_fol, 'freesurfer')):
        if not utils.is_windows():
            if os.environ.get('FREESURFER_HOME', '') == '':
                print('If you have FreeSurfer installed, please source it and rerun')
                cont = input("Do you want to continue (y/n)?") # If you choose to continue, you'll need to create a link to FreeSurfer manually")
                if cont.lower() != 'y':
                    return
            else:
                freesurfer_fol = os.environ['FREESURFER_HOME']
                create_real_folder(freesurfer_fol)
    if not utils.is_link(op.join(links_fol, 'mmvt')):
        ret = utils.message_box('Please select where do you want to put the blend files? ', TITLE)
        if ret == 1:
            mmvt_fol = utils.choose_folder_gui() if gui else input()
            create_real_folder(mmvt_fol)
            utils.create_folder_link(mmvt_fol, op.join(links_fol, 'mmvt'))
    if not utils.is_link(op.join(links_fol, 'subjects')):
        ret = utils.message_box('Please select where do you want to store the FreeSurfer recon-all files neccessary for MMVT?\n' +
              'It prefered to create a local folder, because MMVT is going to save files to this directory: ', TITLE)
        if ret == 1:
            subjects_fol = utils.choose_folder_gui() if gui else input()
            create_real_folder(subjects_fol)
            utils.create_folder_link(subjects_fol, op.join(links_fol, 'subjects'))
    if not utils.is_link(op.join(links_fol, 'blender')):
        ret = utils.message_box('Please select where did you install Blender? ')
        if ret == 1:
            blender_fol = utils.choose_folder_gui() if gui else input()
            create_real_folder(blender_fol)
            utils.create_folder_link(blender_fol, op.join(links_fol, 'blender'))
    if not utils.is_link(op.join(links_fol, 'meg')):
        ret = utils.message_box('Please select where do you want to put the MEG files (Enter/Cancel if you are not going to use MEG data): ', TITLE)
        if ret == 1:
            meg_fol = utils.choose_folder_gui() if gui else input()
            create_real_folder(meg_fol)
            utils.create_folder_link(meg_fol, op.join(links_fol, 'meg'))
            if meg_fol != utils.get_resources_fol():
                utils.make_dir(op.join(meg_fol, 'default'))
    if not utils.is_link(op.join(links_fol, 'fMRI')):
        ret = utils.message_box('Please select where do you want to put the fMRI files (Enter/Cancel if you are not going to use fMRI data): ', TITLE)
        if ret == 1:
            fmri_fol = utils.choose_folder_gui() if gui else input()
            create_real_folder(fmri_fol)
            utils.create_folder_link(fmri_fol, op.join(links_fol, 'fMRI'))
    if not utils.is_link(op.join(links_fol, 'electrodes')):
        ret = utils.message_box('Please select where do you want to put the electrodes files (Enter/Cancel if you are not going to use electrodes data): ', TITLE)
        if ret == 1:
            electrodes_fol = utils.choose_folder_gui() if gui else input()
            create_real_folder(electrodes_fol)
            utils.create_folder_link(electrodes_fol, op.join(links_fol, 'electrodes'))

    # for real_fol, link_name in zip([mmvt_fol, subjects_fol, blender_fol, meg_fol, fmri_fol, electrodes_fol, freesurfer_fol],
    #         links_names):
    #     try:
    #         # utils.create_folder_link(real_fol, op.join(links_fol, link_name))
    #         # if not op.islink(op.join(links_fol, link_name)):
    #         #     os.symlink(real_fol, op.join(links_fol, link_name))
    #         # Add the default task in meg folder
    #         if link_name == 'meg' and real_fol != utils.get_resources_fol():
    #             utils.make_dir(op.join(real_fol, 'default'))
    #     except:
    #         print('Error with folder {} and link {}'.format(real_fol, link_name))
    #         print(traceback.format_exc())
    return np.all([utils.is_link(op.join(links_fol, link_name)) for link_name in links_names])