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.










share|improve this question

























    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.










    share|improve this question























      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 8:33









      Georgе Stoyanov

      7712




      7712






















          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.)






          share|improve this answer




















          • Thanks, upvoted
            – oguzismail
            Nov 9 at 10:39






          • 1




            Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
            – tripleee
            Nov 9 at 10:39










          • 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
            Nov 9 at 11:11










          • @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
            Nov 9 at 12:08

















          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)





          share|improve this answer


















          • 2




            This won't redirect stdout.
            – Gyan
            Nov 9 at 10:48










          • What does ffmpeg write to stdout?
            – oguzismail
            Nov 9 at 10:51






          • 1




            Some of the analytical filters can write to stdout like metadata or libvmaf.
            – Gyan
            Nov 9 at 11:01










          • @Gyan how can I redirect the output of the ffmpeg command directly to a file?
            – Georgе Stoyanov
            Nov 9 at 12:10










          • Within python, don't know.
            – Gyan
            Nov 9 at 12:45










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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















          Required, but never shown

























          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.)






          share|improve this answer




















          • Thanks, upvoted
            – oguzismail
            Nov 9 at 10:39






          • 1




            Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
            – tripleee
            Nov 9 at 10:39










          • 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
            Nov 9 at 11:11










          • @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
            Nov 9 at 12:08














          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.)






          share|improve this answer




















          • Thanks, upvoted
            – oguzismail
            Nov 9 at 10:39






          • 1




            Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
            – tripleee
            Nov 9 at 10:39










          • 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
            Nov 9 at 11:11










          • @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
            Nov 9 at 12:08












          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.)






          share|improve this answer












          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.)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 10:35









          tripleee

          87.4k12122177




          87.4k12122177











          • Thanks, upvoted
            – oguzismail
            Nov 9 at 10:39






          • 1




            Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
            – tripleee
            Nov 9 at 10:39










          • 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
            Nov 9 at 11:11










          • @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
            Nov 9 at 12:08
















          • Thanks, upvoted
            – oguzismail
            Nov 9 at 10:39






          • 1




            Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
            – tripleee
            Nov 9 at 10:39










          • 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
            Nov 9 at 11:11










          • @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
            Nov 9 at 12:08















          Thanks, upvoted
          – oguzismail
          Nov 9 at 10:39




          Thanks, upvoted
          – oguzismail
          Nov 9 at 10:39




          1




          1




          Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
          – tripleee
          Nov 9 at 10:39




          Thanks. Sadly, I'm out of votes for today, but I'll keep you in mind.
          – tripleee
          Nov 9 at 10:39












          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
          Nov 9 at 11:11




          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
          Nov 9 at 11:11












          @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
          Nov 9 at 12:08




          @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
          Nov 9 at 12:08












          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)





          share|improve this answer


















          • 2




            This won't redirect stdout.
            – Gyan
            Nov 9 at 10:48










          • What does ffmpeg write to stdout?
            – oguzismail
            Nov 9 at 10:51






          • 1




            Some of the analytical filters can write to stdout like metadata or libvmaf.
            – Gyan
            Nov 9 at 11:01










          • @Gyan how can I redirect the output of the ffmpeg command directly to a file?
            – Georgе Stoyanov
            Nov 9 at 12:10










          • Within python, don't know.
            – Gyan
            Nov 9 at 12:45














          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)





          share|improve this answer


















          • 2




            This won't redirect stdout.
            – Gyan
            Nov 9 at 10:48










          • What does ffmpeg write to stdout?
            – oguzismail
            Nov 9 at 10:51






          • 1




            Some of the analytical filters can write to stdout like metadata or libvmaf.
            – Gyan
            Nov 9 at 11:01










          • @Gyan how can I redirect the output of the ffmpeg command directly to a file?
            – Georgе Stoyanov
            Nov 9 at 12:10










          • Within python, don't know.
            – Gyan
            Nov 9 at 12:45












          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)





          share|improve this answer














          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)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 11:08

























          answered Nov 9 at 10:30









          oguzismail

          2,7622823




          2,7622823







          • 2




            This won't redirect stdout.
            – Gyan
            Nov 9 at 10:48










          • What does ffmpeg write to stdout?
            – oguzismail
            Nov 9 at 10:51






          • 1




            Some of the analytical filters can write to stdout like metadata or libvmaf.
            – Gyan
            Nov 9 at 11:01










          • @Gyan how can I redirect the output of the ffmpeg command directly to a file?
            – Georgе Stoyanov
            Nov 9 at 12:10










          • Within python, don't know.
            – Gyan
            Nov 9 at 12:45












          • 2




            This won't redirect stdout.
            – Gyan
            Nov 9 at 10:48










          • What does ffmpeg write to stdout?
            – oguzismail
            Nov 9 at 10:51






          • 1




            Some of the analytical filters can write to stdout like metadata or libvmaf.
            – Gyan
            Nov 9 at 11:01










          • @Gyan how can I redirect the output of the ffmpeg command directly to a file?
            – Georgе Stoyanov
            Nov 9 at 12:10










          • Within python, don't know.
            – Gyan
            Nov 9 at 12:45







          2




          2




          This won't redirect stdout.
          – Gyan
          Nov 9 at 10:48




          This won't redirect stdout.
          – Gyan
          Nov 9 at 10:48












          What does ffmpeg write to stdout?
          – oguzismail
          Nov 9 at 10:51




          What does ffmpeg write to stdout?
          – oguzismail
          Nov 9 at 10:51




          1




          1




          Some of the analytical filters can write to stdout like metadata or libvmaf.
          – Gyan
          Nov 9 at 11:01




          Some of the analytical filters can write to stdout like metadata or libvmaf.
          – Gyan
          Nov 9 at 11:01












          @Gyan how can I redirect the output of the ffmpeg command directly to a file?
          – Georgе Stoyanov
          Nov 9 at 12:10




          @Gyan how can I redirect the output of the ffmpeg command directly to a file?
          – Georgе Stoyanov
          Nov 9 at 12:10












          Within python, don't know.
          – Gyan
          Nov 9 at 12:45




          Within python, don't know.
          – Gyan
          Nov 9 at 12:45

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          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















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          Crossroads (UK TV series)

          ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế