0

I would like to ask how to use variables in ffmpy (Python wrapper for FFmpeg).

To be exactly: I want to use variables to crop video. The FFmpeg command is:

ffmpeg in.mp4 -filter:v "crop=out_w:out_h:x:y" out.mp4

https://ffmpeg.org/ffmpeg-filters.html#crop

To use it in Python/ffmpy, I wrote these codes:

from ffmpy import FFmpeg
import ffmpy

# define the variables
out_w = 100
out_h = 120
x = 50
y = 80

inputFile = "F:\\in.avi"
outputFile = "F:\\out.avi"

ff = FFmpeg(inputs = {inputFile: None}, outputs= {outputFile: " -y -filter:v `crop=100:120:50:80'"}) 
#This line works fine.

#Now rewrite the above line using variables...
ff = FFmpeg(inputs = {inputFile: None}, outputs={outputFile: " -y -filter:v `crop=out_w:out_h:x:y'"} ) 
#...It line does not work. I guess it is wrong to use variables in the statement.
#...This is my question. How to write this line using variables? 

ff.cmd
ff.run()

Thanks for your help!

2 Answers 2

1

I can solve your problem:

Recently I am trying to run this command:

ff = ffmpy.FFmpeg(inputs={inputs: None}, outputs={output: '-ss 0:1:0 -t 0:2:0 -c copy'})

It's perfectly running. But from the user's point of view, the inputs should given by them.

So I modified the command a little bit.

ff = ffmpy.FFmpeg(inputs={input: None}, outputs={output: '-ss %d:%d:%d -t %d:%d:%d -c copy' % (start_hour,start_min,start_sec,end_hour,end_min,end_sec)})

I simply use %d to use variables (int) in my command and it runs perfectly! I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Late answer but this might help others.

f-strings solved this for me. This works in python 3.6 or above.

import ffmpy

# define the variables
out_w = 100
out_h = 100
x = 50
y = 80

#set file locations
inputFile = "F:\\in.avi"
outputFile = "F:\\out.avi"

ff = ffmpy.FFmpeg(
    inputs = {inputFile : None},

    outputs = {outputFile :  f" -y -filter:v crop={out_w}:{out_h}:{x}:{y}" }    
) 
print(ff.cmd) #optional
ff.cmd
ff.run()

The "f" in the output line tells python to use f-strings and replace with the variables you defined above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.