Redirect stderr and stdout from ffmpeg to a file in Python with subprocess
up vote
0
down vote
favorite
I am trying to redirect both the stderr and stdout of a ffmpeg command to a file and to suppress them when executing the Python script. This is my code:
import subprocess, shlex
cmd = 'ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4'
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
ffmpeg_stdout = ffmpeg_cmd.communicate()
for i in range(len(ffmpeg_stdout) - 1):
log.write(str(ffmpeg_stdout[i]) + "n")
So in general I want to do something similar to ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4 &> ffmpeg_out.txt
. So currently in the ffmpeg_stdout I have only (b'', None)
and the stdout and stderr are both printed when executing the script.
python ffmpeg subprocess stdout stderr
add a comment |
up vote
0
down vote
favorite
I am trying to redirect both the stderr and stdout of a ffmpeg command to a file and to suppress them when executing the Python script. This is my code:
import subprocess, shlex
cmd = 'ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4'
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
ffmpeg_stdout = ffmpeg_cmd.communicate()
for i in range(len(ffmpeg_stdout) - 1):
log.write(str(ffmpeg_stdout[i]) + "n")
So in general I want to do something similar to ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4 &> ffmpeg_out.txt
. So currently in the ffmpeg_stdout I have only (b'', None)
and the stdout and stderr are both printed when executing the script.
python ffmpeg subprocess stdout stderr
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to redirect both the stderr and stdout of a ffmpeg command to a file and to suppress them when executing the Python script. This is my code:
import subprocess, shlex
cmd = 'ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4'
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
ffmpeg_stdout = ffmpeg_cmd.communicate()
for i in range(len(ffmpeg_stdout) - 1):
log.write(str(ffmpeg_stdout[i]) + "n")
So in general I want to do something similar to ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4 &> ffmpeg_out.txt
. So currently in the ffmpeg_stdout I have only (b'', None)
and the stdout and stderr are both printed when executing the script.
python ffmpeg subprocess stdout stderr
I am trying to redirect both the stderr and stdout of a ffmpeg command to a file and to suppress them when executing the Python script. This is my code:
import subprocess, shlex
cmd = 'ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4'
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE)
ffmpeg_stdout = ffmpeg_cmd.communicate()
for i in range(len(ffmpeg_stdout) - 1):
log.write(str(ffmpeg_stdout[i]) + "n")
So in general I want to do something similar to ffmpeg -hide_banner -i input.mp4 -c:v libx264 -b:v 2M -c:a copy enc_out.mp4 &> ffmpeg_out.txt
. So currently in the ffmpeg_stdout I have only (b'', None)
and the stdout and stderr are both printed when executing the script.
python ffmpeg subprocess stdout stderr
python ffmpeg subprocess stdout stderr
asked 21 hours ago
Georgе Stoyanov
6010
6010
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
oguzismail's answer is probably superior in this particular case, but just for the record, it's not hard at all.
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.run(shlex.split(cmd),
stdout=log, stderr=log)
Notice also the preference for subprocess.run()
over raw Popen
. (Probably add check=True
as well.)
Thanks, upvoted
– oguzismail
19 hours ago
1
Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
– tripleee
19 hours ago
More tangentially,shlex.split()
doesn't do anything you can't do yourself here. Just replace the spaces with', '
and make the command a list.
– tripleee
19 hours ago
@tripleee actually the command I want to execute is way longer, so this is saving me some time and also in the past I was constantly missing to put the,
so the script was crashing and I needed to look where I have missed to put one...
– Georgе Stoyanov
18 hours ago
add a comment |
up vote
1
down vote
ffmpeg
can redirect stderr to a file on its own, and since it doesn't print anything necessary to stdout in most cases, this would be a useful workaround
my_env = os.environ.copy()
my_env["FFREPORT"] = "file=ffmpeg_out.txt:level=32"
subprocess.Popen(shlex.split(cmd), env=my_env)
2
This won't redirect stdout.
– Gyan
19 hours ago
What does ffmpeg write to stdout?
– oguzismail
19 hours ago
1
Some of the analytical filters can write to stdout like metadata or libvmaf.
– Gyan
19 hours ago
@Gyan how can I redirect the output of theffmpeg
command directly to a file?
– Georgе Stoyanov
18 hours ago
Within python, don't know.
– Gyan
17 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
oguzismail's answer is probably superior in this particular case, but just for the record, it's not hard at all.
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.run(shlex.split(cmd),
stdout=log, stderr=log)
Notice also the preference for subprocess.run()
over raw Popen
. (Probably add check=True
as well.)
Thanks, upvoted
– oguzismail
19 hours ago
1
Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
– tripleee
19 hours ago
More tangentially,shlex.split()
doesn't do anything you can't do yourself here. Just replace the spaces with', '
and make the command a list.
– tripleee
19 hours ago
@tripleee actually the command I want to execute is way longer, so this is saving me some time and also in the past I was constantly missing to put the,
so the script was crashing and I needed to look where I have missed to put one...
– Georgе Stoyanov
18 hours ago
add a comment |
up vote
3
down vote
accepted
oguzismail's answer is probably superior in this particular case, but just for the record, it's not hard at all.
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.run(shlex.split(cmd),
stdout=log, stderr=log)
Notice also the preference for subprocess.run()
over raw Popen
. (Probably add check=True
as well.)
Thanks, upvoted
– oguzismail
19 hours ago
1
Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
– tripleee
19 hours ago
More tangentially,shlex.split()
doesn't do anything you can't do yourself here. Just replace the spaces with', '
and make the command a list.
– tripleee
19 hours ago
@tripleee actually the command I want to execute is way longer, so this is saving me some time and also in the past I was constantly missing to put the,
so the script was crashing and I needed to look where I have missed to put one...
– Georgе Stoyanov
18 hours ago
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
oguzismail's answer is probably superior in this particular case, but just for the record, it's not hard at all.
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.run(shlex.split(cmd),
stdout=log, stderr=log)
Notice also the preference for subprocess.run()
over raw Popen
. (Probably add check=True
as well.)
oguzismail's answer is probably superior in this particular case, but just for the record, it's not hard at all.
with open("ffmpeg_out.txt", 'w') as log:
ffmpeg_cmd = subprocess.run(shlex.split(cmd),
stdout=log, stderr=log)
Notice also the preference for subprocess.run()
over raw Popen
. (Probably add check=True
as well.)
answered 19 hours ago
tripleee
86.3k12120175
86.3k12120175
Thanks, upvoted
– oguzismail
19 hours ago
1
Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
– tripleee
19 hours ago
More tangentially,shlex.split()
doesn't do anything you can't do yourself here. Just replace the spaces with', '
and make the command a list.
– tripleee
19 hours ago
@tripleee actually the command I want to execute is way longer, so this is saving me some time and also in the past I was constantly missing to put the,
so the script was crashing and I needed to look where I have missed to put one...
– Georgе Stoyanov
18 hours ago
add a comment |
Thanks, upvoted
– oguzismail
19 hours ago
1
Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
– tripleee
19 hours ago
More tangentially,shlex.split()
doesn't do anything you can't do yourself here. Just replace the spaces with', '
and make the command a list.
– tripleee
19 hours ago
@tripleee actually the command I want to execute is way longer, so this is saving me some time and also in the past I was constantly missing to put the,
so the script was crashing and I needed to look where I have missed to put one...
– Georgе Stoyanov
18 hours ago
Thanks, upvoted
– oguzismail
19 hours ago
Thanks, upvoted
– oguzismail
19 hours ago
1
1
Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
– tripleee
19 hours ago
Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
– tripleee
19 hours ago
More tangentially,
shlex.split()
doesn't do anything you can't do yourself here. Just replace the spaces with ', '
and make the command a list.– tripleee
19 hours ago
More tangentially,
shlex.split()
doesn't do anything you can't do yourself here. Just replace the spaces with ', '
and make the command a list.– tripleee
19 hours ago
@tripleee actually the command I want to execute is way longer, so this is saving me some time and also in the past I was constantly missing to put the
,
so the script was crashing and I needed to look where I have missed to put one...– Georgе Stoyanov
18 hours ago
@tripleee actually the command I want to execute is way longer, so this is saving me some time and also in the past I was constantly missing to put the
,
so the script was crashing and I needed to look where I have missed to put one...– Georgе Stoyanov
18 hours ago
add a comment |
up vote
1
down vote
ffmpeg
can redirect stderr to a file on its own, and since it doesn't print anything necessary to stdout in most cases, this would be a useful workaround
my_env = os.environ.copy()
my_env["FFREPORT"] = "file=ffmpeg_out.txt:level=32"
subprocess.Popen(shlex.split(cmd), env=my_env)
2
This won't redirect stdout.
– Gyan
19 hours ago
What does ffmpeg write to stdout?
– oguzismail
19 hours ago
1
Some of the analytical filters can write to stdout like metadata or libvmaf.
– Gyan
19 hours ago
@Gyan how can I redirect the output of theffmpeg
command directly to a file?
– Georgе Stoyanov
18 hours ago
Within python, don't know.
– Gyan
17 hours ago
add a comment |
up vote
1
down vote
ffmpeg
can redirect stderr to a file on its own, and since it doesn't print anything necessary to stdout in most cases, this would be a useful workaround
my_env = os.environ.copy()
my_env["FFREPORT"] = "file=ffmpeg_out.txt:level=32"
subprocess.Popen(shlex.split(cmd), env=my_env)
2
This won't redirect stdout.
– Gyan
19 hours ago
What does ffmpeg write to stdout?
– oguzismail
19 hours ago
1
Some of the analytical filters can write to stdout like metadata or libvmaf.
– Gyan
19 hours ago
@Gyan how can I redirect the output of theffmpeg
command directly to a file?
– Georgе Stoyanov
18 hours ago
Within python, don't know.
– Gyan
17 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
ffmpeg
can redirect stderr to a file on its own, and since it doesn't print anything necessary to stdout in most cases, this would be a useful workaround
my_env = os.environ.copy()
my_env["FFREPORT"] = "file=ffmpeg_out.txt:level=32"
subprocess.Popen(shlex.split(cmd), env=my_env)
ffmpeg
can redirect stderr to a file on its own, and since it doesn't print anything necessary to stdout in most cases, this would be a useful workaround
my_env = os.environ.copy()
my_env["FFREPORT"] = "file=ffmpeg_out.txt:level=32"
subprocess.Popen(shlex.split(cmd), env=my_env)
edited 19 hours ago
answered 19 hours ago
oguzismail
1,739516
1,739516
2
This won't redirect stdout.
– Gyan
19 hours ago
What does ffmpeg write to stdout?
– oguzismail
19 hours ago
1
Some of the analytical filters can write to stdout like metadata or libvmaf.
– Gyan
19 hours ago
@Gyan how can I redirect the output of theffmpeg
command directly to a file?
– Georgе Stoyanov
18 hours ago
Within python, don't know.
– Gyan
17 hours ago
add a comment |
2
This won't redirect stdout.
– Gyan
19 hours ago
What does ffmpeg write to stdout?
– oguzismail
19 hours ago
1
Some of the analytical filters can write to stdout like metadata or libvmaf.
– Gyan
19 hours ago
@Gyan how can I redirect the output of theffmpeg
command directly to a file?
– Georgе Stoyanov
18 hours ago
Within python, don't know.
– Gyan
17 hours ago
2
2
This won't redirect stdout.
– Gyan
19 hours ago
This won't redirect stdout.
– Gyan
19 hours ago
What does ffmpeg write to stdout?
– oguzismail
19 hours ago
What does ffmpeg write to stdout?
– oguzismail
19 hours ago
1
1
Some of the analytical filters can write to stdout like metadata or libvmaf.
– Gyan
19 hours ago
Some of the analytical filters can write to stdout like metadata or libvmaf.
– Gyan
19 hours ago
@Gyan how can I redirect the output of the
ffmpeg
command directly to a file?– Georgе Stoyanov
18 hours ago
@Gyan how can I redirect the output of the
ffmpeg
command directly to a file?– Georgе Stoyanov
18 hours ago
Within python, don't know.
– Gyan
17 hours ago
Within python, don't know.
– Gyan
17 hours ago
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222231%2fredirect-stderr-and-stdout-from-ffmpeg-to-a-file-in-python-with-subprocess%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password