Skip to content

Commit 5c1ba3d

Browse files
committed
functionalize get_videos
1 parent 85a37c1 commit 5c1ba3d

File tree

5 files changed

+47
-33
lines changed

5 files changed

+47
-33
lines changed

demo.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ def mp_worker(i: int, Niter: int, tic: float):
7474
P = ArgumentParser(
7575
description="Demonstrate differences between coroutines, threads and proceses."
7676
)
77-
P.add_argument("method", choices=["c", "t", "p"], help="c: coroutine, t: threading, p: multiprocessing")
77+
P.add_argument(
78+
"method", choices=["c", "t", "p"], help="c: coroutine, t: threading, p: multiprocessing"
79+
)
7880
P.add_argument("-Nworker", help="number of workers", type=int, default=4)
7981
P.add_argument(
8082
"-Niter", help="number of loop iterations (arbitrary)", type=int, default=5000000,
@@ -90,9 +92,7 @@ def mp_worker(i: int, Niter: int, tic: float):
9092
t.start()
9193
ts.append(t)
9294
elif A.method == "p":
93-
p = multiprocessing.Process(
94-
target=mp_worker, args=(i, A.Niter, tic)
95-
)
95+
p = multiprocessing.Process(target=mp_worker, args=(i, A.Niter, tic))
9696
p.start()
9797
ps.append(p)
9898

play_coroutine.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
#!/usr/bin/env python
2-
from pathlib import Path
1+
#!/usr/bin/env python3
32
from argparse import ArgumentParser
43

54
import asyncioffmpeg.ffplay as play
65
from asyncioffmpeg.runner import runner
6+
from asyncioffmpeg import get_videos
77

88

99
if __name__ == "__main__":
@@ -17,10 +17,7 @@
1717
)
1818
P = p.parse_args()
1919

20-
path = Path(P.path).expanduser()
21-
if not path.is_dir():
22-
raise NotADirectoryError(path)
23-
24-
flist = (f for f in path.iterdir() if f.is_file() and f.suffix in P.suffix)
20+
flist = get_videos(P.path, P.suffix)
21+
print("found", len(flist), "files in", P.path)
2522

2623
runner(play.main, flist)

play_sync.py renamed to play_queue.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
"""
33
example of using queue with non-asyncio subprocess and threading
44
"""
5-
from pathlib import Path
5+
66
from argparse import ArgumentParser
77
import queue
88
import threading
99

1010
import asyncioffmpeg.ffplay as play
11+
from asyncioffmpeg import get_videos
1112

1213
NTHREADS = 2
1314

@@ -16,16 +17,12 @@
1617
p = ArgumentParser(description="Plays media files with FFplay")
1718
p.add_argument("path", help="directory where media files are kept")
1819
p.add_argument(
19-
"-suffix",
20-
help="file suffixes of desired media file types",
21-
nargs="+",
22-
default=[".mp4", ".avi", ".ogv", ".wmv", ".flv", ".mov"],
20+
"-suffix", help="file suffixes of desired media file types", nargs="+",
2321
)
2422
P = p.parse_args()
2523

26-
path = Path(P.path).expanduser()
27-
28-
flist = (f for f in path.iterdir() if f.is_file() and f.suffix in P.suffix)
24+
flist = get_videos(P.path, P.suffix)
25+
print("found", len(flist), "files in", P.path)
2926

3027
qin = queue.Queue() # type: ignore
3128
for fn in flist:

play_threadpool.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
from pathlib import Path
33
import subprocess
44
from argparse import ArgumentParser
55
import concurrent.futures
6-
from asyncioffmpeg.ffplay import FFPLAY
6+
import shutil
7+
8+
from asyncioffmpeg import get_videos
9+
10+
EXE = shutil.which("ffplay")
11+
if not EXE:
12+
raise FileNotFoundError("ffplay")
713

814

915
def ffplay(filein: Path):
10-
assert filein.is_file()
1116

12-
cmd = [FFPLAY, "-v", "warning", "-autoexit", str(filein)]
17+
if not filein.is_file():
18+
raise FileNotFoundError(filein)
19+
20+
cmd = [EXE, "-v", "warning", "-autoexit", str(filein)]
1321

1422
subprocess.check_call(cmd)
1523

@@ -18,18 +26,12 @@ def ffplay(filein: Path):
1826
p = ArgumentParser(description="Asynchronous playback with ThreadPool and FFplay")
1927
p.add_argument("path", help="directory where media files are kept")
2028
p.add_argument(
21-
"-suffix",
22-
help="file suffixes of desired media file types",
23-
nargs="+",
24-
default=[".mp4", ".avi", ".ogv", ".wmv", ".flv", ".mov"],
29+
"-suffix", help="file suffixes of desired media file types", nargs="+",
2530
)
2631
P = p.parse_args()
2732

28-
path = Path(P.path).expanduser()
29-
if not path.is_dir():
30-
raise FileNotFoundError(f"{path} is not a directory")
31-
32-
flist = (f for f in path.iterdir() if f.is_file() and f.suffix in P.suffix)
33+
flist = get_videos(P.path, P.suffix)
34+
print("found", len(flist), "files in", P.path)
3335

3436
with concurrent.futures.ThreadPoolExecutor(max_workers=2, thread_name_prefix="ffplay") as pool:
3537
pool.map(ffplay, flist)

src/asyncioffmpeg/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from pathlib import Path
2+
import typing as T
3+
4+
5+
def get_videos(path: Path, suffixes: T.Sequence[str] = None) -> T.List[Path]:
6+
7+
if not suffixes:
8+
suffixes = [".mp4", ".avi", ".ogv", ".wmv", ".flv", ".mov"]
9+
10+
path = Path(path).expanduser()
11+
12+
if not path.is_dir():
13+
raise FileNotFoundError(f"{path} is not a directory")
14+
15+
flist = [f for f in path.iterdir() if f.is_file() and f.suffix in suffixes]
16+
if len(flist) == 0:
17+
raise FileNotFoundError(f"No files found in {path} with {suffixes}")
18+
return flist

0 commit comments

Comments
 (0)