使用subprocess.Popen

时间:2015-10-13 08:35:26

标签: python-2.7 ffmpeg

在ffmpeg和livestreamer中我只实时获得几行,然后在完成这个过程后我会得到其余的一行......

我尝试了两个代码:

from subprocess import *
p = Popen('ffmpeg -i infile.mp4 outfile.mp4', stderr=STDOUT, stdout=PIPE)
for line in iter(p.stdout.readline(), b'')
   print line.rstrip()

from subprocess import *
p = Popen('ffmpeg -i infile.mp4 outfile.mp4', stderr=STDOUT, stdout=PIPE)
while p.poll is None:
   line = p.stdout.readline()
   print line.rstrip()

他们两个先打印:

ffmpeg version N-72805-g913685f Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 4.9.2 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-av
isynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enab
le-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --
enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-l
ibilbc --enable-libmodplug --enable-libmfx --enable-libmp3lame --enable-libopenc
ore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --ena
ble-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable
-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enabl
e-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable
-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --e
nable-lzma --enable-decklink --enable-zlib
  libavutil      54. 27.100 / 54. 27.100
  libavcodec     56. 41.100 / 56. 41.100
  libavformat    56. 36.100 / 56. 36.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 16.101 /  5. 16.101
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.100 /  1.  2.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'a.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.10.100
  Duration: 01:34:36.14, start: 0.021333, bitrate: 6205 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720,
 5938 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, flt
p, 256 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
[mp4 @ 05013ce0] Codec for stream 0 does not use global headers but container fo
rmat requires global headers
[mp4 @ 05013ce0] Codec for stream 1 does not use global headers but container fo
rmat requires global headers
Output #0, mp4, to 'b.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf56.36.100
    Stream #0:0(und): Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1280x720, q
=2-31, 5938 kb/s, 30 fps, 30 tbr, 15360 tbn, 15360 tbc (default)
    Metadata:
      handler_name    : VideoHandler
    Stream #0:1(und): Audio: aac ([64][0][0][0] / 0x0040), 48000 Hz, stereo, 256
 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help

然后当转换过程完成时,其余的日志会出现......

如何实时获取所有stdout?

编辑:

如果我使用文本文件写入,则实时获取所有内容。

from subprocess import *
out_file = open('stdout.txt','a')
Popen('ffmpeg -i infile.mp4 outfile.mp4', stderr=STDOUT, stdout=out_file)

但我希望它能实时打印出stdout ..

为什么它可以实时处理文件但不能直接打印?

1 个答案:

答案 0 :(得分:0)

请检查一下: https://stackoverflow.com/a/2358686/1314124

无法打印的原因是“ffmpeg从不打印一行,通过写入\ r \ n(carrige返回)更新状态,然后再次写入行。”