合并视频和音频后如何删除下载的视频文件?

时间:2019-05-09 07:18:44

标签: shell ffmpeg youtube-dl

我使用youtube-dl从youtube下载了1080p视频。视频文件(.f137.mp4)和音频文件(f140.m4a)已下载。 ffmpeg将视频文件和音频文件合并到mp4文件(.mp4)。我想保留.f140.m4a文件和.mp4文件,同时删除f137.mp4文件。我该怎么办?

我尝试了youtube-dl中的--exec选项,但失败了。

这些是我的命令:

root@OMV:~/youtube-dl# cat dl-test.sh
DL_DIR="/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007"
ALBUM="Test"
mkdir -p "$DL_DIR/$ALBUM"
youtube-dl --batch-file  /root/youtube-dl/url-test.list --download-archive /root/youtube-dl/archive-test.list                           \
-f  'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio'                                 \
--write-sub --embed-sub --all-subs                                                              \
--embed-thumbnail --add-metadata                                                                \
-o "$DL_DIR/$ALBUM/%(title)s.%(ext)s"                                                           \
--exec "rm "$(ls {} | cut -d . -f1)""

结果是:

root@OMV:~/youtube-dl# ./dl-test.sh
ls: cannot access '{}': No such file or directory
...
[ffmpeg] Merging formats into "/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test/UpTown Spot.mp4"
...
[atomicparsley] Adding thumbnail to "/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test/UpTown Spot.mp4"
[exec] Executing command: rm  '/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test/UpTown Spot.mp4'

.mp4文件已删除,.f137.mp4文件仍然存在。我得到相反的结果。

root@OMV:~/youtube-dl# ls '/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test'
UpTown Spot.f137.mp4  UpTown Spot.f140.m4a

更新1: 我改为

--exec "rm "$(ls {} -1 | sed -n '/\.f[0-9]*\.mp4$/p')""

它仍然删除.mp4文件。输出为:

root@OMV:~/youtube-dl# echo "" > "/root/youtube-dl/archive-test.list"
root@OMV:~/youtube-dl# ./dl-test.sh
ls: cannot access '{}': No such file or directory
...
[download] /srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test/UpTown Spot.mp4 has already been downloaded and merged
...
[exec] Executing command: rm  '/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test/UpTown Spot.mp4'

然后将其更改为:

--exec "rm "$(ls -1 | sed -n '/\.f[0-9]*\.mp4$/p')""

输出几乎相同:

root@OMV:~/youtube-dl# ./dl-test.sh
...
[exec] Executing command: rm  '/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test/UpTown Spot.mp4'

我在SHELL中测试了正常表达,它起作用了:

root@OMV:~/youtube-dl# cd /srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test
root@OMV:/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test# ls "$(ls  /srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test | sed -n '/\.f[0-9]*\.mp4$/p')"
UpTown Spot.f137.mp4
root@OMV:/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test# rm "$(ls  /srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test | sed -n '/\.f[0-9]*\.mp4$/p')"

但是在youtube-dl --exec选项中运行的类似命令失败了!

更新2: 似乎管道之后的cmd无法执行。

root@OMV:~/youtube-dl# ./dl-test.sh
+ DL_DIR=/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007
+ ALBUM=Test
+ mkdir -p /srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test
+ youtube-dl --batch-file /root/youtube-dl/url-test.list --download-archive /root/youtube-dl/archive-test.list -f 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio' --write-sub --embed-sub --all-subs --embed-thumbnail --add-metadata -o '/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test/%(title)s.%(ext)s' --exec 'ls | grep mp4'
...
[exec] Executing command: ls | grep mp4 '/srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test/UpTown Spot.mp4'
Binary file /srv/dev-disk-by-id-ata-QEMU_HARDDISK_QM00007/Test/UpTown Spot.mp4 matches
ls: write error: Broken pipe

2 个答案:

答案 0 :(得分:0)

尝试一下:

更改

$(ls {} | cut -d . -f1)

$(ls -1 {} | sed -n '/\.f[0-9]*\.mp4$/p')

解释

ls -1      # print every file in one line

sed -n     # suppress automatic output
'/\.       # pattern start with .
f          # followed by f
[0-9]*     # some numbers
\.mp4$     # and .mp4
/p'        # print line

答案 1 :(得分:0)

我通过添加一个钩子脚本解决了这个问题。 这是我的dl.sh:

root@OMV:~/youtube-dl# cat dl.sh
#!/bin/bash

DL_DIR="$1"
ALBUM="$2"
SAVE_DIR="$DL_DIR/$ALBUM"
WORK_DIR=$(dirname $(readlink -f "$0"))
URL_LIST="$WORK_DIR/url.list"
ARCHIVE_LIST="$WORK_DIR/archive.list"
HOOK="$WORK_DIR/hook.sh"

if      [ ! -n "$DL_DIR" ] ||[ ! -n "$ALBUM" ];
then
        echo "Usage: $0 <download_dir> <album_name>"
        exit 1
fi

mkdir -p "$SAVE_DIR"
youtube-dl --batch-file  $URL_LIST --download-archive $ARCHIVE_LIST            \
-f  'bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio'                \
--write-sub --embed-sub --all-subs                                             \
--embed-thumbnail --add-metadata                                               \
-o "$SAVE_DIR/%(title)s.%(ext)s"                                               \
--exec $HOOK

这是我的hook.sh:

root@OMV:~/youtube-dl# cat hook.sh
#!/bin/bash
file="$1"
file_full_name=$(readlink -f "$file" -n)
dir=$(dirname "$file_full_name")
file_rm=$(ls "$dir" | sed -n '/\.f[0-9]*\.mp4$/p')
#echo "file to be removed = $file_rm"
rm -i "$dir/$file_rm"

谢谢大家的提示!