Skip to content

Custom FFmpeg Commands in WriteGear API

Abhishek Thakur edited this page Apr 30, 2020 · 10 revisions

VidGear Logo

Custom FFmpeg Commands in WriteGear API

WriteGear API now provides the execute_ffmpeg_cmd function that enables the user to pass any custom command as an input to its internal FFmpeg Pipeline in the Compression Mode.

This opens endless possibilities of exploiting every FFmpeg params within WriteGear without relying on a third-party API to do the same and while doing that it robustly handles all errors/warnings quietly. A user can now pass any custom Terminal command (that works on the terminal) directly to the WriteGear's FFmpeg pipeline by formating it as a list.

 

 

Table of Contents:

 

 

Features :

  • Provides the ability to pass any custom command to WriteGear FFmpeg Pipeline.

  • Compatible with any FFmpeg terminal command.

  • Standalone On-the-fly functioning.

  • Can work without interfering with WriteGear API's Writer pipeline.

  • Minimum hassle and extremely easy to enable and use.

 

 


Important Information ⚠️

  • Requires WriteGear's Compression Mode enabled(compression_mode = True):

    • requires FFmpeg installation for Compression capabilities. Follow this WIKI for FFmpeg installation.

    • In case WriteGear fails to detect valid FFmpeg executables on your system(even if Compression Mode is enabled), it can automatically fallbacks to Non-Compression Mode.

  • Only Python list is a valid datatype supported as input by this function!

  • Read FFmpeg Docs carefully, before passing any params to execute_ffmpeg_cmd function, Otherwise wrong parameters may result in undesired Errors or no Output.


 

 

Functions:

  • execute_ffmpeg_cmd (function): allows the users to pass the custom FFmpeg terminal commands as a formatted list directly to WriteGear API's FFmpeg pipeline for processing/execution. Its usage is as follows:

    #format FFmpeg terminal command `ffmpeg -y -i source_video -acodec copy input_audio.aac` as a list
    ffmpeg_command = ['-y', '-i', source_video, '-acodec', 'copy', 'input_audio.aac']
    
    #execute this list using this function
    execute_ffmpeg_cmd(ffmpeg_command)

 

 

Usage:

Following usage examples is just an idea of what can be done with this powerful function. So just Tinker with various FFmpeg parameters/commands yourself and see it working and if you're unable to run any terminal FFmpeg command then report an issue.

 

A. Basic Example (Separating Audio from a Video):

In this example, we will extract and save audio from a URL stream:

# import required libraries
from vidgear.gears import WriteGear

#define a valid url
url_to_stream = 'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4'

# Define writer with default parameters
writer = WriteGear(output_filename = 'Output.mp4', logging = True)  

#format command to convert stream audio as 'output_audio.aac' as list
ffmpeg_command_to_save_audio = ['-y', '-i', url_to_stream, 'output_audio.aac'] # `-y` parameter is to overwrite outputfile if exists

#execute FFmpeg command
writer.execute_ffmpeg_cmd(ffmpeg_command_to_save_audio)

# safely close writer
writer.close()

After running this script, You will get the final 'output_audio.aac' audio file.

 

B. Basic Example (Adding Audio to a Video):

In this example, we will merge audio with video:


⚠️ Example Assumptions:

  • You already have a separate video(i.e 'input-video.mp4') and audio(i.e 'input-audio.aac') files.

  • Both these Audio and Video files are of equal duration.

  • FFmpeg installed: Follow this WIKI for FFmpeg installation on your machine.


# import required libraries
from vidgear.gears import VideoGear
from vidgear.gears import WriteGear
import cv2
import time

#Open input video stream
stream = VideoGear(source='input-video.mp4').start()

#set input audio stream path 
input_audio = "input-audio.aac"

#define your parameters
output_params = {"-input_framerate":stream.framerate} #output framerate must match source framerate

# Define writer with defined parameters and suitable output filename for e.g. `Output.mp4`
writer = WriteGear(output_filename = 'Output.mp4', **output_params)

# loop over
while True:

    # read frames from stream
    frame = stream.read()

    # check for frame if Nonetype
    if frame is None:
        break


    # {do something with the frame here}


    # write frame to writer
    writer.write(frame)

    # Show output window
    cv2.imshow("Output Frame", frame)

    # check for 'q' key if pressed
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# close output window
cv2.destroyAllWindows()

# safely close video stream
stream.stop()

# safely close writer
writer.close()


#sleep 1 sec as the above video might still be rendering
time.sleep(1)


#format FFmpeg command to generate `Output_with_audio.mp4` by merging input_audio in above rendered `Output.mp4` 
ffmpeg_command = ['-y', '-i', 'Output.mp4', '-i', input_audio, '-c:v', 'copy', '-c:a', 'copy', '-map', '0:v:0', '-map', '1:a:0', '-shortest', 'Output_with_audio.mp4'] # `-y` parameter is to overwrite outputfile if exists

#execute FFmpeg command
writer.execute_ffmpeg_cmd(ffmpeg_command)

After running this script, You will get the final 'Output_with_audio.mp4' file with both video and audio merged.