Compare commits
22 Commits
main
...
e0124c0ff3
| Author | SHA1 | Date | |
|---|---|---|---|
|
e0124c0ff3
|
|||
|
7e14afcb84
|
|||
|
7ea498edc8
|
|||
|
2b4ece9bee
|
|||
|
8929fc9bac
|
|||
|
83029fbc44
|
|||
|
6fd5286b15
|
|||
|
fb07373122
|
|||
|
51fa25809d
|
|||
|
de6c3a4e7f
|
|||
|
2ed4c7b6d8
|
|||
|
4afaf287bb
|
|||
|
5825c67f30
|
|||
|
97dd6c691b
|
|||
|
b0c3fbc1b1
|
|||
|
7cf7a8cb9a
|
|||
|
5af462fea2
|
|||
|
417238626b
|
|||
|
b31704420f
|
|||
|
dfd0902256
|
|||
|
2316435af8
|
|||
|
3c56a880da
|
5
.gitignore
vendored
5
.gitignore
vendored
@ -6,10 +6,11 @@ __pycache__/
|
|||||||
|
|
||||||
# C extensions
|
# C extensions
|
||||||
*.so
|
*.so
|
||||||
|
*.so.*
|
||||||
|
|
||||||
# Distribution / packaging
|
# Distribution / packaging
|
||||||
.Python
|
.Python
|
||||||
build/
|
/build/
|
||||||
develop-eggs/
|
develop-eggs/
|
||||||
dist/
|
dist/
|
||||||
downloads/
|
downloads/
|
||||||
@ -174,3 +175,5 @@ cython_debug/
|
|||||||
# PyPI configuration file
|
# PyPI configuration file
|
||||||
.pypirc
|
.pypirc
|
||||||
|
|
||||||
|
# PVE
|
||||||
|
/test.mp4
|
||||||
|
|||||||
10
.gitmodules
vendored
Normal file
10
.gitmodules
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
[submodule "ffmpeg"]
|
||||||
|
path = ffmpeg
|
||||||
|
url = https://git.ffmpeg.org/ffmpeg.git
|
||||||
|
branch = release/8.0
|
||||||
|
shallow = true
|
||||||
|
[submodule "codecs/x264"]
|
||||||
|
path = codecs/x264
|
||||||
|
url = https://code.videolan.org/videolan/x264.git
|
||||||
|
branch = stable
|
||||||
|
shallow = true
|
||||||
1
codecs/x264
Submodule
1
codecs/x264
Submodule
Submodule codecs/x264 added at b35605ace3
1
ffmpeg
Submodule
1
ffmpeg
Submodule
Submodule ffmpeg added at d8605a6b55
63
make/configure-ffmpeg.sh
Executable file
63
make/configure-ffmpeg.sh
Executable file
@ -0,0 +1,63 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
source_directory="ffmpeg"
|
||||||
|
include_directory="build/include"
|
||||||
|
lib_directory="build/lib"
|
||||||
|
build_directory="build/${source_directory}"
|
||||||
|
install_directory="build"
|
||||||
|
install_library_directory="pyav"
|
||||||
|
|
||||||
|
mkdir -p ${build_directory}
|
||||||
|
mkdir -p ${install_directory}
|
||||||
|
|
||||||
|
absolute_source_directory=$(realpath ${source_directory})
|
||||||
|
absolute_include_directory=$(realpath ${include_directory})
|
||||||
|
absolute_lib_directory=$(realpath ${lib_directory})
|
||||||
|
absolute_build_directory=$(realpath ${build_directory})
|
||||||
|
absolute_install_directory=$(realpath ${install_directory})
|
||||||
|
absolute_install_library_directory=$(realpath ${install_library_directory})
|
||||||
|
|
||||||
|
cd "${absolute_build_directory}"
|
||||||
|
|
||||||
|
PKG_CONFIG_PATH="${absolute_lib_directory}/pkgconfig" \
|
||||||
|
${absolute_source_directory}/configure \
|
||||||
|
--prefix=${absolute_install_directory} \
|
||||||
|
--pkg-config-flags="--static" \
|
||||||
|
--extra-libs="-lpthread -lm" \
|
||||||
|
--extra-cflags="-I${absolute_include_directory}" \
|
||||||
|
--extra-ldflags="-L${absolute_lib_directory}" \
|
||||||
|
--disable-static \
|
||||||
|
--enable-shared \
|
||||||
|
--enable-rpath \
|
||||||
|
--shlibdir=${absolute_install_library_directory} \
|
||||||
|
--disable-programs \
|
||||||
|
--disable-avdevice \
|
||||||
|
--disable-avfilter \
|
||||||
|
--disable-swresample \
|
||||||
|
--disable-swscale \
|
||||||
|
--disable-network \
|
||||||
|
--disable-manpages \
|
||||||
|
--disable-podpages \
|
||||||
|
--disable-txtpages \
|
||||||
|
--enable-gpl \
|
||||||
|
--enable-libx264 \
|
||||||
|
--disable-xlib \
|
||||||
|
--disable-libxcb \
|
||||||
|
--disable-libxcb-shm \
|
||||||
|
--disable-alsa \
|
||||||
|
--disable-iconv
|
||||||
44
make/configure-x264.sh
Executable file
44
make/configure-x264.sh
Executable file
@ -0,0 +1,44 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
source_directory="codecs/x264"
|
||||||
|
build_directory="build/${source_directory}"
|
||||||
|
install_directory="build"
|
||||||
|
|
||||||
|
mkdir -p ${build_directory}
|
||||||
|
mkdir -p ${install_directory}
|
||||||
|
|
||||||
|
absolute_source_directory=$(realpath ${source_directory})
|
||||||
|
absolute_build_directory=$(realpath ${build_directory})
|
||||||
|
absolute_install_directory=$(realpath ${install_directory})
|
||||||
|
|
||||||
|
cd "${absolute_build_directory}"
|
||||||
|
${absolute_source_directory}/configure \
|
||||||
|
--prefix=${absolute_install_directory} \
|
||||||
|
--disable-cli \
|
||||||
|
--enable-static \
|
||||||
|
--enable-shared \
|
||||||
|
--disable-bashcompletion \
|
||||||
|
--bit-depth=all \
|
||||||
|
--chroma-format=all \
|
||||||
|
--enable-lto \
|
||||||
|
--disable-avs \
|
||||||
|
--disable-swscale \
|
||||||
|
--disable-lavf \
|
||||||
|
--disable-ffms \
|
||||||
|
--disable-gpac \
|
||||||
|
--disable-lsmash
|
||||||
19
make/make-all.sh
Executable file
19
make/make-all.sh
Executable file
@ -0,0 +1,19 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
./make/make-x264.sh
|
||||||
|
./make/make-ffmpeg.sh
|
||||||
26
make/make-ffmpeg.sh
Executable file
26
make/make-ffmpeg.sh
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
source_directory="ffmpeg"
|
||||||
|
build_directory="build/${source_directory}"
|
||||||
|
|
||||||
|
if ! [ -d ${build_directory} ] ; then
|
||||||
|
./make/configure-ffmpeg.sh || exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
cd ${build_directory}
|
||||||
|
make && make install
|
||||||
26
make/make-x264.sh
Executable file
26
make/make-x264.sh
Executable file
@ -0,0 +1,26 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
source_directory="codecs/x264"
|
||||||
|
build_directory="build/${source_directory}"
|
||||||
|
|
||||||
|
if ! [ -d ${build_directory} ] ; then
|
||||||
|
./make/configure-x264.sh || exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
cd ${build_directory}
|
||||||
|
make && make install
|
||||||
48
pve.py
Normal file
48
pve.py
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import cProfile
|
||||||
|
|
||||||
|
from pyav.demuxer import Demuxer
|
||||||
|
from pyav.decoder import Decoder
|
||||||
|
|
||||||
|
demuxer = Demuxer('test.mp4')
|
||||||
|
|
||||||
|
print(f"nb_streams = {demuxer.nb_streams}")
|
||||||
|
print(f"video codec = {demuxer.video_stream.codec.name}")
|
||||||
|
print(f"audio codec = {demuxer.audio_stream.codec.name}")
|
||||||
|
|
||||||
|
video_decoder = Decoder(demuxer.video_stream)
|
||||||
|
audio_decoder = Decoder(demuxer.audio_stream)
|
||||||
|
|
||||||
|
num_frames = 0
|
||||||
|
|
||||||
|
with cProfile.Profile() as pr:
|
||||||
|
while True:
|
||||||
|
packet = demuxer.read_packet()
|
||||||
|
eof = (packet is None)
|
||||||
|
if eof or demuxer.video_stream.contains(packet):
|
||||||
|
video_frames = video_decoder.decode(packet)
|
||||||
|
num_frames += len(video_frames)
|
||||||
|
# print(f"decoded {len(video_frames)} video frames")
|
||||||
|
if eof or demuxer.audio_stream.contains(packet):
|
||||||
|
audio_frames = audio_decoder.decode(packet)
|
||||||
|
# print(f"decoded {len(audio_frames)} audio frames")
|
||||||
|
if eof:
|
||||||
|
break
|
||||||
|
|
||||||
|
print(f"num frames: {num_frames}")
|
||||||
|
pr.print_stats()
|
||||||
22
pve.sh
Executable file
22
pve.sh
Executable file
@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
shared_library_path="./pyav"
|
||||||
|
absolute_shared_library_path=$(realpath ${shared_library_path})
|
||||||
|
|
||||||
|
LD_LIBRARY_PATH=${absolute_shared_library_path} \
|
||||||
|
python3 pve.py
|
||||||
15
pyav/__init__.py
Normal file
15
pyav/__init__.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
29
pyav/codec.py
Normal file
29
pyav/codec.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
class Codec:
|
||||||
|
__slots__ = '_ref'
|
||||||
|
|
||||||
|
def __init__(self, ref):
|
||||||
|
self._ref = ref
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _as_parameter_(self):
|
||||||
|
return self._ref
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
return self._ref.contents.name.decode("utf-8")
|
||||||
58
pyav/decoder.py
Normal file
58
pyav/decoder.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from . import libav
|
||||||
|
from .packet import Packet
|
||||||
|
from .frame import Frame
|
||||||
|
|
||||||
|
class Decoder:
|
||||||
|
__slots__ = '_context'
|
||||||
|
|
||||||
|
def __init__(self, stream):
|
||||||
|
self._context = libav.codec_alloc_context(stream.codec)
|
||||||
|
if not self._context:
|
||||||
|
raise MemoryError
|
||||||
|
errcode = libav.codec_parameters_to_context(self._context, stream.parameters)
|
||||||
|
if errcode < 0:
|
||||||
|
libav.codec_free_context(self._context)
|
||||||
|
raise Exception("Failed to set context parameters")
|
||||||
|
errcode = libav.codec_open(self._context, stream.codec)
|
||||||
|
if errcode < 0:
|
||||||
|
libav.codec_free_context(self._context)
|
||||||
|
raise Exception("Failed to open codec context")
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self._context:
|
||||||
|
libav.codec_free_context(self._context)
|
||||||
|
|
||||||
|
def decode(self, packet):
|
||||||
|
if not self._context:
|
||||||
|
return None
|
||||||
|
errcode = libav.codec_send_packet(self._context, packet)
|
||||||
|
if errcode < 0:
|
||||||
|
errstring = libav.strerror(errcode)
|
||||||
|
raise Exception(f"Failed to send packet: {errstring}")
|
||||||
|
frames = []
|
||||||
|
while True:
|
||||||
|
frame = Frame()
|
||||||
|
errcode = libav.codec_receive_frame(self._context, frame)
|
||||||
|
if errcode in (libav.AVERROR_EOF, libav.AVERROR_EAGAIN):
|
||||||
|
break
|
||||||
|
elif errcode < 0:
|
||||||
|
errstring = libav.strerror(errcode)
|
||||||
|
raise Exception(f"Failed to receive frame: {errstring}")
|
||||||
|
frames.append(frame)
|
||||||
|
return frames
|
||||||
69
pyav/demuxer.py
Normal file
69
pyav/demuxer.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from . import libav
|
||||||
|
from .codec import Codec
|
||||||
|
from .stream import Stream
|
||||||
|
from .packet import Packet
|
||||||
|
|
||||||
|
class Demuxer:
|
||||||
|
__slots__ = '_context', 'video_stream', 'audio_stream'
|
||||||
|
|
||||||
|
def __init__(self, path):
|
||||||
|
self._context = libav.format_alloc_context()
|
||||||
|
if not self._context:
|
||||||
|
raise MemoryError
|
||||||
|
errcode = libav.format_open_input(self._context, "file:" + path)
|
||||||
|
if errcode < 0:
|
||||||
|
raise Exception(f"Failed to open: {path}")
|
||||||
|
errcode = libav.format_find_stream_info(self._context)
|
||||||
|
if errcode < 0:
|
||||||
|
libav.format_close_input(self._context)
|
||||||
|
raise Exception("Failed to find stream info")
|
||||||
|
self.video_stream = self._find_stream(libav.AVMEDIA_TYPE_VIDEO)
|
||||||
|
if self.video_stream is None:
|
||||||
|
libav.format_close_input(self._context)
|
||||||
|
raise Exception("Failed to find a video stream")
|
||||||
|
self.audio_stream = self._find_stream(libav.AVMEDIA_TYPE_AUDIO)
|
||||||
|
if self.audio_stream is None:
|
||||||
|
libav.format_close_input(self._context)
|
||||||
|
raise Exception("Failed to find an audio stream")
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self._context:
|
||||||
|
libav.format_close_input(self._context)
|
||||||
|
|
||||||
|
def _find_stream(self, type):
|
||||||
|
index, codec_ref = libav.format_find_best_stream(self._context, type)
|
||||||
|
if index < 0 or not codec_ref:
|
||||||
|
return None
|
||||||
|
parameters = self._context.contents.streams[index].contents.codecpar
|
||||||
|
return Stream(index, Codec(codec_ref), parameters)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def nb_streams(self):
|
||||||
|
if not self._context:
|
||||||
|
return 0
|
||||||
|
return self._context.contents.nb_streams
|
||||||
|
|
||||||
|
def read_packet(self):
|
||||||
|
if not self._context:
|
||||||
|
return None
|
||||||
|
packet = Packet()
|
||||||
|
errcode = libav.read_frame(self._context, packet)
|
||||||
|
if errcode < 0:
|
||||||
|
return None
|
||||||
|
return packet
|
||||||
33
pyav/frame.py
Normal file
33
pyav/frame.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from . import libav
|
||||||
|
|
||||||
|
class Frame:
|
||||||
|
__slots__ = '_ref'
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._ref = libav.frame_alloc()
|
||||||
|
if not self._ref:
|
||||||
|
raise MemoryError
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self._ref:
|
||||||
|
libav.frame_free(self._ref)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _as_parameter_(self):
|
||||||
|
return self._ref
|
||||||
269
pyav/libav.py
Normal file
269
pyav/libav.py
Normal file
@ -0,0 +1,269 @@
|
|||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import errno
|
||||||
|
import ctypes
|
||||||
|
from pathlib import PurePath
|
||||||
|
|
||||||
|
shared_library_path = PurePath(__file__).parent
|
||||||
|
|
||||||
|
def load_shared_library(name):
|
||||||
|
path = str(shared_library_path / ("lib" + name + ".so"))
|
||||||
|
return ctypes.cdll.LoadLibrary(path)
|
||||||
|
|
||||||
|
_avutil = load_shared_library("avutil")
|
||||||
|
_avformat = load_shared_library("avformat")
|
||||||
|
_avcodec = load_shared_library("avcodec")
|
||||||
|
|
||||||
|
def _errtag(a, b, c, d):
|
||||||
|
return -(ord(a) | (ord(b) << 8) | (ord(c) << 16) | (ord(d) << 24))
|
||||||
|
|
||||||
|
if errno.EAGAIN < 0:
|
||||||
|
AVERROR_EAGAIN = errno.EAGAIN
|
||||||
|
else:
|
||||||
|
AVERROR_EAGAIN = -errno.EAGAIN
|
||||||
|
|
||||||
|
AVERROR_EOF = _errtag('E', 'O', 'F', ' ')
|
||||||
|
|
||||||
|
_AV_ERROR_MAX_STRING_SIZE = 64
|
||||||
|
|
||||||
|
AVMEDIA_TYPE_UNKNOWN = -1
|
||||||
|
AVMEDIA_TYPE_VIDEO = 0
|
||||||
|
AVMEDIA_TYPE_AUDIO = 1
|
||||||
|
AVMEDIA_TYPE_DATA = 2
|
||||||
|
AVMEDIA_TYPE_SUBTITLE = 3
|
||||||
|
AVMEDIA_TYPE_ATTACHMENT = 4
|
||||||
|
|
||||||
|
AV_NUM_DATA_POINTERS = 8
|
||||||
|
|
||||||
|
c_uint8_p = ctypes.POINTER(ctypes.c_uint8)
|
||||||
|
c_uint8_pp = ctypes.POINTER(c_uint8_p)
|
||||||
|
|
||||||
|
class AVRational(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
("num", ctypes.c_int),
|
||||||
|
("den", ctypes.c_int)]
|
||||||
|
|
||||||
|
class AVFrame(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
("data", c_uint8_p * AV_NUM_DATA_POINTERS),
|
||||||
|
("linesize", ctypes.c_int * AV_NUM_DATA_POINTERS),
|
||||||
|
("extended_data", c_uint8_pp),
|
||||||
|
("width", ctypes.c_int),
|
||||||
|
("height", ctypes.c_int),
|
||||||
|
("nb_samples", ctypes.c_int),
|
||||||
|
("format", ctypes.c_int),
|
||||||
|
("key_frame", ctypes.c_int),
|
||||||
|
("pict_type", ctypes.c_int),
|
||||||
|
("sample_aspect_ratio", AVRational),
|
||||||
|
("pts", ctypes.c_int64),
|
||||||
|
("pkt_dts", ctypes.c_int64),
|
||||||
|
("time_base", AVRational)]
|
||||||
|
# ...
|
||||||
|
|
||||||
|
AVFrame_p = ctypes.POINTER(AVFrame)
|
||||||
|
AVFrame_pp = ctypes.POINTER(AVFrame_p)
|
||||||
|
|
||||||
|
class AVCodecParameters(ctypes.Structure):
|
||||||
|
pass
|
||||||
|
|
||||||
|
AVCodecParameters_p = ctypes.POINTER(AVCodecParameters)
|
||||||
|
|
||||||
|
class AVStream(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
("av_class", ctypes.c_void_p),
|
||||||
|
("index", ctypes.c_int),
|
||||||
|
("id", ctypes.c_int),
|
||||||
|
("codecpar", AVCodecParameters_p),
|
||||||
|
("priv_data", ctypes.c_void_p),
|
||||||
|
("time_base", AVRational)]
|
||||||
|
# ...
|
||||||
|
|
||||||
|
AVStream_p = ctypes.POINTER(AVStream)
|
||||||
|
AVStream_pp = ctypes.POINTER(AVStream_p)
|
||||||
|
|
||||||
|
class AVFormatContext(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
("av_class", ctypes.c_void_p),
|
||||||
|
("iformat", ctypes.c_void_p),
|
||||||
|
("oformat", ctypes.c_void_p),
|
||||||
|
("priv_data", ctypes.c_void_p),
|
||||||
|
("pb", ctypes.c_void_p),
|
||||||
|
("ctx_flags", ctypes.c_int),
|
||||||
|
("nb_streams", ctypes.c_uint),
|
||||||
|
("streams", AVStream_pp)]
|
||||||
|
# ...
|
||||||
|
|
||||||
|
AVFormatContext_p = ctypes.POINTER(AVFormatContext)
|
||||||
|
AVFormatContext_pp = ctypes.POINTER(AVFormatContext_p)
|
||||||
|
|
||||||
|
class AVPacket(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
("buf", ctypes.c_void_p),
|
||||||
|
("pts", ctypes.c_int64),
|
||||||
|
("dts", ctypes.c_int64),
|
||||||
|
("data", ctypes.c_void_p),
|
||||||
|
("size", ctypes.c_int),
|
||||||
|
("stream_index", ctypes.c_int)]
|
||||||
|
# ...
|
||||||
|
|
||||||
|
AVPacket_p = ctypes.POINTER(AVPacket)
|
||||||
|
AVPacket_pp = ctypes.POINTER(AVPacket_p)
|
||||||
|
|
||||||
|
class AVCodec(ctypes.Structure):
|
||||||
|
_fields_ = [
|
||||||
|
("name", ctypes.c_char_p),
|
||||||
|
("long_name", ctypes.c_char_p)]
|
||||||
|
# ...
|
||||||
|
|
||||||
|
AVCodec_p = ctypes.POINTER(AVCodec)
|
||||||
|
AVCodec_pp = ctypes.POINTER(AVCodec_p)
|
||||||
|
|
||||||
|
class AVCodecContext(ctypes.Structure):
|
||||||
|
pass
|
||||||
|
|
||||||
|
AVCodecContext_p = ctypes.POINTER(AVCodecContext)
|
||||||
|
AVCodecContext_pp = ctypes.POINTER(AVCodecContext_p)
|
||||||
|
|
||||||
|
_avutil.av_strerror.restype = ctypes.c_int
|
||||||
|
_avutil.av_strerror.argtypes = [
|
||||||
|
ctypes.c_int, # errno
|
||||||
|
ctypes.c_char_p, # errbuf
|
||||||
|
ctypes.c_size_t] # errbuff_size
|
||||||
|
|
||||||
|
def strerror(errno):
|
||||||
|
errbuf = ctypes.create_string_buffer(_AV_ERROR_MAX_STRING_SIZE)
|
||||||
|
_avutil.av_strerror(errno, errbuf, _AV_ERROR_MAX_STRING_SIZE)
|
||||||
|
return errbuf.value.decode("utf-8")
|
||||||
|
|
||||||
|
_avutil.av_frame_alloc.restype = AVFrame_p
|
||||||
|
_avutil.av_frame_alloc.argtypes = None
|
||||||
|
|
||||||
|
def frame_alloc():
|
||||||
|
return _avutil.av_frame_alloc()
|
||||||
|
|
||||||
|
_avutil.av_frame_free.restype = None
|
||||||
|
_avutil.av_frame_free.argtypes = [AVFrame_pp]
|
||||||
|
|
||||||
|
def frame_free(frame):
|
||||||
|
_avutil.av_frame_free(ctypes.byref(frame))
|
||||||
|
|
||||||
|
_avformat.avformat_alloc_context.restype = AVFormatContext_p
|
||||||
|
_avformat.avformat_alloc_context.argtypes = None
|
||||||
|
|
||||||
|
def format_alloc_context():
|
||||||
|
return _avformat.avformat_alloc_context()
|
||||||
|
|
||||||
|
_avformat.avformat_free_context.restype = None
|
||||||
|
_avformat.avformat_free_context.argtypes = [AVFormatContext_p]
|
||||||
|
|
||||||
|
def format_free_context(context):
|
||||||
|
_avformat.avformat_free_context(context)
|
||||||
|
|
||||||
|
_avformat.avformat_open_input.restype = ctypes.c_int
|
||||||
|
_avformat.avformat_open_input.argtypes = [
|
||||||
|
AVFormatContext_pp,
|
||||||
|
ctypes.c_char_p, # url
|
||||||
|
ctypes.c_void_p, # format
|
||||||
|
ctypes.POINTER(ctypes.c_void_p)] # options
|
||||||
|
|
||||||
|
def format_open_input(context, url):
|
||||||
|
return _avformat.avformat_open_input(ctypes.byref(context), url.encode('ascii', 'ignore'), None, None)
|
||||||
|
|
||||||
|
_avformat.avformat_close_input.restype = None
|
||||||
|
_avformat.avformat_close_input.argtypes = [AVFormatContext_pp]
|
||||||
|
|
||||||
|
def format_close_input(context):
|
||||||
|
_avformat.avformat_close_input(ctypes.byref(context))
|
||||||
|
|
||||||
|
_avformat.avformat_find_stream_info.restype = ctypes.c_int
|
||||||
|
_avformat.avformat_find_stream_info.argtypes = [
|
||||||
|
AVFormatContext_p,
|
||||||
|
ctypes.POINTER(ctypes.c_void_p)] # options
|
||||||
|
|
||||||
|
def format_find_stream_info(context):
|
||||||
|
return _avformat.avformat_find_stream_info(context, None)
|
||||||
|
|
||||||
|
_avformat.av_find_best_stream.restype = ctypes.c_int
|
||||||
|
_avformat.av_find_best_stream.argtypes = [
|
||||||
|
AVFormatContext_p,
|
||||||
|
ctypes.c_int, # type
|
||||||
|
ctypes.c_int, # wanted stream
|
||||||
|
ctypes.c_int, # related stream
|
||||||
|
AVCodec_pp,
|
||||||
|
ctypes.c_int] # flags
|
||||||
|
|
||||||
|
def format_find_best_stream(context, type):
|
||||||
|
codec = AVCodec_p()
|
||||||
|
index = _avformat.av_find_best_stream(context, type, -1, -1, ctypes.byref(codec), 0)
|
||||||
|
return index, codec
|
||||||
|
|
||||||
|
_avformat.av_packet_alloc.restype = AVPacket_p
|
||||||
|
_avformat.av_packet_alloc.argtypes = None
|
||||||
|
|
||||||
|
def packet_alloc():
|
||||||
|
return _avformat.av_packet_alloc()
|
||||||
|
|
||||||
|
_avformat.av_packet_free.restype = None
|
||||||
|
_avformat.av_packet_free.argtypes = [AVPacket_pp]
|
||||||
|
|
||||||
|
def packet_free(packet):
|
||||||
|
_avformat.av_packet_free(ctypes.byref(packet))
|
||||||
|
|
||||||
|
_avformat.av_read_frame.restype = ctypes.c_int
|
||||||
|
_avformat.av_read_frame.argtypes = [AVFormatContext_p, AVPacket_p]
|
||||||
|
|
||||||
|
def read_frame(context, packet):
|
||||||
|
return _avformat.av_read_frame(context, packet)
|
||||||
|
|
||||||
|
_avcodec.avcodec_alloc_context3.restype = AVCodecContext_p
|
||||||
|
_avcodec.avcodec_alloc_context3.argtypes = [AVCodec_p]
|
||||||
|
|
||||||
|
def codec_alloc_context(codec):
|
||||||
|
return _avcodec.avcodec_alloc_context3(codec)
|
||||||
|
|
||||||
|
_avcodec.avcodec_free_context.restype = None
|
||||||
|
_avcodec.avcodec_free_context.argtypes = [AVCodecContext_pp]
|
||||||
|
|
||||||
|
def codec_free_context(context):
|
||||||
|
_avcodec.avcodec_free_context(ctypes.byref(context))
|
||||||
|
|
||||||
|
_avcodec.avcodec_parameters_to_context.restype = ctypes.c_int
|
||||||
|
_avcodec.avcodec_parameters_to_context.argtypes = [AVCodecContext_p, AVCodecParameters_p]
|
||||||
|
|
||||||
|
def codec_parameters_to_context(context, parameters):
|
||||||
|
return _avcodec.avcodec_parameters_to_context(context, parameters)
|
||||||
|
|
||||||
|
_avcodec.avcodec_open2.restype = ctypes.c_int
|
||||||
|
_avcodec.avcodec_open2.argtypes = [
|
||||||
|
AVCodecContext_p,
|
||||||
|
AVCodec_p,
|
||||||
|
ctypes.POINTER(ctypes.c_void_p)] # options
|
||||||
|
|
||||||
|
def codec_open(context, codec):
|
||||||
|
return _avcodec.avcodec_open2(context, codec, None)
|
||||||
|
|
||||||
|
_avcodec.avcodec_send_packet.restype = ctypes.c_int
|
||||||
|
_avcodec.avcodec_send_packet.argtypes = [AVCodecContext_p, AVPacket_p]
|
||||||
|
|
||||||
|
def codec_send_packet(context, packet):
|
||||||
|
return _avcodec.avcodec_send_packet(context, packet)
|
||||||
|
|
||||||
|
_avcodec.avcodec_receive_frame.restype = ctypes.c_int
|
||||||
|
_avcodec.avcodec_receive_frame.argtypes = [AVCodecContext_p, AVFrame_p]
|
||||||
|
|
||||||
|
def codec_receive_frame(context, frame):
|
||||||
|
return _avcodec.avcodec_receive_frame(context, frame)
|
||||||
39
pyav/packet.py
Normal file
39
pyav/packet.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
from . import libav
|
||||||
|
|
||||||
|
class Packet:
|
||||||
|
__slots__ = '_ref'
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._ref = libav.packet_alloc()
|
||||||
|
if not self._ref:
|
||||||
|
raise MemoryError
|
||||||
|
|
||||||
|
def __del__(self):
|
||||||
|
if self._ref:
|
||||||
|
libav.packet_free(self._ref)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def _as_parameter_(self):
|
||||||
|
return self._ref
|
||||||
|
|
||||||
|
@property
|
||||||
|
def stream_index(self):
|
||||||
|
if self._ref:
|
||||||
|
return self._ref.contents.stream_index
|
||||||
|
return -1
|
||||||
26
pyav/stream.py
Normal file
26
pyav/stream.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# People's Video Editor: high quality, GPU accelerated mp4 editor
|
||||||
|
# Copyright (C) 2025 Roz K <roz@rozk.net>
|
||||||
|
#
|
||||||
|
# This file is part of People's Video Editor.
|
||||||
|
#
|
||||||
|
# People's Video Editor is free software: you can redistribute it and/or modify it under the terms of the
|
||||||
|
# GNU General Public License as published by the Free Software Foundation, either version 3 of the License,
|
||||||
|
# or (at your option) any later version.
|
||||||
|
#
|
||||||
|
# People's Video Editor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||||
|
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License along with People's Video Editor.
|
||||||
|
# If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
class Stream:
|
||||||
|
__slots__ = 'index', 'codec', 'parameters'
|
||||||
|
|
||||||
|
def __init__(self, index, codec, parameters):
|
||||||
|
self.index = index
|
||||||
|
self.codec = codec
|
||||||
|
self.parameters = parameters
|
||||||
|
|
||||||
|
def contains(self, packet):
|
||||||
|
return (self.index == packet.stream_index)
|
||||||
Reference in New Issue
Block a user