FFmpeg创建输出目录层次结构

时间:2017-04-23 12:39:27

标签: ffmpeg

最近买了一台输出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可以随时创建输出目录吗?

1 个答案:

答案 0 :(得分:2)

不是,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不起作用,请确保您正在打字sudo ./record.sh在测试和安装的crontab作为根用户是这样的:sudo crotab -u root -e,为@dashesy在指出his comment

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

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