Giter Club home page Giter Club logo

Comments (1)

WyattBlue avatar WyattBlue commented on September 12, 2024

For filters like atempo, you need to push multiple frames before the filter starts making output frames. See my example for how you would do that in code:

import av
from av.filter.context import FilterContext

import os
import time
import tempfile


def link_nodes(*nodes: FilterContext) -> None:
    for c, n in zip(nodes, nodes[1:]):
        c.link_to(n)


def main() -> None:
    stretched_aud_path = "out.mp3"

    audio_in_container = av.open("test.wav", mode="r", format="wav")
    audio_in_stream = audio_in_container.streams.audio[0]

    if os.path.exists(stretched_aud_path):
        os.remove(stretched_aud_path)

    audio_out_container = av.open(stretched_aud_path, mode="w", format="wav")
    output_stream = audio_out_container.add_stream(codec_name="mp3", rate=48000)

    # Setup filter graph
    filter_graph = av.filter.Graph()
    link_nodes(
        filter_graph.add_abuffer(template=audio_in_stream),
        filter_graph.add("atempo", "2.0"),
        filter_graph.add("abuffersink"),
    )
    filter_graph.configure()

    # Render audio
    for frame in audio_in_container.decode(audio=0):
        filter_graph.push(frame)
        while True:
            try:
                output_frame = filter_graph.pull()
                for packet in output_stream.encode(output_frame):
                    audio_out_container.mux(packet)
            except av.BlockingIOError:
                break

    # After pushing all frames, flush the filter graph
    for packet in output_stream.encode(None):
        audio_out_container.mux(packet)

    audio_out_container.close()
    audio_in_container.close()


if __name__ == "__main__":
    main()

from pyav.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.