-
-
Notifications
You must be signed in to change notification settings - Fork 257
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.
- 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.
-
⚠️ 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 toexecute_ffmpeg_cmd
function, Otherwise wrong params may result in ERRORS. -
This feature as of now only available with the
testing
branch only!
-
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)
💡 See its example below
Note::bulb: This usage example is just an example of what can be done with this powerful function. So just Tinker with various FFmpeg parameters/commands yourself and can see it working and if you're unable to run any terminal FFmpeg command then report.
git clone https://github.com/abhiTronix/vidgear.git
cd vidgear
git checkout testing
sudo pip3 install .
cd
- You already have a separate video(
'input-video.mp4'
) and audio(input-audio.aac
) files. - Both these Audio and Video files are of equal durations.
-
Compression Mode 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)
#finally prepare 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('input-video.mp4'
) and audio(input-audio.aac
) files combined.