【问题标题】:FFmpeg create output directory hierarchyFFmpeg 创建输出目录层次结构
【发布时间】:2017-09-20 03:24:55
【问题描述】:

最近买了一个输出 RTSP 流的 ip-cam。我正在使用 FFmpeg 的分段选项来创建 60 分钟长的录音。

我希望FFmpeg将文件写入基于Year/Month/Date的目录,并写入文件Hour-Minute.mp4 例如: /raid1/homes/share/public/recordings/queue/bedroom/2017/04/23/13-05.mp4 录制于 2017 年 4 月 23 日 13:05 开始。

不幸的是,FFmpeg 似乎没有创建目录层次结构。由于找不到目录,FFmpeg 退出。

Input #0, rtsp, from 'rtsp://192.168.1.240/unicast':
  Metadata:
    title           : LIVE555 Streaming Media v2014.07.04
    comment         : LIVE555 Streaming Media v2014.07.04
  Duration: N/A, start: 0.000750, bitrate: N/A
    Stream #0:0: Video: h264 (High), yuv420p, 1920x1080, 90k tbr, 90k tbn, 180k tbc
    Stream #0:1: Audio: pcm_alaw, 8000 Hz, 1 channels, s16, 64 kb/s
[segment @ 0x2557300] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
[segment @ 0x2557300] Failed to open segment '/raid1/homes/share/public/recordings/queue/bedroom/2017/04/23/14-19.mp4'
Output #0, segment, to '/raid1/homes/share/public/recordings/queue/bedroom/%Y/%m/%d/%H-%M.mp4':
  Metadata:
    title           : LIVE555 Streaming Media v2014.07.04
    comment         : LIVE555 Streaming Media v2014.07.04
    encoder         : Lavf57.41.100
    Stream #0:0: Video: h264, yuv420p, 1920x1080, q=2-31, 90k tbr, 90k tbn, 90k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Could not write header for output file #0 (incorrect codec parameters ?): No such file or directory

record.sh如下:

#!/bin/sh
ffmpeg -stimeout 600\
 -rtsp_transport udp \
 -i rtsp://192.168.1.240/unicast \
 -c copy \
 -map 0:0 \
 -f segment \
 -segment_time 3600 \
 -segment_wrap 100 \
 -segment_format mov \
 -strftime 1 \
 -reset_timestamps 1 \
 "/raid1/homes/share/public/recordings/queue/bedroom/%Y/%m/%d/%H-%M.mp4"

我试过不使用目录层次结构:"/raid1/homes/share/public/recordings/queue/bedroom/%Y-%m-%d_%H-%M.mp4"。这工作正常。

$ ffmpeg -version
ffmpeg version N-80901-gfebc862 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.8 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
configuration: --extra-libs=-ldl --prefix=/opt/ffmpeg --mandir=/usr/share/man --enable-avresample --disable-debug --enable-nonfree --enable-gpl --enable-version3 --enable-libopencore-amrnb --enable-libopencore-amrwb --disable-decoder=amrnb --disable-decoder=amrwb --enable-libpulse --enable-libfreetype --enable-gnutls --enable-libx264 --enable-libx265 --enable-libfdk-aac --enable-libvorbis --enable-libmp3lame --enable-libopus --enable-libvpx --enable-libspeex --enable-libass --enable-avisynth --enable-libsoxr --enable-libxvid --enable-libvidstab
libavutil      55. 28.100 / 55. 28.100
libavcodec     57. 48.101 / 57. 48.101
libavformat    57. 41.100 / 57. 41.100
libavdevice    57.  0.102 / 57.  0.102
libavfilter     6. 47.100 /  6. 47.100
libavresample   3.  0.  0 /  3.  0.  0
libswscale      4.  1.100 /  4.  1.100
libswresample   2.  1.100 /  2.  1.100
libpostproc    54.  0.100 / 54.  0.100

FFmpeg 可以在旅途中创建输出目录吗?

【问题讨论】:

  • 我建议将问题编辑为最后一句

标签: ffmpeg


【解决方案1】:

不,FFmpeg 做不到 ?

不过,我们可以在您的 record.sh 脚本的顶部添加几行来为您创建目录。

#!/bin/sh

BASEDIRECTORY = /raid1/homes/share/public/recordings/queue/bedroom

YEAR=$(date +"%Y");
MONTH=$(date +"%m");
DAY=$(date +"%d");

# Create a directory tree for the day 
# using the current year, month, and day
mkdir -p $BASEDIRECTORY/$YEAR/$MONTH/$DAY

# Start FFmpeg hourly recordings
ffmpeg -stimeout 600\
 -rtsp_transport udp \
 -i rtsp://192.168.1.240/unicast \
 -c copy \
 -map 0:0 \
 -f segment \
 -segment_time 3600 \
 -segment_wrap 100 \
 -segment_format mov \
 -strftime 1 \
 -reset_timestamps 1 \
 "$BASEDIRECTORY/$YEAR/$MONTH/$DAY/%H-%M.mp4"

现在我们只需要record.sh 在一夜之间重新启动,为$DAY 创建一个新文件夹。这听起来像是 cron 的完美工作:How to write a cron that will run a script every day at midnight?

注意:此脚本现在需要 root 权限,因此如果您收到以下错误且 cron 不起作用,请确保您输入的是 @ 987654330@ 以 root 用户身份测试和安装您的 crontab,如下所示:sudo crotab -u root -e,正如@dashesyhis comment 中指出的那样。

这是因为带有-p--parent 选项的mkdir 需要root 权限,如果该目录不存在,FFmpeg 将抛出此错误:

Failed to open segment '/raid1/homes/share/public/recordings/queue/bedroom/2019/01/31/10-37.mp4'
Could not write header for output file #0 (incorrect codec parameters ?): No such file or directory

【讨论】:

  • 更新:对于 HLS,使用 -strftime 1-strftime_mkdir 1-hls_segment_filename '%Y/%m/%d/%H-%M.ts'。 FFmpeg 将创建目录结构。请参阅此处的 FFmpeg 文档:ffmpeg.org/ffmpeg-formats.html#Options-5
  • HLS?那是什么?行不行?
  • 这些实际上都不需要root,这些错误只是因为脚本以root身份运行(在root用户的crontab中或使用sudo运行),所以它在root用户下运行时创建了目录(因此归根拥有)。只要将脚本添加到普通用户的 crontab 并且目录归该用户所有,您就可以以该用户身份运行此脚本(无需 root/sudo)。
猜你喜欢
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
相关资源
最近更新 更多