如何将 bash 数组正确扩展为参数
How to expand bash array into arguments correctly
我正在 Bash 上编写脚本。
objective 是通过命令将 36 个参数传递给 ffmpeg。
这里的步骤:
我验证了命令。这个工作正常:
ffmpeg -i "$f" -y -acodec aac -ab 128k -vcodec copy -f mp4 -movflags use_metadata_tags -metadata MtsExifToolVersionNumber="12.30" -metadata MtsFileName="00017.MTS" -metadata MtsDirectory="." -metadata MtsFileSize="59 MiB" -metadata MtsFileModificationDate/Time="2020" -metadata MtsFileAccessDate/Time="2021" -metadata MtsFileInodeChangeDate/Time="2022" -metadata MtsFilePermissions="-rwxr-xr-x" -metadata MtsFileType="M2TS" -metadata MtsFileTypeExtension="mts" -metadata MtsMIMEType="video/m2ts" -metadata MtsVideoStreamType="H.264 (AVC) Video" -metadata MtsAudioBitrate="256 kbps" -metadata MtsSurroundMode="Not indicated" -metadata MtsAudioChannels="2" -metadata MtsAudioStreamType="PGS Audio" -metadata MtsImageWidth="1920" -metadata MtsImageHeight="1080" -metadata MtsDate/TimeOriginal="2020" -metadata MtsApertureSetting="Auto" -metadata MtsGain="0 dB" -metadata MtsExposureProgram="Program AE" -metadata MtsWhiteBalance="Auto" -metadata MtsFocus="Auto (0.155)" -metadata MtsImageStabilization="On (0x3f)" -metadata MtsExposureTime="1/60" -metadata MtsFNumber="3.4" -metadata MtsMake="Sony" -metadata MtsCameraModelName="HDR-CX405" -metadata MtsWarning="[minor] The ExtractEmbedded option may find more tags in the video data" -metadata MtsAudioSampleRate="48000" -metadata MtsDuration="18.71 s" -metadata MtsAperture="3.4" -metadata MtsImageSize="1920x1080" -metadata MtsMegapixels="2.1" -metadata MtsShutterSpeed="1/60" -metadata Offset="$Offset" -metadata creation_time="$DATE" "./$output/convert_exif_ok/$MP4_NAME"
现在.. 我创建了一个 bash 数组,并尝试直接在命令上展开它:
ffmpeg -i "$f" -y -acodec aac -ab 128k -vcodec copy -f mp4 -movflags use_metadata_tags $(echo "${exif_2[@]}" ) -metadata Offset="$Offset" -metadata creation_time="$DATE" "./$output/convert_exif_ok/$MP4_NAME"
而且..这行不通..
似乎是一个参数这么长,而不是 36 个不同的参数..
这是输出:
ffmpeg version 5.0.1 Copyright (c) 2000-2022 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/Cellar/ffmpeg/5.0.1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
libavutil 57. 17.100 / 57. 17.100
libavcodec 59. 18.100 / 59. 18.100
libavformat 59. 16.100 / 59. 16.100
libavdevice 59. 4.100 / 59. 4.100
libavfilter 8. 24.100 / 8. 24.100
libswscale 6. 4.100 / 6. 4.100
libswresample 4. 3.100 / 4. 3.100
libpostproc 56. 3.100 / 56. 3.100
Input #0, mpegts, from '00017.MTS':
Duration: 00:00:18.72, start: 1.020000, bitrate: 26466 kb/s
Program 1
Stream #0:0[0x1011]: Video: h264 (High) (HDPR / 0x52504448), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 50 fps, 50 tbr, 90k tbn
Stream #0:1[0x1100]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, stereo, fltp, 256 kb/s
Stream #0:2[0x1200]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090), 1920x1080
-metadata MtsExifToolVersionNumber="12.30" -metadata MtsFileName="00017.MTS" -metadata MtsDirectory="." -metadata MtsFileSize="59 MiB" -metadata MtsFileModificationDate/Time="2020" -metadata MtsFileAccessDate/Time="2021" -metadata MtsFileInodeChangeDate/Time="2022" -metadata MtsFilePermissions="-rwxr-xr-x" -metadata MtsFileType="M2TS" -metadata MtsFileTypeExtension="mts" -metadata MtsMIMEType="video/m2ts" -metadata MtsVideoStreamType="H.264 (AVC) Video" -metadata MtsAudioBitrate="256 kbps" -metadata MtsSurroundMode="Not indicated" -metadata MtsAudioChannels="2" -metadata MtsAudioStreamType="PGS Audio" -metadata MtsImageWidth="1920" -metadata MtsImageHeight="1080" -metadata MtsDate/TimeOriginal="2020" -metadata MtsApertureSetting="Auto" -metadata MtsGain="0 dB" -metadata MtsExposureProgram="Program AE" -metadata MtsWhiteBalance="Auto" -metadata MtsFocus="Auto (0.155)" -metadata MtsImageStabilization="On (0x3f)" -metadata MtsExposureTime="1/60" -metadata MtsFNumber="3.4" -metadata MtsMake="Sony" -metadata MtsCameraModelName="HDR-CX405" -metadata MtsWarning="[minor] The ExtractEmbedded option may find more tags in the video data" -metadata MtsAudioSampleRate="48000" -metadata MtsDuration="18.71 s" -metadata MtsAperture="3.4" -metadata MtsImageSize="1920x1080" -metadata MtsMegapixels="2.1" -metadata MtsShutterSpeed="1/60": File name too long
嗯..我确定我在将数组内容传递给参数的过程中做错了什么..
有什么帮助吗?
谢谢
如果你有
$ typeset -p exif_2
declare -a exif_2='([0]=" -metadata MtsExifToolVersionNumber=\"12.30\"" ...
您创建 数组时引号太多。 -metadata
和 MtsExifToolVersionNumber="12.30"
需要是数组中的独立元素。
试试这个(注意数组定义中允许任意空格,包括换行符)
exif_2=(
-movflags use_metadata_tags
-metadata MtsExifToolVersionNumber="12.30"
-metadata MtsFileName="00017.MTS"
-metadata MtsDirectory="."
-metadata MtsFileSize="59 MiB"
-metadata MtsFileModificationDate/Time="2020"
-metadata MtsFileAccessDate/Time="2021"
-metadata MtsFileInodeChangeDate/Time="2022"
-metadata MtsFilePermissions="-rwxr-xr-x"
-metadata MtsFileType="M2TS"
-metadata MtsFileTypeExtension="mts"
-metadata MtsMIMEType="video/m2ts"
-metadata MtsVideoStreamType="H.264 (AVC) Video"
-metadata MtsAudioBitrate="256 kbps"
-metadata MtsSurroundMode="Not indicated"
-metadata MtsAudioChannels="2"
-metadata MtsAudioStreamType="PGS Audio"
-metadata MtsImageWidth="1920"
-metadata MtsImageHeight="1080"
-metadata MtsDate/TimeOriginal="2020"
-metadata MtsApertureSetting="Auto"
-metadata MtsGain="0 dB"
-metadata MtsExposureProgram="Program AE"
-metadata MtsWhiteBalance="Auto"
-metadata MtsFocus="Auto (0.155)"
-metadata MtsImageStabilization="On (0x3f)"
-metadata MtsExposureTime="1/60"
-metadata MtsFNumber="3.4"
-metadata MtsMake="Sony"
-metadata MtsCameraModelName="HDR-CX405"
-metadata MtsWarning="[minor] The ExtractEmbedded option may find more tags in the video data"
-metadata MtsAudioSampleRate="48000"
-metadata MtsDuration="18.71 s"
-metadata MtsAperture="3.4"
-metadata MtsImageSize="1920x1080"
-metadata MtsMegapixels="2.1"
-metadata MtsShutterSpeed="1/60"
)
ffmpeg_opts=(
-i "$f"
-y
-acodec aac
-ab 128k
-vcodec copy
-f mp4
"${exif_2[@]}"
-metadata Offset="$Offset"
-metadata creation_time="$DATE"
)
ffmpeg "${ffmpeg_opts[@]}" "./$output/convert_exif_ok/$MP4_NAME"
我正在 Bash 上编写脚本。
objective 是通过命令将 36 个参数传递给 ffmpeg。
这里的步骤:
我验证了命令。这个工作正常:
ffmpeg -i "$f" -y -acodec aac -ab 128k -vcodec copy -f mp4 -movflags use_metadata_tags -metadata MtsExifToolVersionNumber="12.30" -metadata MtsFileName="00017.MTS" -metadata MtsDirectory="." -metadata MtsFileSize="59 MiB" -metadata MtsFileModificationDate/Time="2020" -metadata MtsFileAccessDate/Time="2021" -metadata MtsFileInodeChangeDate/Time="2022" -metadata MtsFilePermissions="-rwxr-xr-x" -metadata MtsFileType="M2TS" -metadata MtsFileTypeExtension="mts" -metadata MtsMIMEType="video/m2ts" -metadata MtsVideoStreamType="H.264 (AVC) Video" -metadata MtsAudioBitrate="256 kbps" -metadata MtsSurroundMode="Not indicated" -metadata MtsAudioChannels="2" -metadata MtsAudioStreamType="PGS Audio" -metadata MtsImageWidth="1920" -metadata MtsImageHeight="1080" -metadata MtsDate/TimeOriginal="2020" -metadata MtsApertureSetting="Auto" -metadata MtsGain="0 dB" -metadata MtsExposureProgram="Program AE" -metadata MtsWhiteBalance="Auto" -metadata MtsFocus="Auto (0.155)" -metadata MtsImageStabilization="On (0x3f)" -metadata MtsExposureTime="1/60" -metadata MtsFNumber="3.4" -metadata MtsMake="Sony" -metadata MtsCameraModelName="HDR-CX405" -metadata MtsWarning="[minor] The ExtractEmbedded option may find more tags in the video data" -metadata MtsAudioSampleRate="48000" -metadata MtsDuration="18.71 s" -metadata MtsAperture="3.4" -metadata MtsImageSize="1920x1080" -metadata MtsMegapixels="2.1" -metadata MtsShutterSpeed="1/60" -metadata Offset="$Offset" -metadata creation_time="$DATE" "./$output/convert_exif_ok/$MP4_NAME"
现在.. 我创建了一个 bash 数组,并尝试直接在命令上展开它:
ffmpeg -i "$f" -y -acodec aac -ab 128k -vcodec copy -f mp4 -movflags use_metadata_tags $(echo "${exif_2[@]}" ) -metadata Offset="$Offset" -metadata creation_time="$DATE" "./$output/convert_exif_ok/$MP4_NAME"
而且..这行不通..
似乎是一个参数这么长,而不是 36 个不同的参数..
这是输出:
ffmpeg version 5.0.1 Copyright (c) 2000-2022 the FFmpeg developers
built with Apple clang version 12.0.0 (clang-1200.0.32.29)
configuration: --prefix=/usr/local/Cellar/ffmpeg/5.0.1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libdav1d --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox
libavutil 57. 17.100 / 57. 17.100
libavcodec 59. 18.100 / 59. 18.100
libavformat 59. 16.100 / 59. 16.100
libavdevice 59. 4.100 / 59. 4.100
libavfilter 8. 24.100 / 8. 24.100
libswscale 6. 4.100 / 6. 4.100
libswresample 4. 3.100 / 4. 3.100
libpostproc 56. 3.100 / 56. 3.100
Input #0, mpegts, from '00017.MTS':
Duration: 00:00:18.72, start: 1.020000, bitrate: 26466 kb/s
Program 1
Stream #0:0[0x1011]: Video: h264 (High) (HDPR / 0x52504448), yuv420p(progressive), 1920x1080 [SAR 1:1 DAR 16:9], 50 fps, 50 tbr, 90k tbn
Stream #0:1[0x1100]: Audio: ac3 (AC-3 / 0x332D4341), 48000 Hz, stereo, fltp, 256 kb/s
Stream #0:2[0x1200]: Subtitle: hdmv_pgs_subtitle ([144][0][0][0] / 0x0090), 1920x1080
-metadata MtsExifToolVersionNumber="12.30" -metadata MtsFileName="00017.MTS" -metadata MtsDirectory="." -metadata MtsFileSize="59 MiB" -metadata MtsFileModificationDate/Time="2020" -metadata MtsFileAccessDate/Time="2021" -metadata MtsFileInodeChangeDate/Time="2022" -metadata MtsFilePermissions="-rwxr-xr-x" -metadata MtsFileType="M2TS" -metadata MtsFileTypeExtension="mts" -metadata MtsMIMEType="video/m2ts" -metadata MtsVideoStreamType="H.264 (AVC) Video" -metadata MtsAudioBitrate="256 kbps" -metadata MtsSurroundMode="Not indicated" -metadata MtsAudioChannels="2" -metadata MtsAudioStreamType="PGS Audio" -metadata MtsImageWidth="1920" -metadata MtsImageHeight="1080" -metadata MtsDate/TimeOriginal="2020" -metadata MtsApertureSetting="Auto" -metadata MtsGain="0 dB" -metadata MtsExposureProgram="Program AE" -metadata MtsWhiteBalance="Auto" -metadata MtsFocus="Auto (0.155)" -metadata MtsImageStabilization="On (0x3f)" -metadata MtsExposureTime="1/60" -metadata MtsFNumber="3.4" -metadata MtsMake="Sony" -metadata MtsCameraModelName="HDR-CX405" -metadata MtsWarning="[minor] The ExtractEmbedded option may find more tags in the video data" -metadata MtsAudioSampleRate="48000" -metadata MtsDuration="18.71 s" -metadata MtsAperture="3.4" -metadata MtsImageSize="1920x1080" -metadata MtsMegapixels="2.1" -metadata MtsShutterSpeed="1/60": File name too long
嗯..我确定我在将数组内容传递给参数的过程中做错了什么..
有什么帮助吗?
谢谢
如果你有
$ typeset -p exif_2
declare -a exif_2='([0]=" -metadata MtsExifToolVersionNumber=\"12.30\"" ...
您创建 数组时引号太多。 -metadata
和 MtsExifToolVersionNumber="12.30"
需要是数组中的独立元素。
试试这个(注意数组定义中允许任意空格,包括换行符)
exif_2=(
-movflags use_metadata_tags
-metadata MtsExifToolVersionNumber="12.30"
-metadata MtsFileName="00017.MTS"
-metadata MtsDirectory="."
-metadata MtsFileSize="59 MiB"
-metadata MtsFileModificationDate/Time="2020"
-metadata MtsFileAccessDate/Time="2021"
-metadata MtsFileInodeChangeDate/Time="2022"
-metadata MtsFilePermissions="-rwxr-xr-x"
-metadata MtsFileType="M2TS"
-metadata MtsFileTypeExtension="mts"
-metadata MtsMIMEType="video/m2ts"
-metadata MtsVideoStreamType="H.264 (AVC) Video"
-metadata MtsAudioBitrate="256 kbps"
-metadata MtsSurroundMode="Not indicated"
-metadata MtsAudioChannels="2"
-metadata MtsAudioStreamType="PGS Audio"
-metadata MtsImageWidth="1920"
-metadata MtsImageHeight="1080"
-metadata MtsDate/TimeOriginal="2020"
-metadata MtsApertureSetting="Auto"
-metadata MtsGain="0 dB"
-metadata MtsExposureProgram="Program AE"
-metadata MtsWhiteBalance="Auto"
-metadata MtsFocus="Auto (0.155)"
-metadata MtsImageStabilization="On (0x3f)"
-metadata MtsExposureTime="1/60"
-metadata MtsFNumber="3.4"
-metadata MtsMake="Sony"
-metadata MtsCameraModelName="HDR-CX405"
-metadata MtsWarning="[minor] The ExtractEmbedded option may find more tags in the video data"
-metadata MtsAudioSampleRate="48000"
-metadata MtsDuration="18.71 s"
-metadata MtsAperture="3.4"
-metadata MtsImageSize="1920x1080"
-metadata MtsMegapixels="2.1"
-metadata MtsShutterSpeed="1/60"
)
ffmpeg_opts=(
-i "$f"
-y
-acodec aac
-ab 128k
-vcodec copy
-f mp4
"${exif_2[@]}"
-metadata Offset="$Offset"
-metadata creation_time="$DATE"
)
ffmpeg "${ffmpeg_opts[@]}" "./$output/convert_exif_ok/$MP4_NAME"