gpt4 book ai didi

linux - 用于安装/更新 ffmpeg 静态构建的 bash 脚本

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:56 24 4
gpt4 key购买 nike

我正在尝试制作我的第一个“真正的”bash 脚本来完成一些实际工作,而不是手动和通过 cron 作业执行。

脚本应该从 https://johnvansickle.com/ffmpeg 下载并安装 ffmpeg-static-build

这是我到目前为止所得到的:

#!/bin/bash
# Still to come, see if the script runs with root privileges else exit
#Download FFMPEG static daily build and it's md5
cd ~
wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz | tar -xf
#wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5
curl -L https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5 | tar -xf | tee -a https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz | cut -d\ -f1 | md5sum > md5sum.txt


#Chech the md5 is currect
#md5sum -c MD5SUMS
file1="md5sum.txt"
file2="ffmpeg-git-64bit-static.tar.xz.md5"

if ! cmp --silent "$file1" "$file2"; then
echo "md5sum doesn't match...\n exit" >&2
exit 1
fi


#tar -xf ffmpeg-*.tar.xz
cp ffmpeg-*-static/ff* /usr/bin/
cp ffmpeg-*-static/ff* /usr/local/bin/
cp ffmpeg-*-static/qt-faststart /usr/bin/
cp ffmpeg-*-static/qt-faststart /usr/local/bin/
# Consider change second location to use ln -s
# Remove downloads and do some clean up

rm -fr ffmpeg-*

#EOF

正如您在脚本中看到的,我想添加 md5sum 检查但它失败了(从 Compare md5 sums in bash script 获取 md5sum 检查部分)

如果我删除脚本的 md5sum 部分,但现在脚本在 | 处失败 tar -xf |部分代码

2017-02-28 00:10:24 (617 KB/s) - ‘ffmpeg-git-64bit-static.tar.xz’ saved [17564756/17564756]

tar: option requires an argument -- 'f'
Try 'tar --help' or 'tar --usage' for more information.
tee: 'https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz': No such file or directory
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 65 100 65 0 0 87 0 --:--:-- --:--:-- --:--:-- 87
(23) Failed writing body
md5sum doesn't match...
exit

因为这是我的第一次,我将不胜感激任何“我 4 岁”的建议以及原因


更新

已根据此线程中的建议对脚本进行了一些更改,但我的问题是在此状态下我没有任何输出:( 所以我认为是时候询问下一条线索了

#!/bin/bash

# version 0.3z

## Download FFMPEG static daily build and it's md5
##
## To make this script working you might need to change the below values
## based on whether you are using a 32bit or 64 bit OS
## to obtain the right links you have to have a look @ https://johnvansickle.com/ffmpeg/
## and then change those below
##
## If you are running on a shared server or dowsn't have root priviliges you might need to uncomment
## point 5.


# a "~" means running users home folder :) and should be different from destination dir
download_dir=~

# as this can change if the ffmpeg is to be stored on ex. shared server
dest_dir=/usr/bin/

# The version it equal the filename from above link
version=ffmpeg-git-64bit-static.tar.xz

## Do not change anything below here!!
source_url=https://johnvansickle.com/ffmpeg/builds/${version}
md5_url=https://johnvansickle.com/ffmpeg/builds/${version}.md5

# Still to come, see if the script runs with write privileges else exit

# 1. Can we enter download directory else exit
cd ${download_dir} ||

printf "%s\n" "You can't enter this folder.\nPlease chage download_dir in this script..." >&2
exit 1

# 2. Check the md5 is correct or have changed from last check
## instead of downloading ffmpeg-git-64bit-static.tar.xz every time,
## regardless if it is new or not, I recommend only downloading it
## if the md5 does not match. Would save John some bandwidth at least
## thx for the idea to @LordNeckbeard



## So somehow i'll guess some sed or grep only first part is nessesary :(
## This is getting more advance than expected for a first time script :/

if ! diff <(md5sum ${version}) <(curl -s ${md5_url})

then
printf "%s\n" "md5sum doesn't match...\n
Downloading new version" >&2
rm -f ${version} >&2
curl ${source_url} -o ${download_dir}/${version} >&2
#exit 2

elif
diff <(md5sum ${version}) <(curl -s ${md5_url})
printf "%s\n" "Nothing new to download" >&2
exit 3
fi

# 3. untar
tar -xf ffmpeg-git-*-static.tar.xz

# 4. Move builds to destination directories

mv ${download_dir}/ffmpeg-*-static/ff* ${dest_dir}/
mv ${download_dir}/ffmpeg-*-static/qt-faststart ${dest_dir}/

# 5. Make soft links to static builds
ln -sfn ${dest_dir}/qt-faststart /usr/local/bin/qt-faststart
ln -sfn ${dest_dir}/ffmpeg /usr/local/bin/ffmpeg
ln -sfn ${dest_dir}/ffmpeg-10bit /usr/local/bin/ffmpeg-10bit
ln -sfn ${dest_dir}/ffprobe /usr/local/bin/ffprobe
ln -sfn ${dest_dir}/ffserver /usr/local/bin/ffserver

# Remove unzipped folder to do some clean up

rm -fr ffmpeg-git-*-static/

#EOF

注意:做一些更深入的回答为什么不从源代码编译:1. 此预编译可用于所有 Linux 变体,无论发行版和版本如何2. 它可以在具有 ssh 访问权限的共享主机服务器上使用


更新 2

#!/bin/bash

# version 0.4z

## Download FFMPEG static daily build and it's md5
##
## To make this script working you might need to change the below values
## based on whether you are using a 32bit or 64 bit OS
## to obtain the right links you have to have a look @ https://johnvansickle.com/ffmpeg/
## and then change those below
##
## Finished and working script should be distributed under GPLv3: free as in freedom
##
## If you are running on a shared server or dowsn't have root priviliges you might need to uncomment
## point 5.


# a "~" means running users home folder :) and should be different from destination dir
download_dir=~

# as this can change if the ffmpeg is to be stored on ex. shared server
dest_dir=/usr/bin/

# The version it equal the filename from above link
version=ffmpeg-git-64bit-static.tar.xz

## Do not change anything below here!!
source_url=https://johnvansickle.com/ffmpeg/builds/${version}
md5="curl -s https://johnvansickle.com/ffmpeg/builds/${version}.md5"

# Still to come, see if the script runs with write privileges else exit

# 1. Can we enter download directory else exit
cd ${download_dir} ||
printf "%s\n" "You can't enter this folder.\nPlease chage download_dir in this script..." >&2
exit 1

# 2. Check the md5 is correct or have changed from last check
## instead of downloading ffmpeg-git-64bit-static.tar.xz every time,
## regardless if it is new or not, I recommend only downloading it
## if the md5 does not match. Would save John some bandwidth at least
## thx for the idea to @LordNeckbeard

## This is getting more advance than expected for a first time script :/

if diff <(md5sum ${version}) <(${md5})

then
printf "%s\n" "No new version availeble" >&2
exit 1

elif ! diff <(md5sum ${version}) <(${md5})
then
rm -f ${version}
curl ${source_url} > ${version}
exit 0

#only proceed if downloaded version match it's md5
if ! diff <(md5sum ${version}) <(${md5})
then
rm -f ${version}
printf "%s\n" "Downloaded version is damaged, try later\ndamaged version have been deleted" >&2
exit 1
fi

# 3. untar
tar -xf ffmpeg-git-*-static.tar.xz

# 4. Move builds to destination directories
mv ${download_dir}/ffmpeg-*-static/ff* ${dest_dir}/
mv ${download_dir}/ffmpeg-*-static/qt-faststart ${dest_dir}/

# 5. Make soft links to static builds
ln -sfn ${dest_dir}/qt-faststart /usr/local/bin/qt-faststart
ln -sfn ${dest_dir}/ffmpeg /usr/local/bin/ffmpeg
ln -sfn ${dest_dir}/ffmpeg-10bit /usr/local/bin/ffmpeg-10bit
ln -sfn ${dest_dir}/ffprobe /usr/local/bin/ffprobe
ln -sfn ${dest_dir}/ffserver /usr/local/bin/ffserver

# Remove unzipped folder to do some clean up
rm -fr ffmpeg-git-*-static/
printf "%s\n" "Going to install new version" >&2
exit 1
fi
#EOF

但是还是有一些问题:(

  1. 运行这个脚本返回:一个 blanc shell,但我期望其中一个 printf 语句
  2. 当我试图缩小问题范围并返回到基本并尝试仅使用“if”部分运行脚本时,它失败并显示 44:语法错误:“(”意外
  3. 运行直接输入 shell/终端本身的完全相同的“if”部分,一切都很开心!!

    if diff <(md5sum ffmpeg-git-64bit-static.tar.xz) <(curl -s "https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5"); then printf "%s\n" "No new version availeble" >&2; elif ! diff <(md5sum ffmpeg-git-64bit-static.tar.xz) <(curl -s "https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5"); then rm -f ffmpeg-git-64bit-static.tar.xz; curl https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz > ffmpeg-git-64bit-static.tar.xz; printf "%s\n" "Going to install new version" >&2; fi
  4. 我很困惑

更新 3@tomas 也引起了我的注意,问题 3 正在通过 sh update_ffmpeg.sh 运行脚本 ofcurse 是错误的,它应该只是通过键入脚本 ex 的完整路径从它的位置执行。 /home/username/update_ffmpeg.sh

所以分享当前的工作版本(但仍然不完整):

#!/bin/bash

# version 0.4z

## Download FFMPEG static daily build and it's md5
##
## To make this script working you might need to change the below values
## based on whether you are using a 32bit or 64 bit OS
## to obtain the right links you have to have a look @ https://johnvansickle.com/ffmpeg/
## and then change those below
##
## Finished and working script should be distributed under GPLv3: free as in freedom
##
## If you are running on a shared server or dowsn't have root priviliges you might need to uncomment
## point 5.


# a "~" means running users home folder :) and should be different from destination dir
download_dir=~

# as this can change if the ffmpeg is to be stored on ex. shared server
dest_dir=/usr/bin/

# The version it equal the filename from above link
version=ffmpeg-git-64bit-static.tar.xz

## Do not change anything below here!!
source_url=https://johnvansickle.com/ffmpeg/builds/${version}
md5_url=https://johnvansickle.com/ffmpeg/builds/${version}.md5

# Still to come, see if the script runs with write privileges else exit

# 1. Can we enter download directory else exit
cd ${download_dir} || {
printf "%s\n" "You can't enter this folder." "Please change download_dir in this script..." >&2
exit 1
}

# 2. Check the md5 is correct or have changed from last check
## instead of downloading ffmpeg-git-64bit-static.tar.xz every time,
## regardless if it is new or not, I recommend only downloading it
## if the md5 does not match. Would save John some bandwidth at least
## thx for the idea to @LordNeckbeard

## This is getting more advance than expected for a first time script :/

if ! diff <(md5sum ${version}) <(curl -s ${md5_url})
then
# Sum doesn't match is not really an error... I comment out the redirection.
printf "%s\n" "md5sum doesn't match..." "Downloading new version"
rm -f ${version}
curl ${source_url} -o ${download_dir}/${version}

# 3. untar
tar -xf ffmpeg-git-*-static.tar.xz

# 4. Move builds to destination directories
mv ${download_dir}/ffmpeg-*-static/ff* ${dest_dir}/
mv ${download_dir}/ffmpeg-*-static/qt-faststart ${dest_dir}/

# 5. Make soft links to static builds
ln -sfn ${dest_dir}/qt-faststart /usr/local/bin/qt-faststart >&2
ln -sfn ${dest_dir}/ffmpeg /usr/local/bin/ffmpeg >&2
ln -sfn ${dest_dir}/ffmpeg-10bit /usr/local/bin/ffmpeg-10bit >&2
ln -sfn ${dest_dir}/ffprobe /usr/local/bin/ffprobe >&2
ln -sfn ${dest_dir}/ffserver /usr/local/bin/ffserver >&2

# Remove unzipped folder to do some clean up
rm -fr ffmpeg-git-*-static/
printf "%s\n" "Installing new version" >&2
else
printf "%s\n" "Nothing new to download" # >&2 -- Why is this an error?
exit # 3 -- I don't think this is any error. You checked and it's fine.

fi

#EOF

下一个任务:

  • 获取脚本以检查当前用户是否具有写入权限download_dirdest_dir else 退出并提示要求新的位置或提升用户权限

再一次,我很高兴到目前为止收到的所有帮助:)

最佳答案

你迷失了自己正在做的事情。将您的脚本与这个脚本进行比较。

#!/bin/bash
# Still to come, see if the script runs with root privileges else exit

# Download FFMPEG static daily build and it's md5

# or exit if not
cd ~ || exit 2

# 1. get the file
wget https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz

# 2. Check the md5 is correct
if ! diff <(md5sum ffmpeg-git-64bit-static.tar.xz) \
<(curl -L https://johnvansickle.com/ffmpeg/builds/ffmpeg-git-64bit-static.tar.xz.md5)
then
printf "%s\n" "md5sum doesn't match..." >&2
exit 1
fi

# 3. untar
tar -xf ffmpeg-git-64bit-static.tar.xz

# 4. and so on..
cp ffmpeg-*-static/ff* /usr/bin/
cp ffmpeg-*-static/ff* /usr/local/bin/
cp ffmpeg-*-static/qt-faststart /usr/bin/
cp ffmpeg-*-static/qt-faststart /usr/local/bin/
# Consider change second location to use ln -s
# Remove downloads and do some clean up

rm -fr ffmpeg-*

#EOF

更新版本的一些修复。我删除了您的评论以使我自己的评论可见。

#!/bin/bash
download_dir=~
dest_dir=/usr/bin/
version=ffmpeg-git-64bit-static.tar.xz
source_url=https://johnvansickle.com/ffmpeg/builds/${version}
md5_url=https://johnvansickle.com/ffmpeg/builds/${version}.md5

# You need braces to build blocks.
# As it was, your script terminated at the exit. Regardless. End of story.
cd ${download_dir} || {
printf "%s\n" "You can't enter this folder." "Please change download_dir in this script..." >&2
exit 1
}


if ! diff <(md5sum ${version}) <(curl -s ${md5_url})
then
# Sum doesn't match is not really an error... I comment out the redirection.
printf "%s\n" "md5sum doesn't match..." "Downloading new version" # >&2
rm -f ${version} # >&2 -- Why would you redirect any output to stderr?
curl ${source_url} -o ${download_dir}/${version} # >&2 -- Same as above.
else
# You've done this already.
# diff <(md5sum ${version}) <(curl -s ${md5_url})
printf "%s\n" "Nothing new to download" # >&2 -- Why is this an error?
exit # 3 -- I don't think this is any error. You checked and it's fine.
# It might stay if you really MEAN it.
fi

# I'm not checking further.

tar -xf ffmpeg-git-*-static.tar.xz
mv ${download_dir}/ffmpeg-*-static/ff* "${dest_dir}"
mv ${download_dir}/ffmpeg-*-static/qt-faststart "${dest_dir}"

ln -sfn ${dest_dir}/qt-faststart /usr/local/bin/qt-faststart
ln -sfn ${dest_dir}/ffmpeg /usr/local/bin/ffmpeg
ln -sfn ${dest_dir}/ffmpeg-10bit /usr/local/bin/ffmpeg-10bit
ln -sfn ${dest_dir}/ffprobe /usr/local/bin/ffprobe
ln -sfn ${dest_dir}/ffserver /usr/local/bin/ffserver

rm -fr ffmpeg-git-*-static

您的评论问题的答案。

  1. 什么时候确定为 block ?

    Here您可以获得更广泛的 block 图。在 Bash 中,它们被称为列表。运行这个:

    man bash | grep -A 30 'Compound Commands'

    看看 Bash 的人对他们有什么看法作为一个良好的开端。 This guide应该更平易近人。

  2. 如何让它把当前运行的命令输出到控制台ex。 tar -xf 等等?

    我知道的唯一现成的解决方案是在 Debug模式下运行 Bash,如果您需要的话。您可以在脚本中的任何位置使用

     set -x

    然后你关闭它:

    set +x

    您可以在命令行中执行相同的操作。

    您还可以在 shebang 中将整个脚本设置为在此模式下运行。

    #!/bin/bash -x

    或者您可以从命令行告诉解释器以这种模式运行,

    bash -x ~/bin/your_script.bash

关于linux - 用于安装/更新 ffmpeg 静态构建的 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42497566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com