ffmpeg # Linux # 利用2>&1將輸出結(jié)果到文件

linux下,有時(shí)候可能希望運(yùn)行ffmpeg命令的結(jié)果保存在文件。
最容易想到的是以下的命令:

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

運(yùn)行后發(fā)現(xiàn)test.txt空空如也, 結(jié)果都打在屏幕上了。

[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輸出分好多種,輸出到屏幕,輸出到文件;正確輸出,錯(cuò)誤輸出等。在shell中,0表示鍵盤輸出,1表示屏幕輸出,2表示錯(cuò)誤輸出。 cmd > file,只是把屏幕輸出重定向到文件中。
我們平時(shí)調(diào)用ffmpeg相關(guān)命令,打印信息都被終端認(rèn)為是錯(cuò)誤輸出,但是一般情況下這種信息也會(huì)輸出到屏幕上,所以我們可以在屏幕上看到它。
但是,當(dāng)我們想重定向到文件中,卻被系統(tǒng)認(rèn)為是錯(cuò)誤輸出而忽略。

怎么辦呢? 用以下命令:

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

運(yùn)行之后,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.

&標(biāo)明其后面跟的是一個(gè)文件描述符,而不是一個(gè)文件名。

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容