Skip to content

Commit feaf606

Browse files
committed
version bump =>0.0.3
renamed transcode_sync() to transcode() added force option to transcode()
1 parent 53626bf commit feaf606

6 files changed

Lines changed: 54 additions & 22 deletions

File tree

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
numpy
22
matplotlib
33
pytest
4-
PyQt5
4+
PyQt5
5+
wheel

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = ffmpegio
3-
version = 0.0.2
3+
version = 0.0.3
44
description = Read/write media files with FFmpeg
55
long_description = Read/write media files with FFmpeg; file: LICENSE
66
keywords = multimedia

src/ffmpegio/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
underlying
1313
"""
1414

15-
from .transcode import transcode_sync
15+
from .transcode import transcode
1616
from . import caps
1717
from . import probe
1818
from . import audio
@@ -21,4 +21,4 @@
2121

2222
set_path = _ffmpeg.find
2323

24-
__all__ = ["transcode_sync", "caps", "probe", "set_path", "audio", "image"]
24+
__all__ = ["transcode", "caps", "probe", "set_path", "audio", "image"]

src/ffmpegio/ffmpeg.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,7 @@ def run_sync(
286286
args.insert(1, "-hide_banner")
287287

288288
ret = sp.run(args, *sp_arg, stdout=stdout, stderr=stderr, **sp_kwargs)
289-
if ret.returncode != 0:
290-
print(ret.stderr)
289+
if ret.returncode != 0 and ret.stderr is not None:
291290
raise Exception(f"execution failed\n {shlex.join(args)}\n\n{ret.stderr.decode('utf-8')}")
292291
return ret.stdout
293292

@@ -302,8 +301,7 @@ def run(args, *sp_arg, hide_banner=True, stdout=sp.PIPE, stderr=sp.PIPE, **sp_kw
302301
args.insert(1, "-hide_banner")
303302

304303
ret = sp.run(args, *sp_arg, stdout=stdout, stderr=stderr, **sp_kwargs)
305-
if ret.returncode != 0:
306-
print(ret.stderr)
304+
if ret.returncode != 0 and ret.stderr is not None:
307305
raise Exception(f"execution failed\n {shlex.join(args)}\n\n{ret.stderr.decode('utf-8')}")
308306
return ret.stdout
309307

src/ffmpegio/transcode.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
from os import path
2+
13
from . import ffmpeg
24
from . import probe
35

46

5-
def transcode_sync(
7+
def transcode(
68
input_url,
79
output_url,
810
start=None,
@@ -18,6 +20,7 @@ def transcode_sync(
1820
video_crf=None,
1921
video_pix_fmt=None,
2022
video_filter=None,
23+
force=None,
2124
input_options=None,
2225
output_options=None,
2326
global_options=None,
@@ -26,7 +29,7 @@ def transcode_sync(
2629
2730
https://ffmpeg.org/ffmpeg.html
2831
29-
:param input_url: url/path of the input media file
32+
:param input_url: url/path of the input media file
3033
:type input_url: str
3134
:param output_url: url/path of the output media file
3235
:type output_url: str
@@ -57,6 +60,8 @@ def transcode_sync(
5760
:param video_filter: filtergraph definition for filtering video streams, defaults to None
5861
:type video_filter: str, optional
5962
:param input_options: dict of user-defined input options, defaults to None. each key is FFmpeg option argument without leading '-' with trailing stream specifier if needed. For flag options, set their values to None.
63+
:param force: True to overwrite if file exists or False to skip. If None or
64+
unspecified, FFmpeg will ask for resolution.
6065
:type input_options: dict, optional
6166
:param output_options: dict of user-defined output options, defaults to None. each key is FFmpeg option argument without leading '-' with trailing stream specifier if needed. For flag options, set their values to None.
6267
:type output_options: dict, optional
@@ -66,15 +71,15 @@ def transcode_sync(
6671
:rtype: int
6772
6873
notes:
69-
74+
7075
`start` vs `end` vs `duration` - Only 2 out of 3 are honored.
7176
7277
======= ===== ========== ======================================================================
7378
`start` `end` `duration` FFmpeg config
7479
======= ===== ========== ======================================================================
7580
X start time specified, transcode till the end of input
7681
X transcode from the beginning of the input till `end` time is hit
77-
X transcode from the beginning of the input till encoded `duration` long
82+
X transcode from the beginning of the input till encoded `duration` long
7883
X X start and end time specified
7984
X X start and duration specified
8085
X X end and duration nspecified (start = end - duration)
@@ -166,13 +171,36 @@ def transcode_sync(
166171
),
167172
}
168173

169-
if global_options and isinstance(global_options, str):
170-
global_options = ffmpeg.parse_options(global_options)
174+
gopts = {}
175+
if force is not None:
176+
if force:
177+
gopts["y"] = None
178+
else:
179+
gopts["n"] = None
180+
181+
if global_options:
182+
gopts = {
183+
**gopts,
184+
**(
185+
ffmpeg.parse_options(global_options)
186+
if isinstance(global_options, str)
187+
else global_options
188+
),
189+
}
171190

172191
args = dict(
173-
global_options=global_options,
192+
global_options=gopts,
174193
inputs=[(input_url, inopts)],
175194
outputs=[(output_url, outopts)],
176195
)
177196

178-
ffmpeg.run_sync(args, stdout=None, stderr=None,)
197+
#TODO run async and monitor stderr for better error handling
198+
try:
199+
ffmpeg.run_sync(
200+
args,
201+
stdout=None,
202+
stderr=None,
203+
)
204+
except Exception as e:
205+
if not (gopts.get("n", True) and path.isfile(output_url)):
206+
raise e

tests/test_transcode.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@
1515
# url = "tests/assets/testaudio-two.wav"
1616
url = "tests/assets/testaudio-three.wav"
1717
# url = "tests/assets/testvideo-5m.mpg"
18+
outext = ".flac"
19+
1820
# url = "tests/assets/testvideo-43.avi"
1921
# url = "tests/assets/testvideo-169.avi"
20-
url = r"C:\Users\tikum\Music\(アルバム) [Jazz Fusion] T-SQUARE - T-Square Live featuring F-1 Grand Prix Theme [EAC] (flac+cue).mka"
21-
outext = ".flac"
22+
# url = r"C:\Users\tikum\Music\(アルバム) [Jazz Fusion] T-SQUARE - T-Square Live featuring F-1 Grand Prix Theme [EAC] (flac+cue).mka"
23+
# outext = ".mp4"
2224

2325
with tempfile.TemporaryDirectory() as tmpdirname:
2426
print(probe.audio_streams_basic(url))
2527
out_url = path.join(tmpdirname, re.sub(r"\..*?$", outext, path.basename(url)))
2628
print(out_url)
27-
transcode.transcode_sync(url, out_url)
29+
transcode(url, out_url, force=True)
2830
print(probe.audio_streams_basic(out_url))
29-
30-
with open(path.join(tmpdirname, "progress.txt")) as f:
31-
print(f.read())
31+
transcode(url, out_url)
32+
transcode(url, out_url, force=False)
33+
transcode(url, out_url, force=True)
34+
35+
# with open(path.join(tmpdirname, "progress.txt")) as f:
36+
# print(f.read())

0 commit comments

Comments
 (0)