def test_open(): url1 = "tests/assets/testvideo-1m.mp4" url2 = "tests/assets/testaudio-1m.mp3" with open((url1, url2), "rav", t=1, blocksize=0) as reader: for st, data in reader: print(st, data['shape'], data['dtype']) print('testing "rvv"') with open( url1, "rvv", t=1, blocksize=0, filter_complex="[0:v]split=2[out1][out2]", map=["[out1]", "[out2]"], ) as reader: for st, data in reader: print(st, data['shape'], data['dtype']) print('testing "raa"') with open( url2, "raa", t=1, blocksize=0, filter_complex="[0:a]asplit=2[out1][out2]", map=["[out1]", "[out2]"], ) as reader: for st, data in reader: print(st, data['shape'], data['dtype'])
def test_fg(): with ffmpegio.open("color=r=red:d=1:r=10", "rv", f_in="lavfi", pix_fmt="rgb24") as f: I = f.read(-1) assert I["shape"][0] == 10
def try_devices(): devices.scan() print(devices.list_sources()) print(devices.list_source_options("dshow", "v:0")) print(devices.list_source_options("dshow", "a:0")) fs, x = video.read("v:0", f_in="dshow", t=1, show_log=True) # capture 10 seconds of audio fs, x = audio.read("a:0", f_in="dshow", t=1, show_log=True) print(f"[a:0] rate={fs}, data={[*x.keys()]}") # stream webcam video feed for with open("v:0", "rv", f_in="dshow", show_log=True) as dev: print(f"[v:0] rate={dev.rate}") for i, frame in enumerate(dev): print(f"Frame {i}: {[*frame.keys()]}") break # save video and audio to mp4 file # - if a device support multiple streams, specify their enums separated by '|' with TemporaryDirectory() as tempdir: transcode( "v:0|a:0", path.join(tempdir, "captured.mp4"), f_in="dshow", t_in=1, show_log=True, )
def test_read_write_audio(): outext = ".flac" with ffmpegio.open(url, "ra") as f: F = b"".join((f.read(100)["buffer"], f.read(-1)["buffer"])) fs = f.rate shape = f.shape dtype = f.dtype bps = f.samplesize out = {"dtype": dtype, "shape": shape} with tempfile.TemporaryDirectory() as tmpdirname: out_url = path.join(tmpdirname, re.sub(r"\..*?$", outext, path.basename(url))) with ffmpegio.open(out_url, "wa", rate_in=fs, show_log=True) as f: f.write({**out, "buffer": F[:100 * bps]}) f.write({**out, "buffer": F[100 * bps:]})
def test_video_filter(): url = "tests/assets/testvideo-1m.mp4" fps = 10 # fractions.Fraction(60000,1001) with ffmpegio.open(url, "rv", blocksize=30, t=30) as src, ffmpegio.open("scale=200:100", "fv", rate_in=src.rate, rate=fps, show_log=True) as f: def process(i, frames): print( f"{i} - output {frames['shape'][0]} frames ({f.nin},{f.nout})") for i, frames in enumerate(src): process(i, f.filter(frames)) assert f.rate_in == src.rate assert f.rate == fps process("end", f.flush())
def test_read_audio(caplog): # caplog.set_level(logging.DEBUG) fs, x = ffmpegio.audio.read(url) bps = utils.get_samplesize(x["shape"][-1:], x["dtype"]) with ffmpegio.open(url, "ra", show_log=True, blocksize=1024**2) as f: # x = f.read(1024) # assert x['shape'] == (1024, f.ac) blks = [blk["buffer"] for blk in f] x1 = b"".join(blks) assert x["buffer"] == x1 n0 = int(0.5 * fs) n1 = int(1.2 * fs) t0 = n0 / fs t1 = n1 / fs with ffmpegio.open(url, "ra", ss_in=t0, to_in=t1, show_log=True, blocksize=1024**2) as f: blks, shapes = zip(*[(blk["buffer"], blk["shape"][0]) for blk in f]) log = f.readlog() shape = sum(shapes) print(log) x2 = b"".join(blks) # # print("# of blks: ", len(blks), x1['shape']) # for i, xi in enumerate(x2): # print(i, xi-x[n0 + i]) # assert np.array_equal(xi, x[n0 + i]) assert shape == n1 - n0 assert x["buffer"][n0 * bps:n1 * bps] == x2
def test_read_video(): w = 420 h = 360 with ffmpegio.open(url, "rv", vf="transpose", pix_fmt="gray", s=(w, h), show_log=True) as f: F = f.read(10) print(f.rate) assert f.shape == (h, w, 1) assert f.samplesize == w * h assert F["shape"] == (10, h, w, 1) assert F["dtype"] == f.dtype
def test_read_write_video(): fs, F = ffmpegio.video.read(url, t=1) bps = utils.get_samplesize(F["shape"][-3:], F["dtype"]) F0 = { "buffer": F["buffer"][:bps], "shape": (1, *F["shape"][1:]), "dtype": F["dtype"], } F1 = { "buffer": F["buffer"][bps:], "shape": (F["shape"][0] - 1, *F["shape"][1:]), "dtype": F["dtype"], } with tempfile.TemporaryDirectory() as tmpdirname: out_url = path.join(tmpdirname, re.sub(r"\..*?$", outext, path.basename(url))) with ffmpegio.open(out_url, "wv", rate_in=fs) as f: f.write(F0) f.write(F1)
import ffmpegio print("CREATE Movie") print(f"ffmpeg ver: {ffmpegio.ffmpeg_info()}") # fileName = self.generateFilename() fileName = 'sandbox/test_filtered_videoout.mp4' fpsin, frames = ffmpegio.video.read('tests/assets/testvideo-1m.mp4', vframes=10) nFrames = frames.shape[0] fps = 16 with ffmpegio.open(fileName, 'wv', rate=fps, vf='vflip,hflip,transpose', show_log=True, overwrite=True) as out: for frame in range(nFrames): myImg = frames[frame] # global_norm = np.true_divide((myImg - minV), (maxV - minV)) # norm = (global_norm * 255) # intNorm = norm.astype(int) out.write(myImg)