Skip to content

Commit 67701a4

Browse files
committed
fix cuda acceleration.
1 parent e2f04e7 commit 67701a4

File tree

8 files changed

+52
-30
lines changed

8 files changed

+52
-30
lines changed

README.md

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!--
22
* @Date: 2021.02.30 17:30:51
33
* @LastEditors: Rustle Karl
4-
* @LastEditTime: 2021.05.04 13:13:00
4+
* @LastEditTime: 2021.05.25 08:27:32
55
-->
66

77
# FFmpeg Generator
@@ -12,6 +12,7 @@ Python bindings for FFmpeg - with almost all filters support, even `gltransition
1212

1313
- [FFmpeg Generator](#ffmpeg-generator)
1414
- [Overview](#overview)
15+
- [TODO](#todo)
1516
- [Installation](#installation)
1617
- [Documents](#documents)
1718
- [GLTransition Filter](#gltransition-filter)
@@ -44,6 +45,11 @@ This project is based on [`ffmpeg-python`](https://github.com/kkroening/ffmpeg-p
4445
- support FFplay&FFprobe
4546
- enable cuda hwaccel by default
4647

48+
## TODO
49+
50+
- [] separate outputs for select filter
51+
- [] experiment: automatically generate subtitle files
52+
4753
## Installation
4854

4955
```shell
@@ -64,7 +70,7 @@ Or read my study notes, plan to demonstrate all the filters, but written in Chin
6470
- [All Examples for Audio Filters](docs/afilters.md)
6571
- [All Examples for Video Filters](docs/vfilters.md)
6672
- [All Examples for Audio/Video Sources](docs/sources.md)
67-
- [All Examples for Media Filters](docs/mfilters.md)
73+
- [All Examples for Media Filters](docs/avfilters.md)
6874
- [Introduce Usage of FFplay](docs/ffplay.md)
6975
- [More Notes](https://github.com/studying-notes/ffmpeg-notes)
7076

@@ -264,7 +270,11 @@ vtools.read_frame_as_jpeg(src="src", frame=10)
264270
```python
265271
from ffmpeg import atools
266272

267-
atools.convert_audio_to_raw_pcm(src="src")
273+
audio = '/path/to/audio.m4a'
274+
dst = '/path/to/dst.pcm'
275+
276+
atools.convert_audio_to_raw_pcm(src=audio, dst=None)
277+
atools.convert_audio_to_raw_pcm(src=audio, dst=dst)
268278
```
269279

270280
### Assemble Video from Sequence of Frames

ffmpeg/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Date: 2021.02.25 14:34:07
33
Description: Omit
44
LastEditors: Rustle Karl
5-
LastEditTime: 2021.05.08 08:38:45
5+
LastEditTime: 2021.05.31 10:38:26
66
'''
77
import subprocess
88

@@ -48,7 +48,7 @@
4848
'vtools',
4949
]
5050

51-
__version__ = '1.0.1'
51+
__version__ = '1.0.2'
5252

5353

5454
def run_ffmpeg(option: str = None, stdout=None, check=True, **kwargs) -> subprocess.CompletedProcess:

ffmpeg/_ffmpeg.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Date: 2021.02.25 15:02:34
33
Description: Omit
44
LastEditors: Rustle Karl
5-
LastEditTime: 2021.05.04 23:37:24
5+
LastEditTime: 2021.05.10 12:33:20
66
'''
77
from pathlib import Path
88

@@ -33,12 +33,11 @@ def input(source, video_device: str = None, audio_device: str = None, format: st
3333

3434
kwargs['source'] = str(source)
3535

36-
if settings.CUDA_ENABLE and enable_cuda and Path(source).suffix not in constants.IMAGE_FORMATS:
36+
if settings.CUDA_ENABLE and enable_cuda and \
37+
Path(source).suffix not in constants.IMAGE_FORMATS:
38+
hwaccel = "cuda"
3739
if vcodec not in constants.CUDA_ENCODERS:
38-
vcodec = None
39-
40-
kwargs['hwaccel'] = "cuda"
41-
kwargs['vcodec'] = settings.DEFAULT_DECODER
40+
vcodec = settings.DEFAULT_DECODER
4241

4342
kwargs = drop_empty_dict_values(kwargs, hwaccel=hwaccel, vcodec=vcodec,
4443
f=format, pix_fmt=pixel_format, ss=start_position,
@@ -98,9 +97,12 @@ def output(*streams_or_source, vn=False, an=False, ar=None, ab=None, ac=None,
9897
if settings.CUDA_ENABLE and enable_cuda and not preview and \
9998
Path(kwargs['source']).suffix not in constants.IMAGE_FORMATS:
10099
if vcodec not in constants.CUDA_ENCODERS:
101-
vcodec = None
100+
vcodec = settings.DEFAULT_ENCODER
102101

103-
kwargs['vcodec'] = settings.DEFAULT_ENCODER
102+
# codec over acodec/vcodec
103+
if codec is not None:
104+
acodec = None
105+
vcodec = None
104106

105107
if video_bitrate is not None:
106108
kwargs['b:v'] = video_bitrate

ffmpeg/_node.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
Date: 2021.02.25 14:34:07
33
Description: Omit
44
LastEditors: Rustle Karl
5-
LastEditTime: 2021.04.29 11:01:29
5+
LastEditTime: 2021.05.24 07:34:34
66
'''
77
from __future__ import annotations
88

9+
from enum import Enum
910
from typing import Any, Dict, List, Tuple, Type, Union
1011

1112
from ._dag import DagEdge, DagNode, Edge, get_outgoing_edges
@@ -114,7 +115,9 @@ def __getitem__(self, index: str) -> Stream:
114115
return self._node.stream(label=self._label, selector=index)
115116

116117

117-
class NodeTypes(object):
118+
class NodeTypes(str, Enum):
119+
# https://stackoverflow.com/questions/58608361/string-based-enum-in-python
120+
118121
Base = 'Base'
119122
Input = 'Input'
120123
Filter = 'Filter'

ffmpeg/_utils.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
LastEditors: Rustle Karl
55
LastEditTime: 2021.05.04 23:37:10
66
'''
7-
from typing import Dict, Iterable, List
7+
from typing import Dict, Iterable, List, Union
88

99
_backslash = '\\'
1010
_empty_symbols = (None, '', [], {}) # exclude 0
@@ -83,11 +83,20 @@ def string_to_seconds(clock: str) -> int:
8383
if isinstance(clock, (int, float)):
8484
return clock
8585

86-
hours, minutes, seconds = [int(c) for c in clock.split(":")]
86+
clock = [int(c) for c in clock.split(":")]
87+
if len(clock) == 0:
88+
hours, minutes, seconds = 0, 0, 0
89+
elif len(clock) == 1:
90+
hours, minutes, seconds = 0, 0, clock[0]
91+
elif len(clock) == 2:
92+
hours, minutes, seconds = 0, clock[0], clock[1]
93+
else:
94+
hours, minutes, seconds = clock[0], clock[1], clock[2]
95+
8796
return hours * 60 * 60 + minutes * 60 + seconds
8897

8998

90-
def seconds_to_string(seconds: float) -> str:
99+
def seconds_to_string(seconds: Union[float, int, str]) -> str:
91100
if isinstance(seconds, str):
92101
return seconds
93102

ffmpeg/nodes.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import copy
1010
import os
11+
import shutil
1112
import subprocess
1213
from collections import defaultdict
1314
from pathlib import Path
@@ -119,6 +120,11 @@ def run_async(self, *, executable="ffmpeg", direct_print=True, join_args=False,
119120
pipe_stdin=False, pipe_stdout=True, pipe_stderr=True, quiet=False,
120121
overwrite=True, progress='') -> subprocess.Popen:
121122
'''Asynchronously invoke ffmpeg for the supplied node graph.'''
123+
if shutil.which(executable) is None:
124+
raise FileNotFoundError(f"Can't find {executable} in $PATH or "
125+
f"current directory. Please specify a absolute path or "
126+
f"add {executable} into $PATH.")
127+
122128
cmd_args_seq = self.compile(
123129
executable=executable,
124130
direct_print=direct_print,
@@ -179,9 +185,10 @@ def output(self, *streams_or_source, vn=False, an=False, ar=None, ab=None, ac=No
179185
acodec=None, vcodec=None, codec: str = None, aq_scale=None, vq_scale=None,
180186
aspect=None, fps=None, format=None, pixel_format=None, video_bitrate=None,
181187
audio_bitrate=None, v_profile=None, preset=None, mov_flags=None,
182-
shortest=False, frame_size=None, v_frames: int = None, start_position: float = None,
183-
duration: float = None, video_filter: str = None, audio_filter: str = None, ignore_output=False,
184-
preview: bool = False, enable_cuda=True, args: list = None, **kwargs) -> OutputStream:
188+
shortest=False, frame_size=None, v_frames: int = None, duration: Union[float, int, str] = None,
189+
start_position: Union[float, int, str] = None, video_filter: str = None,
190+
audio_filter: str = None, ignore_output=False, preview: bool = False,
191+
enable_cuda=True, args: list = None, **kwargs) -> OutputStream:
185192
raise NotImplementedError
186193

187194
def filter(self, *args, **kwargs) -> FilterableStream:

requirements.txt

-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
graphviz~=0.16
2-
matplotlib~=3.2.2
3-
numpy~=1.19.5
4-
pillow~=8.0.1
51
project-pkgs~=1.0.0
6-
setuptools~=49.2.1
7-
tqdm~=4.47.0
8-
psutil~=5.8.0

setup.py

-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111

1212
# What packages are required for this module to be executed?
1313
requires = [
14-
'graphviz',
1514
'project-pkgs',
16-
'tqdm',
1715
]
1816

1917
# Import the README and use it as the long-description.

0 commit comments

Comments
 (0)