如何防止/保持 tail 将 \r 替换为 \n

如何防止/保持 tail 将 \r 替换为 \n

当我在后台播放带有 mpv 的视频时,我得到以下信息:

$ tail -f nohup.out 
Playing: https://www.facebook.com/TBN/videos/1580372468665943/
 (+) Video --vid=1 (*) (h264 1266x720 30.000fps)
 (+) Audio --aid=1 (*) 'DASH audio' (aac 1ch 48000Hz) (external)
File tags:
 Artist: TBN
 Date: 20180113
 Title: Joseph Prince joins Matt & Laurie TONIGHT on PRAISE... watch NOW!
AO: [pulse] 48000Hz mono 1ch float
VO: [gpu] 1266x720 yuv420p
AV: 00:00:00 / 00:56:50 (0%) A-V:  0.000
AV: 00:00:00 / 00:56:50 (0%) A-V:  0.000
AV: 00:00:00 / 00:56:50 (0%) A-V:  0.000
... # THERE ARE QUITE A LOT OF LINES
AV: 00:00:03 / 00:56:50 (0%) A-V:  0.000
AV: 00:00:03 / 00:56:50 (0%) A-V:  0.000
Saving state.


Exiting... (Quit)
^C

但我希望有这个输出(就像我播放没有 的文件时一样nohup):

$ mpv --no-resume-playback https://www.facebook.com/TBN/videos/1580372468665943/
Playing: https://www.facebook.com/TBN/videos/1580372468665943/
 (+) Video --vid=1 (*) (h264 1266x720 30.000fps)
 (+) Audio --aid=1 (*) 'DASH audio' (aac 1ch 48000Hz) (external)
File tags:
 Artist: TBN
 Date: 20180113
 Title: Joseph Prince joins Matt & Laurie TONIGHT on PRAISE... watch NOW!
AO: [pulse] 48000Hz mono 1ch float
VO: [gpu] 1266x720 yuv420p
AV: 00:00:03 / 00:56:50 (0%) A-V:  0.000
Saving state.


Exiting... (Quit)

EDIT1:我使用类似 tty /dev/pts/2,因此如果我输入stty inlcr它应该会影响当前的 tty。

我仍然得到同样混乱的输出tail -f nohup.out

编辑2:根据@mosvy的说法,tail这不是应该受到指责的人,而是mpv应该受到指责的人。

mpv我找到了一种对s进行 S&R 的方法stderr

$ mpv --no-resume-playback https://www.facebook.com/TBN/videos/1580372468665943/ 2>&1 | perl -p -e '$| = 1;s/\n/\r/g if $_ =~ /^AV:/;s/Saving state/\nSaving state/' | tee mpv_all.log
Playing: https://www.facebook.com/TBN/videos/1580372468665943/
 (+) Video --vid=1 (*) (h264 634x360 30.000fps)
 (+) Audio --aid=1 (*) (aac 1ch 48000Hz)
AO: [pulse] 48000Hz mono 1ch float
VO: [gpu] 634x360 yuv420p
AV: 00:00:08 / 00:56:50 (0%) A-V:  0.000 Cache: 1024s+86MB
Saving state.
[ffmpeg] https: Will reconnect at 63421312 in 0 second(s), error=End of file.


Exiting... (Quit)

编辑3:@mosvy 谢谢,它现在可以工作了:

$ nohup sh -c ' mpv --no-resume-playback https://www.facebook.com/TBN/videos/1580372468665943/ 2>&1 | perl -p -e '\''$| = 1;s/\n/\r/g if $_ =~ /^AV:/;s/Saving state/\nSaving state/'\'' ' &
$ tail -f nohup.out
Playing: https://www.facebook.com/TBN/videos/1580372468665943/
 (+) Video --vid=1 (*) (h264 634x360 30.000fps)
 (+) Audio --aid=1 (*) (aac 1ch 48000Hz)
AO: [pulse] 48000Hz mono 1ch float
VO: [gpu] 634x360 yuv420p
AV: 00:00:08 / 00:56:50 (0%) A-V:  0.000 Cache: 1767s+147MB
Saving state.
[ffmpeg] https: Will reconnect at 103693952 in 0 second(s), error=End of file.


Exiting... (Quit)
^C

编辑4:另一个解决方案感谢@jw013:

export perl_script='$| = 1;s/\n/\r/g if $_ =~ /^AV:/;s/Saving state/\nSaving state/'
mpv="command mpv"
args=("$@")
nohup sh -c "$mpv ${args[*]} 2>&1 | perl -p -e '$perl_script' | tee ~/mpv_all.log" &

我怎样才能做到这一点 ?解决了 !

答案1

tail不是替换\r\n.

您的mpv程序根据其 stderr 是否是终端来调整其状态输出。

tail关于不替换为\r的简单测试\n

$ nohup sh -c 'while printf "s=$((s=s+1))\r"; do sleep 1; done' &
[1] 3897
$ nohup: ignoring input and appending output to 'nohup.out'

$ tail -f nohup.out
s=8  # s=9, s=10 ... on the same line

正如它的父级一样mplayermpv当有人试图以不同于其作者想象的方式运行它时,它会非常暴躁;当 stderr 是常规文件或管道时,我无法找到任何选项使其以与终端上相同的方式打印其状态。您可以做的最简单的事情就是在单独的screen/窗口中运行它tmux,而不是使用nohup.

相关内容