美文网首页
ffmpeg # Linux # 利用2>&1将输

ffmpeg # Linux # 利用2>&1将输

作者: FlyingPenguin | 来源:发表于2018-08-01 17:05 被阅读268次

linux下,有时候可能希望运行ffmpeg命令的结果保存在文件。
最容易想到的是以下的命令:

ffmpeg -i test.flv -vcodec copy -acodec copy output.mkv > output.txt

运行后发现test.txt空空如也, 结果都打在屏幕上了。

[root@10.10.10.10 gzl]# ffmpeg -i test.flv -vcodec copy -acodec copy output.mkv > output.txt
ffmpeg version 2.0-tag-n2.0 Copyright (c) 2000-2013 the FFmpeg developers
  built on Feb 19 2014 12:01:05 with gcc 4.1.2 (GCC) 20080704 (Red Hat 4.1.2-46)
  configuration: --enable-libfdk-aac --enable-libfaac --enable-libx264 --enable-gpl --enable-nonfree --extra-version=tag-n2.0
  libavutil      52. 38.100 / 52. 38.100
  libavcodec     55. 18.102 / 55. 18.102
  libavformat    55. 12.100 / 55. 12.100
  libavdevice    55.  3.100 / 55.  3.100
  libavfilter     3. 79.101 /  3. 79.101
  libswscale      2.  3.100 /  2.  3.100
  libswresample   0. 17.102 /  0. 17.102
  libpostproc    52.  3.100 / 52.  3.100
Input #0, flv, from 'test.flv':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2013-05-17 04:15:00
    encoder         : Lavf52.108.0
  Duration: 00:00:30.08, start: 0.080000, bitrate: 919 kb/s
    Stream #0:0: Video: h264 (High), yuv420p, 640x360, 600 kb/s, 25 tbr, 1k tbn, 50 tbc
    Stream #0:1: Audio: adpcm_swf, 44100 Hz, stereo, s16, 352 kb/s
[NULL @ 0x29e09e0] requested bits_per_coded_sample (16) and actually stored (4) differ
Output #0, matroska, to 'output.mkv':
  Metadata:
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    encoder         : Lavf55.12.100
    Stream #0:0: Video: h264 (H264 / 0x34363248), yuv420p, 640x360, q=2-31, 600 kb/s, 1k tbn, 1k tbc
    Stream #0:1: Audio: adpcm_swf (FS[0][0] / 0x5346), 44100 Hz, stereo, 352 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
  Stream #0:1 -> #0:1 (copy)
Press [q] to stop, [?] for help
frame=  750 fps=0.0 q=-1.0 Lsize=    3358kB time=00:00:29.95 bitrate= 918.3kbits/s    
video:2052kB audio:1295kB subtitle:0 global headers:0kB muxing overhead 0.314513%

shell输出分好多种,输出到屏幕,输出到文件;正确输出,错误输出等。在shell中,0表示键盘输出,1表示屏幕输出,2表示错误输出。 cmd > file,只是把屏幕输出重定向到文件中。
我们平时调用ffmpeg相关命令,打印信息都被终端认为是错误输出,但是一般情况下这种信息也会输出到屏幕上,所以我们可以在屏幕上看到它
但是,当我们想重定向到文件中,却被系统认为是错误输出而忽略。

怎么办呢? 用以下命令:

ffmpeg -i test.flv -vcodec copy -acodec copy -y output.mkv > output.txt 2>&1

运行之后,OK了。

File descriptor(linux)

In Unix and related computer operating systems, a file descriptor (FD, less frequently fildes) is an abstract indicator (handle) used to access a file or other input/output resource, such as a pipeor network socket.
File descriptors form part of the POSIX application programming interface.
A file descriptor is a non-negative integer, generally represented in the C programming language as the type int (negative values being reserved to indicate "no value" or an error condition).

some file descriptor.png

2>&1

File descriptor 1 is the standard output (stdout).
File descriptor 2 is the standard error (stderr).

Here is one way to remember this construct (although it is not entirely accurate):
at first, 2>1 may look like a good way to redirect stderr to stdout.
However, it will actually be interpreted as "redirect stderr to a file named 1".
& indicates that what follows is a file descriptor and not a filename.
So the construct becomes: 2>&1.

&标明其后面跟的是一个文件描述符,而不是一个文件名。

So 2>&1 says to send standard error to where ever standard output is being redirected as well.

You use &1 to reference the value of the file descriptor 1 (stdout).
So when you use 2>&1 you are basically saying “Redirect the stderr to the same place we are redirecting the stdout”.
And that’s why we can do something like this to redirect both stdout and stderr to the same place:

将stderr重定向到“stdout重定向的地方”。

另一种方式:

ffmpeg -version &> out.txt

References:

https://blog.csdn.net/u010029439/article/details/78673739
https://superuser.com/questions/71428/what-does-21-do-in-command-line
https://stackoverflow.com/questions/818255/in-the-shell-what-does-21-mean
https://www.brianstorti.com/understanding-shell-script-idiom-redirect/
https://en.wikipedia.org/wiki/File_descriptor

相关文章

网友评论

      本文标题:ffmpeg # Linux # 利用2>&1将输

      本文链接:https://www.haomeiwen.com/subject/moyrvftx.html