Esempio n. 1
0
 def images_found(self):
     """ Number of images or frames """
     if self.is_video:
         retval = int(im_ffm.count_frames_and_secs(self.args.input_dir)[0])
     else:
         retval = len(self.input_images)
     return retval
Esempio n. 2
0
def test_brain_save_movie(tmpdir, renderer, brain_gc):
    """Test saving a movie of a Brain instance."""
    if renderer._get_3d_backend() == "mayavi":
        pytest.skip('Save movie only supported on PyVista')
    from imageio_ffmpeg import count_frames_and_secs
    brain = _create_testing_brain(hemi='lh', time_viewer=False)
    filename = str(op.join(tmpdir, "brain_test.mov"))
    for interactive_state in (False, True):
        # for coverage, we set interactivity
        if interactive_state:
            brain._renderer.plotter.enable()
        else:
            brain._renderer.plotter.disable()
        with pytest.raises(TypeError, match='unexpected keyword argument'):
            brain.save_movie(filename,
                             time_dilation=1,
                             tmin=1,
                             tmax=1.1,
                             bad_name='blah')
        assert not op.isfile(filename)
        tmin = 1
        tmax = 5
        duration = np.floor(tmax - tmin)
        brain.save_movie(filename,
                         time_dilation=1.,
                         tmin=tmin,
                         tmax=tmax,
                         interpolation='nearest')
        assert op.isfile(filename)
        _, nsecs = count_frames_and_secs(filename)
        assert_allclose(duration, nsecs, atol=0.2)

        os.remove(filename)
    brain.close()
Esempio n. 3
0
 def count(self):
     """ Number of faces or frames """
     if self.is_video:
         retval = int(im_ffm.count_frames_and_secs(self.folder)[0])
     else:
         retval = len(self.file_list_sorted)
     return retval
Esempio n. 4
0
def test_write1():

    for n in (1, 9, 14, 279, 280, 281):

        # Prepare for writing
        gen = imageio_ffmpeg.write_frames(test_file2, (64, 64))
        assert isinstance(gen, types.GeneratorType)
        gen.send(None)  # seed

        # Write n frames
        for i in range(n):
            data = bytes([min(255, 100 + i * 10)] * 64 * 64 * 3)
            gen.send(data)
        gen.close()

        # Check that number of frames is correct
        nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
        assert nframes == n

        # Check again by actually reading
        gen2 = imageio_ffmpeg.read_frames(test_file2)
        gen2.__next__()  # == meta
        count = 0
        for frame in gen2:
            count += 1
        assert count == n
Esempio n. 5
0
def test_write_big_frames():
    """Test that we give ffmpeg enough time to finish."""
    try:
        import numpy as np
    except ImportError:
        return skip("Missing 'numpy' test dependency")

    n = 9

    def _write_frames(pixfmt, bpp, tout):
        gen = imageio_ffmpeg.write_frames(test_file2, (2048, 2048),
                                          pix_fmt_in=pixfmt,
                                          ffmpeg_timeout=tout)
        gen.send(None)  # seed
        for i in range(n):
            data = (255 * np.random.rand(2048 * 2048 * bpp)).astype(np.uint8)
            data = bytes(data)
            gen.send(data)
        gen.close()

    # Short timeout is not enough time
    # Note that on Windows, if we wait a bit before calling count_frames_and_secs(),
    # it *does* work. Probably because killing a process on Windows is not instant (?)
    # and ffmpeg is able to still process the frames.
    _write_frames("rgb24", 3, 1.0)
    raises(RuntimeError, imageio_ffmpeg.count_frames_and_secs, test_file2)

    _write_frames("gray", 1, 15.0)
    nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
    assert nframes == n

    _write_frames("rgb24", 3, 15.0)
    nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
    assert nframes == n

    _write_frames("rgba", 4, 15.0)
    nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
    assert nframes == n

    _write_frames("rgba", 4, None)  # the default os to wait (since v0.4.0)
    nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
    assert nframes == n
Esempio n. 6
0
def test_write_pix_fmt_in():

    sizes = []
    for pixfmt, bpp in [("gray", 1), ("rgb24", 3), ("rgba", 4)]:
        # Prepare for writing
        gen = imageio_ffmpeg.write_frames(test_file2, (64, 64), pix_fmt_in=pixfmt)
        gen.send(None)  # seed
        for i in range(9):
            data = bytes([min(255, 100 + i * 10)] * 64 * 64 * bpp)
            gen.send(data)
        gen.close()
        with open(test_file2, "rb") as f:
            sizes.append(len(f.read()))
        # Check nframes
        nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
        assert nframes == 9
Esempio n. 7
0
def test_write_quality():

    sizes = []
    for quality in [2, 5, 9]:
        # Prepare for writing
        gen = imageio_ffmpeg.write_frames(test_file2, (64, 64), quality=quality)
        gen.send(None)  # seed
        for i in range(9):
            data = bytes([min(255, 100 + i * 10)] * 64 * 64 * 3)
            gen.send(data)
        gen.close()
        with open(test_file2, "rb") as f:
            sizes.append(len(f.read()))
        # Check nframes
        nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
        assert nframes == 9

    assert sizes[0] < sizes[1] < sizes[2]
Esempio n. 8
0
def test_write_pix_fmt_out():

    sizes = []
    for pixfmt in ["gray", "yuv420p"]:
        # Prepare for writing
        gen = imageio_ffmpeg.write_frames(test_file2, (64, 64), pix_fmt_out=pixfmt)
        gen.send(None)  # seed
        for i in range(9):
            data = bytes([min(255, 100 + i * 10)] * 64 * 64 * 3)
            gen.send(data)
        gen.close()
        with open(test_file2, "rb") as f:
            sizes.append(len(f.read()))
        # Check nframes
        nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
        assert nframes == 9

    assert sizes[0] < sizes[1]
Esempio n. 9
0
def test_write_audio_path():
    # Provide an audio

    gen = imageio_ffmpeg.write_frames(test_file2, (64, 64),
                                      audio_path=test_file3,
                                      audio_codec="aac")
    gen.send(None)  # seed
    for i in range(9):
        data = bytes([min(255, 100 + i * 10)] * 64 * 64 * 3)
        gen.send(data)
    gen.close()
    # Check nframes
    nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
    assert nframes == 9
    # Check size
    meta = imageio_ffmpeg.read_frames(test_file2).__next__()
    audio_codec = meta["audio_codec"]

    assert nframes == 9
    assert audio_codec == "aac"
Esempio n. 10
0
def test_write_macro_block_size():

    frame_sizes = []
    for mbz in [None, 10]:  # None is default == 16
        # Prepare for writing
        gen = imageio_ffmpeg.write_frames(test_file2, (40, 50), macro_block_size=mbz)
        gen.send(None)  # seed
        for i in range(9):
            data = bytes([min(255, 100 + i * 10)] * 40 * 50 * 3)
            gen.send(data)
        gen.close()
        # Check nframes
        nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
        assert nframes == 9
        # Check size
        meta = imageio_ffmpeg.read_frames(test_file2).__next__()
        frame_sizes.append(meta["size"])

    assert frame_sizes[0] == (48, 64)
    assert frame_sizes[1] == (40, 50)
Esempio n. 11
0
def test_write_bitrate():

    # Mind that we send uniform images, so the difference is marginal

    sizes = []
    for bitrate in ["1k", "10k", "100k"]:
        # Prepare for writing
        gen = imageio_ffmpeg.write_frames(test_file2, (64, 64), bitrate=bitrate)
        gen.send(None)  # seed
        for i in range(9):
            data = bytes([min(255, 100 + i * 10)] * 64 * 64 * 3)
            gen.send(data)
        gen.close()
        with open(test_file2, "rb") as f:
            sizes.append(len(f.read()))
        # Check nframes
        nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file2)
        assert nframes == 9

    assert sizes[0] < sizes[1] < sizes[2]
Esempio n. 12
0
def test_read_nframes():
    nframes, nsecs = imageio_ffmpeg.count_frames_and_secs(test_file1)
    assert nframes == 280
    assert 13.80 < nsecs < 13.99
Esempio n. 13
0
input_movie = '/home/auesro/Desktop/ABRS Test/hour120.mp4'
reader = imageio.get_reader(input_movie)

writer = imageio.get_writer(input_movie[:-4] + '_nocv2.mp4',
                            fps=reader.get_meta_data()['fps'],
                            codec='libx264',
                            quality=5)
writercv2 = imageio.get_writer(input_movie[:-4] + '_cv2.mp4',
                               fps=reader.get_meta_data()['fps'],
                               codec='libx264',
                               quality=5)

a = reader.iter_data()
frames = []
framescv2 = []
c = imageio_ffmpeg.count_frames_and_secs(input_movie)
for i in range(500):
    frame = np.uint8(next(a))
    framecv2 = cv2.cvtColor(np.uint8(next(a)), cv2.COLOR_BGR2GRAY)
    frames.append(np.resize(frame, (960, 960, 1)))
    framescv2.append(framecv2)
    writer.append_data(frames[i])
    writercv2.append_data(framescv2[i])

#resized = np.resize(frame[:,:,1], (960, 960, 1))

#rframe = np.pad(frame[:,:,1], ((210, 210),(0, 0)), 'constant')
#rframe = np.resize(rframe, (960, 960, 1))
#
#sidepad = np.pad(frame[:,:,1], ((0, 420),(0, 0)), 'constant')
#sidepad = np.resize(sidepad, (960, 960, 1))