Skip to content

Custom FFmpeg Commands in WriteGear API

Abhishek Thakur edited this page Apr 25, 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:

from vidgear.gears import WriteGear

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

#Define writer
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)

#clean resources
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 durations.

  • Compression Mode is enabled compression_mode = True.


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"


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

#define writer
writer = WriteGear(output_filename = 'Output.mp4', logging = True, **output_params) #Define writer 

# infinite loop
while True:
  
  frame = stream.read()
  # read frames

  # check if frame is None
  if frame is None:
    #if True break the infinite loop
    break
  
  # do something with frame here

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

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

cv2.destroyAllWindows()
# close output window

# safely close video stream
stream.stop()

#close writer
writer.close()


#sleep 1sec 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.

 

Clone this wiki locally