ffmpeg-python icon indicating copy to clipboard operation
ffmpeg-python copied to clipboard

Help with FFMPEG Python and RTMP stream

Open andrewvictor opened this issue 1 year ago • 1 comments

Hey folks,

I was hoping one of you fine folks could translate this:

"ffmpeg -f lavfi -i anullsrc=r=16000:cl=mono -f v4l2 -r 24 -i /dev/video0 -c:v libx264 -pix_fmt yuv420p -preset ultrafast -g 20 -b:v 2500k -c:a aac -ar 44100 -threads 0 -bufsize 512k -strict experimental -f flv rtmp://a.rtmp.youtube.com/live2/<STREAM KEY>"

into FFMPEG-Python. This works for me but I don't have any control over starting and stopping the stream via line commands.

Any help would be greatly appreciated!

andrewvictor avatar Dec 20 '23 03:12 andrewvictor

here piece of code what i know how to live on youtube

import ffmpeg

payload = {
    'video': 'test.mp4',
    'fps': 'original',
    'size': 'original',
    'rtmp': 'output.mp4', # change to rtmp://a.rtmp.youtube.com/live2/
    'bitrate': '2500',
    'bufsize': '5000',
    'preset': 'veryfast',
}

# Open the video file
media = ffmpeg.input(payload['video'], stream_loop=0, re=None)

# Set the video stream to decode and open the video codec
video_stream = media.video

# Create a new stream with the desired frame rate
if 'original' not in payload['fps']:
    video_stream = ffmpeg.filter(video_stream, 'fps', fps=payload['fps'])

if 'original' not in payload['size']:
    width, height = payload['size'].split('x')
    video_stream = ffmpeg.filter(video_stream, 'scale', width, height, force_original_aspect_ratio="decrease")

# Open the audio stream (if present) and the audio codec
audio_stream = media.audio

"""
dont apply more effect or ffmpeg cpu usage will very high !!!
merge audio 
"""
media = ffmpeg.concat(video_stream, audio_stream, v=1, a=1)

ffmpegProcess = (
    media
    .output(                  
        filename=payload['rtmp'],
        # video encode
        video_bitrate=f'{payload["bitrate"]}k',                    
        maxrate=f'{payload["bitrate"]}k',
        bufsize=f'{payload["bufsize"]}k',
        vcodec='libx264',
        # audio encode
        audio_bitrate='128k', # a:b
        ar='44100', # a:r
        acodec='aac',
        # result          
        # r=payload['fps'], # smooth playback 20fps (frame rate) 
        g=50, # smooth playback 20 gop size (group of pictures)
        # crf=20, # convert pixel  Constant Rate Factor (CRF)
        pix_fmt='yuv420p', # pixel formater
        preset=f'{payload["preset"]}',
        format='flv',
        threads='1'
    )
    .global_args(
        '-hide_banner',
        # '-progress', 'pipe:1'
    )
    .overwrite_output()
    .run_async(
        # pipe_stdin=True,
        # pipe_stdout=True,
        # pipe_stderr=True,
        # cmd=ffmpegPath
    )
)

ffmpegProcess.wait()

irfanykywz avatar Dec 26 '23 06:12 irfanykywz