Sublime text 4的设置

  • 让状态栏显示文档编码: show_encoding设置为true;
  • 在状态栏中显示行结尾形式:"show_line_endings": true;
  • 在检测不到文档编码时默认以什么编码打开:"fallback_encoding": "Chinese (GB18030)";
  • 在查找后不要关闭查找工具条:"close_find_after_find_all": false;
  • 在替换所有后不要关闭查找工具条:"close_find_after_replace_all": false;
1
2
3
4
5
6
7
8
9
10
11
12
{
"ignored_packages":
[
"Vintage",
],
"show_encoding": true,
"show_line_endings": true,
"fallback_encoding": "Chinese (GB18030)",
"close_find_after_find_all": false,
"close_find_after_replace_all": false,
}

Ubuntu Server 常用管理

1.重启网络服务:

sudo /etc/init.d/networking restart

或者:

service networking restart

2.设置Nameserver(2种方法):

  • 修改/etc/resolv.conf,但此文件会被resolvconf重写,故应该修改resolvconf配置文件/etc/resolvconf/resolv.conf.d/head, 增加:nameserver 202.96.209.133
  • 修改/etc/network/interfaces,DNS对应的参数名为dns-nameservers,这里设置的优先级比上一种方法高。如果两种方法都设置了,则都会出现在/etc/resolv.conf中。

3.将用户加入 group:

usermod -G groupname username (这种会把用户从其他组中去掉,只属于该组)
如:usermod -G git git (git只属于git组)

usermod -a -G groupname username (把用户添加到这个组,之前所属组不影响)
如:usermod -a -G www git (git属于之前git组,也属于www组)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
xxx@xxxx:~$ cat /etc/group|grep user1
user1:x:999:user1
xxx@xxxx:~$ cat /etc/group|grep sudo
sudo:x:27:
xxx@xxxx:~$ usermod -G 27 user1
xxx@xxxx:~$ cat /etc/group|grep sudo
sudo:x:27:user1
xxx@xxxx:~$ cat /etc/group|grep user1
user1:x:999:
xxx@xxxx:~$ usermod -a -G 999 user1
xxx@xxxx:~$ cat /etc/group|grep user1
user1:x:999:user1
xxx@xxxx:~$ cat /etc/group|grep sudo
sudo:x:27:user1
1
xxx@xxxx:~$ useradd -r -m -s /bin/bash -G 27 test1 

4.修改时区和时间:

  1. 首先查看时区:

    1
    2
    redtux@redtux-PC:~$ date -R
    Thu, 31 May 2018 00:03:22 +0800
  2. 修改时区,执行sudo tzselect,按提示选择时区.

  3. 复制文件到/etc目录下 sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

  4. 更新时间 sudo ntpdate time.windows.com

5.搜索文件夹并删除

1
find ./* -name "@eaDir" -type d -print0 | xargs -0 rm -rf

6.gdisk硬盘分区工具

gdisk是fdisk分区工具的GPT版,MBR分区格式只能支持最大2T的硬盘,大于2T的硬盘必须采用GPT格式,gdisk就是针对GPT格式的升级版。

gdisk

  -l /dev/sdX 查看分区信息

gdisk

  (?|m) 帮助

  n 建立分区

  d 删除分区

  c 更改分区名字

  I 显示分区的详细信息

  p 显示分区信息

  t 转换分区类型

  a 将指定分区设置/取消 活动分区

  l 查看分区类型

  o 重建分区表

  v 验证分区表,显示剩余没有被分区划分的扇区数量

  q 退出不保存,不保存,所有的修改都不生效

  w 退出并保存

执行gdisk /dev/sdx对sdx硬盘进行编辑,分区表编辑完成保存后,需要重新启动系统,否则系统内核还不知道硬盘分区已经改变了,这个时候你ls /dev/sd*查看到的分区情况还是编辑之前的。或者可以先执行partprobe命令试一下看系统是否检测到硬盘分区的变化。

7.blkid 列出所有分区的uuid

硬盘根据插拔接口顺序不同可能是sda,也可能是sdb,这样重启后会不能正确的挂载所有的硬盘,为解决这个问题,UUID被文件系统设计者采用,使其可以持久唯一标识一个硬盘分区。其实方式很简单,就是在文件系统的超级块中使用128位存放UUID。这个UUID是在使用文件系统格式化分区时计算生成的,例如Linux下的文件系统工具mkfs就在格式化分区的同时,生成UUID并把它记录到超级块的固定区域中。

使用mkfs对分区格式化后,会显示分区的uuid,如果我们没有记录下来后面怎么查呢,这个就要用到blkid工具了,他可以列出系统中所有硬盘分区的uuid。

8.rsync 远程同步工具

rsync 是一个常用的 Linux 应用程序,用于文件同步。

它可以在本地计算机与远程计算机之间,或者两个本地目录之间同步文件(但不支持两台远程计算机之间的同步)。它也可以当作文件复制工具,替代cp和mv命令。

它名称里面的r指的是 remote,rsync 其实就是”远程同步”(remote sync)的意思。与其他文件传输工具(如 FTP 或 scp)不同,rsync 的最大特点是会检查发送方和接收方已有的文件,仅传输有变动的部分(默认规则是文件大小或修改时间有变动)。

用法参考文章

-r 参数

本机使用 rsync 命令时,可以作为cp和mv命令的替代方法,将源目录同步到目标目录。

1
$ rsync -r source destination

上面命令中,-r表示递归,即包含子目录。注意,-r是必须的,否则 rsync 运行不会成功。source目录表示源目录,destination表示目标目录。

如果有多个文件或目录需要同步,可以写成下面这样。

1
$ rsync -r source1 source2 destination

上面命令中,source1、source2都会被同步到destination目录。

-a 参数

-a参数可以替代-r,除了可以递归同步以外,还可以同步元信息(比如修改时间、权限等)。由于 rsync 默认使用文件大小和修改时间决定文件是否需要更新,所以-a比-r更有用。下面的用法才是常见的写法。

1
$ rsync -a source destination

目标目录destination如果不存在,rsync 会自动创建。执行上面的命令后,源目录source被完整地复制到了目标目录destination下面,即形成了destination/source的目录结构。

如果只想同步源目录source里面的内容到目标目录destination,则需要在源目录后面加上斜杠。

1
$ rsync -a source/ destination

上面命令执行后,source目录里面的内容,就都被复制到了destination目录里面,并不会在destination下面创建一个source子目录。

-n 参数

如果不确定 rsync 执行后会产生什么结果,可以先用-n或–dry-run参数模拟执行的结果。

1
$ rsync -anv source/ destination

上面命令中,-n参数模拟命令执行的结果,并不真的执行命令。-v参数则是将结果输出到终端,这样就可以看到哪些内容会被同步。

–delete 参数

默认情况下,rsync 只确保源目录的所有内容(明确排除的文件除外)都复制到目标目录。它不会使两个目录保持相同,并且不会删除文件。如果要使得目标目录成为源目录的镜像副本,则必须使用–delete参数,这将删除只存在于目标目录、不存在于源目录的文件。

1
$ rsync -av --delete source/ destination

上面命令中,–delete参数会使得destination成为source的一个镜像。

–exclude 参数

有时,我们希望同步时排除某些文件或目录,这时可以用–exclude参数指定排除模式。

1
2
3
$ rsync -av --exclude='*.txt' source/ destination
# 或者
$ rsync -av --exclude '*.txt' source/ destination

上面命令排除了所有 TXT 文件。

注意,rsync 会同步以”点”开头的隐藏文件,如果要排除隐藏文件,可以这样写–exclude=”.*”。

如果要排除某个目录里面的所有文件,但不希望排除目录本身,可以写成下面这样。

1
$ rsync -av --exclude 'dir1/*' source/ destination

多个排除模式,可以用多个–exclude参数。

1
$ rsync -av --exclude 'file1.txt' --exclude 'dir1/*' source/ destination

多个排除模式也可以利用 Bash 的大扩号的扩展功能,只用一个–exclude参数。

1
$ rsync -av --exclude={'file1.txt','dir1/*'} source/ destination

如果排除模式很多,可以将它们写入一个文件,每个模式一行,然后用–exclude-from参数指定这个文件。

1
$ rsync -av --exclude-from='exclude-file.txt' source/ destination

–include 参数

–include参数用来指定必须同步的文件模式,往往与–exclude结合使用。

1
$ rsync -av --include="*.txt" --exclude='*' source/ destination

上面命令指定同步时,排除所有文件,但是会包括 TXT 文件。

增量备份

rsync 的最大特点就是它可以完成增量备份,也就是默认只复制有变动的文件。

除了源目录与目标目录直接比较,rsync 还支持使用基准目录,即将源目录与基准目录之间变动的部分,同步到目标目录。

具体做法是,第一次同步是全量备份,所有文件在基准目录里面同步一份。以后每一次同步都是增量备份,只同步源目录与基准目录之间有变动的部分,将这部分保存在一个新的目标目录。这个新的目标目录之中,也是包含所有文件,但实际上,只有那些变动过的文件是存在于该目录,其他没有变动的文件都是指向基准目录文件的硬链接。

–link-dest参数用来指定同步时的基准目录。

1
$ rsync -a --delete --link-dest /compare/path /source/path /target/path

上面命令中,–link-dest参数指定基准目录/compare/path,然后源目录/source/path跟基准目录进行比较,找出变动的文件,将它们拷贝到目标目录/target/path。那些没变动的文件则会生成硬链接。这个命令的第一次备份时是全量备份,后面就都是增量备份了。

下面是一个脚本示例,备份用户的主目录。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash

# A script to perform incremental backups using rsync

set -o errexit
set -o nounset
set -o pipefail

readonly SOURCE_DIR="${HOME}"
readonly BACKUP_DIR="/mnt/data/backups"
readonly DATETIME="$(date '+%Y-%m-%d_%H:%M:%S')"
readonly BACKUP_PATH="${BACKUP_DIR}/${DATETIME}"
readonly LATEST_LINK="${BACKUP_DIR}/latest"

mkdir -p "${BACKUP_DIR}"

rsync -av --delete \
"${SOURCE_DIR}/" \
--link-dest "${LATEST_LINK}" \
--exclude=".cache" \
"${BACKUP_PATH}"

rm -rf "${LATEST_LINK}"
ln -s "${BACKUP_PATH}" "${LATEST_LINK}"

上面脚本中,每一次同步都会生成一个新目录${BACKUP_DIR}/${DATETIME},并将软链接${BACKUP_DIR}/latest指向这个目录。下一次备份时,就将${BACKUP_DIR}/latest作为基准目录,生成新的备份目录。最后,再将软链接${BACKUP_DIR}/latest指向新的备份目录。

Udisk 管理工具

udisksctl: The udisks command line tool

udisksctl status
udisksctl info {–object-path OBJECT | –block-device DEVICE | –drive DRIVE}
udisksctl mount {–object-path OBJECT | –block-device DEVICE} [–filesystem-type TYPE] [–options OPTIONS…] [–no-user-interaction]
udisksctl unmount {–object-path OBJECT | –block-device DEVICE} [–force] [–no-user-interaction]
udisksctl unlock {–object-path OBJECT | –block-device DEVICE} [–no-user-interaction] [–key-file PATH] [–read-only]
udisksctl lock {–object-path OBJECT | –block-device DEVICE} [–no-user-interaction]
udisksctl loop-setup –file PATH [–read-only] [–offset OFFSET] [–size SIZE] [–no-user-interaction]
udisksctl loop-delete {–object-path OBJECT | –block-device DEVICE} [–no-user-interaction]
udisksctl power-off {–object-path OBJECT | –block-device DEVICE} [–no-user-interaction]
udisksctl smart-simulate –file PATH {–object-path OBJECT | –block-device DEVICE} [–no-user-interaction]
udisksctl monitor
udisksctl dump
udisksctl help

命令手册

完全卸载U盘

1
udisksctl power-off --block-device /dev/sdx

自建Nextcloud私有云盘

安装

从Nextcloud官方网站下载server的安装包,复制到/var/www目录解压。

详细安装过程参考官方文档

1、按照官方文档安装完善php模块

必须要安装的模块

  • PHP (see System requirements for a list of supported versions)

  • PHP module ctype

  • PHP module curl

  • PHP module dom

  • PHP module fileinfo (included with PHP)

  • PHP module filter (only on Mageia and FreeBSD)

  • PHP module GD

  • PHP module hash (only on FreeBSD)

  • PHP module JSON (included with PHP >= 8.0)

  • PHP module libxml (Linux package libxml2 must be >=2.7.0)

  • PHP module mbstring

  • PHP module openssl (included with PHP >= 8.0)

  • PHP module posix

  • PHP module session

  • PHP module SimpleXML

  • PHP module XMLReader

  • PHP module XMLWriter

  • PHP module zip

  • PHP module zlib

选择合适的数据库模块:

  • PHP module pdo_sqlite (>= 3, usually not recommended for performance reasons)

  • PHP module pdo_mysql (MySQL/MariaDB)

  • PHP module pdo_pgsql (PostgreSQL)

推荐安装的模块:

  • PHP module bz2 (recommended, required for extraction of apps)

  • PHP module intl (increases language translation performance and fixes sorting of non-ASCII characters)

2、Apache配置文件

新增配置文件vim /etc/apache2/sites-available/nextcloud.conf,复制以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<VirtualHost *:80>
DocumentRoot /var/www/nextcloud/
ServerName your.server.com

<Directory /var/www/nextcloud/>
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews

<IfModule mod_dav.c>
Dav off
</IfModule>
</Directory>
</VirtualHost>

保存,然后运行:

1
a2ensite nextcloud.conf

启用必要的Apache模块:

1
2
3
4
5
6
a2enmod rewrite

a2enmod headers
a2enmod env
a2enmod dir
a2enmod mime

If you’re running mod_fcgi instead of the standard mod_php also enable:

1
a2enmod setenvif

最后重启Apache:

1
service apache2 restart

现在就可以访问http://your.server.com安装Nextcloud。

Redis缓存配置

修改配置文件nextcloud/config/config.php

Small/Private home server

Only use APCu:

1
'memcache.local' => '\OC\Memcache\APCu',

Organizations with single-server

Use Redis for everything except local memcache:

1
2
3
4
5
6
7
'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'redis' => [
'host' => 'localhost',
'port' => 6379,
],

Organizations with clustered setups

Use APCu for local cache and either Redis cluster …:

1
2
3
4
5
6
7
8
9
'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Redis',
'memcache.locking' => '\OC\Memcache\Redis',
'redis.cluster' => [
'seeds' => [ // provide some/all of the cluster servers to bootstrap discovery, port required
'cache-cluster:7000',
'cache-cluster:7001',
],
]

APCu is disabled by default on CLI which could cause issues with nextcloud’s cron jobs. Please make sure you set the apc.enable_cli to 1 on your php.ini config file or append --define apc.enable_cli=1 to the cron job call.

1
vim /etc/php/8.1/cli/php.ini

在末尾添加apc.enable_cli=1,否则当你运行occ命令的时候也会提示你APCu不可用。

OCC命令的使用

直接运行occ命令,不带任何选项,就会列出occ命令的所有可用的选项和用法:

1
sudo -u www-data php8.1 /var/www/nextcloud/occ

返回信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
Nextcloud 24.0.12

Usage:
command [options] [arguments]

Options:
-h, --help Display this help message
-q, --quiet Do not output any message
-V, --version Display this application version
--ansi Force ANSI output
--no-ansi Disable ANSI output
-n, --no-interaction Do not ask any interactive question
--no-warnings Skip global warnings, show command output only
-v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
check check dependencies of the server environment
help Display help for a command
list List commands
status show some status information
upgrade run upgrade routines after installation of a new release. The release has to be installed before.
activity
activity:send-mails Sends the activity notification mails
app
app:check-code check code to be compliant
app:disable disable an app
app:enable enable an app
app:getpath Get an absolute path to the app directory
app:install install an app
app:list List all available apps
app:remove remove an app
app:update update an app or all apps
background
background:ajax Use ajax to run background jobs
background:cron Use cron to run background jobs
background:webcron Use webcron to run background jobs
background-job
background-job:execute Execute a single background job manually
broadcast
broadcast:test test the SSE broadcaster
circles
circles:check Checking your configuration
circles:maintenance Clean stuff, keeps the app running
circles:manage:config edit config/type of a Circle
circles:manage:create create a new circle
circles:manage:destroy destroy a circle by its ID
circles:manage:details get details about a circle by its ID
circles:manage:edit edit displayName or description of a Circle
circles:manage:join emulate a user joining a Circle
circles:manage:leave simulate a user joining a Circle
circles:manage:list listing current circles
circles:manage:setting edit setting for a Circle
circles:members:add Add a member to a Circle
circles:members:details get details about a member by its ID
circles:members:level Change the level of a member from a Circle
circles:members:list listing Members from a Circle
circles:members:remove remove a member from a circle
circles:members:search Change the level of a member from a Circle
circles:memberships index and display memberships for local and federated users
circles:remote remote features
circles:shares:files listing shares files
circles:sync Sync Circles and Members
circles:test testing some features
config
config:app:delete Delete an app config value
config:app:get Get an app config value
config:app:set Set an app config value
config:import Import a list of configs
config:list List all configs
config:system:delete Delete a system config value
config:system:get Get a system config value
config:system:set Set a system config value
dav
dav:create-addressbook Create a dav addressbook
dav:create-calendar Create a dav calendar
dav:delete-calendar Delete a dav calendar
dav:list-calendars List all calendars of a user
dav:move-calendar Move a calendar from an user to another
dav:remove-invalid-shares Remove invalid dav shares
dav:retention:clean-up
dav:send-event-reminders Sends event reminders
dav:sync-birthday-calendar Synchronizes the birthday calendar
dav:sync-system-addressbook Synchronizes users to the system addressbook
db
db:add-missing-columns Add missing optional columns to the database tables
db:add-missing-indices Add missing indices to the database tables
db:add-missing-primary-keys Add missing primary keys to the database tables
db:convert-filecache-bigint Convert the ID columns of the filecache to BigInt
db:convert-mysql-charset Convert charset of MySQL/MariaDB to use utf8mb4
db:convert-type Convert the Nextcloud database to the newly configured one
encryption
encryption:change-key-storage-root Change key storage root
encryption:decrypt-all Disable server-side encryption and decrypt all files
encryption:disable Disable encryption
encryption:enable Enable encryption
encryption:encrypt-all Encrypt all files for all users
encryption:list-modules List all available encryption modules
encryption:migrate-key-storage-format Migrate the format of the keystorage to a newer format
encryption:set-default-module Set the encryption default module
encryption:show-key-storage-root Show current key storage root
encryption:status Lists the current status of encryption
federation
federation:sync-addressbooks Synchronizes addressbooks of all federated clouds
files
files:cleanup cleanup filecache
files:recommendations:recommend
files:repair-tree Try and repair malformed filesystem tree structures
files:scan rescan filesystem
files:scan-app-data rescan the AppData folder
files:transfer-ownership All files and folders are moved to another user - outgoing shares and incoming user file shares (optionally) are moved as well.
files_external
files_external:applicable Manage applicable users and groups for a mount
files_external:backends Show available authentication and storage backends
files_external:config Manage backend configuration for a mount
files_external:create Create a new mount configuration
files_external:delete Delete an external mount
files_external:export Export mount configurations
files_external:import Import mount configurations
files_external:list List configured admin or personal mounts
files_external:notify Listen for active update notifications for a configured external mount
files_external:option Manage mount options for a mount
files_external:verify Verify mount configuration
group
group:add Add a group
group:adduser add a user to a group
group:delete Remove a group
group:info Show information about a group
group:list list configured groups
group:removeuser remove a user from a group
integrity
integrity:check-app Check integrity of an app using a signature.
integrity:check-core Check integrity of core code using a signature.
integrity:sign-app Signs an app using a private key.
integrity:sign-core Sign core using a private key.
l10n
l10n:createjs Create javascript translation files for a given app
log
log:file manipulate logging backend
log:manage manage logging configuration
log:tail Tail the nextcloud logfile
log:watch Watch the nextcloud logfile
maintenance
maintenance:data-fingerprint update the systems data-fingerprint after a backup is restored
maintenance:mimetype:update-db Update database mimetypes and update filecache
maintenance:mimetype:update-js Update mimetypelist.js
maintenance:mode set maintenance mode
maintenance:repair repair this installation
maintenance:repair-share-owner repair invalid share-owner entries in the database
maintenance:theme:update Apply custom theme changes
maintenance:update:htaccess Updates the .htaccess file
notification
notification:generate Generate a notification for the given user
notification:test-push Generate a notification for the given user
preview
preview:repair distributes the existing previews into subfolders
preview:reset-rendered-texts Deletes all generated avatars and previews of text and md files
security
security:bruteforce:reset resets bruteforce attemps for given IP address
security:certificates list trusted certificates
security:certificates:import import trusted certificate in PEM format
security:certificates:remove remove trusted certificate
serverinfo
serverinfo:update-storage-statistics Triggers an update of the counts related to storages used in serverinfo
sharing
sharing:cleanup-remote-storages Cleanup shared storage entries that have no matching entry in the shares_external table
sharing:expiration-notification Notify share initiators when a share will expire the next day.
support
support:report Generate a system report
tag
tag:add Add new tag
tag:delete delete a tag
tag:edit edit tag attributes
tag:list list tags
text
text:reset Reset a text document
theming
theming:config Set theming app config values
trashbin
trashbin:cleanup Remove deleted files
trashbin:expire Expires the users trashbin
trashbin:restore Restore all deleted files
trashbin:size Configure the target trashbin size
twofactorauth
twofactorauth:cleanup Clean up the two-factor user-provider association of an uninstalled/removed provider
twofactorauth:disable Disable two-factor authentication for a user
twofactorauth:enable Enable two-factor authentication for a user
twofactorauth:enforce Enabled/disable enforced two-factor authentication
twofactorauth:state Get the two-factor authentication (2FA) state of a user
update
update:check Check for server and app updates
user
user:add adds a user
user:add-app-password Add app password for the named user
user:delete deletes the specified user
user:disable disables the specified user
user:enable enables the specified user
user:info show user info
user:lastseen shows when the user was logged in last time
user:list list configured users
user:report shows how many users have access
user:resetpassword Resets the password of the named user
user:setting Read and modify user settings
versions
versions:cleanup Delete versions
versions:expire Expires the users file versions
workflows
workflows:list Lists configured workflows

扫描所有用户文件更新:

1
sudo -u www-data php8.1 /var/www/nextcloud/occ files:scan --all

扫描特定用户文件更新:

1
sudo -u www-data php8.1 /var/www/nextcloud/occ files:scan username

扫描特定目录文件更新:

1
sudo -u www-data php8.1 /var/www/nextcloud/occ files:scan --path=/path/to/scan

后台服务优化

后台服务有三种方式:AJAX,Webcron,Cron

systemd

If systemd is installed on the system, a systemd timer could be an alternative to a cronjob.

This approach requires two files: nextcloudcron.service and nextcloudcron.timer. Create these two files in /etc/systemd/system/.

nextcloudcron.service should look like this:

1
2
3
4
5
6
7
[Unit]
Description=Nextcloud cron.php job

[Service]
User=www-data
ExecStart=/usr/bin/php -f /var/www/nextcloud/cron.php
KillMode=process

Replace the user www-data with the user of your http server and /var/www/nextcloud/cron.php with the location of cron.php in your nextcloud directory.

The KillMode=process setting is necessary for external programs that are started by the cron job to keep running after the cron job has finished.

Note that the .service unit file does not need an [Install] section. Please check your setup because we recommended it in earlier versions of this admin manual.

nextcloudcron.timer should look like this:

1
2
3
4
5
6
7
8
9
10
[Unit]
Description=Run Nextcloud cron.php every 5 minutes

[Timer]
OnBootSec=5min
OnUnitActiveSec=5min
Unit=nextcloudcron.service

[Install]
WantedBy=timers.target

The important parts in the timer-unit are OnBootSec and OnUnitActiveSec. OnBootSec will start the timer 5 minutes after boot, otherwise you would have to start it manually after every boot. OnUnitActiveSec will set a 5 minute timer after the service-unit was last activated.

Now all that is left is to start and enable the timer by running this command:

1
systemctl enable --now nextcloudcron.timer

When the option --now is used with enable, the respective unit will also be started.

客户端下载

直接从这里下载https://download.nextcloud.com/desktop/releases/

Microsoft Office及Windows的KMS激活步骤

Docker 部署KMS 服务器

Docker镜像地址: https://hub.docker.com/r/mogeko/vlmcsd

此程序的单独地址: https://github.com/Wind4/vlmcsd

部署KMS服务器,使用Docker ,端口为 1688。

1
# docker run -d -p 1688:1688 --restart=always --name vlmcsd mikolatero/vlmcsd
1
2
3
4
5
6
7
docker logs vlmcsd

2019-07-27 08:14:00: Listening on [::]:1688

2019-07-27 08:14:00: Listening on 0.0.0.0:1688

2019-07-27 08:14:00: vlmcsd svn1112-2-gf1a3c7f, built 2019-07-26 18:11:51 UTC started successfully

查看日志,确定正常工作。

Microsoft Office KMS激活步骤:

  1. 以管理员用户打开命令行工具;
  2. 切换到office的安装目录,例如office2016安装路径为C:\\Program Files\\Microsoft Office\\Office16
  3. 设置kms服务器:cscript ospp.vbs /sethst:[kms.03k.org](http://kms.03k.org),返回信息;
1
2
3
4
5
6
7
8
9
10
---Processing--------------------------

---------------------------------------

Successfully applied setting.

---------------------------------------

---Exiting-----------------------------

  1. 激活office:`cscript ospp.vbs /act`。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

---Processing--------------------------

---------------------------------------

Installed product key detected - attempting to activate the following product:

SKU ID: d450596f-894d-49e0-966a-fd39ed4c4c64

LICENSE NAME: Office 16, Office16ProPlusVL_KMS_Client edition

LICENSE DESCRIPTION: Office 16, VOLUME_KMSCLIENT channel

Last 5 characters of installed product key: WFG99

<Product activation successful>

---------------------------------------

---------------------------------------

---Exiting-----------------------------

Microsoft Windows KMS激活步骤:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 卸载已安装密钥
slmgr.vbs /upk

# 安装需要激活的版本密钥
# 这里以专业版为例,Windows 10 和 11 通用
# 更多版本的系统密钥见下文
slmgr /ipk W269N-WFGWX-YVC9B-4J6C9-T83GX

# 设置 KMS 激活服务器
# 请将 192.168.7.186 替换为实际 KMS 服务器网址或 IP
slmgr /skms 192.168.7.186

# 激活
slmgr /ato

所有WINDOWS版本的GVLK密钥对照表

最新的参看微软官方网站

GVLK密钥是专门用于KMS激活的密钥,如果想使用KMS激活,那么必须先将系统的KEY替换为对应版本的GVLK密钥。

KMS不仅可以激活Windows 8、Windows 8.1和Windows 7、Windows 10这类我们常用的系统,还可以激活各种版本的Windows Server系统。

以下是GVLK密钥版本对照表,可配合KMS服务器进行使用。

WINDOWS SERVER 半年频道版本

Windows Server 版本 1909、版本1903 和版本 1809

操作系统版本 KMS 客户端安装程序密钥
Windows Server Datacenter 6NMRW-2C8FM-D24W7-TQWMY-CWH2D
Windows Server Standard N2KJX-J94YW-TQVFB-DG9YT-724CC

WINDOWS SERVER LTSC/LTSB 版本

Windows Server 2019

操作系统版本 KMS 客户端安装程序密钥
Windows Server 2019 Datacenter WMDGN-G9PQG-XVVXX-R3X43-63DFG
Windows Server 2019 Standard N69G4-B89J2-4G8F4-WWYCC-J464C
Windows Server 2019 Essentials WVDHN-86M7X-466P6-VHXV7-YY726

Windows Server 2016

操作系统版本 KMS 客户端安装程序密钥
Windows Server 2016 Datacenter CB7KF-BWN84-R7R2Y-793K2-8XDDG
Windows Server 2016 Standard WC2BQ-8NRM3-FDDYY-2BFGV-KHKQY
Windows Server 2016 Essentials JCKRF-N37P4-C2D82-9YXRT-4M63B

WINDOWS 10,所有支持的半年频道版本

有关受支持的版本和服务终止日期的信息,请参阅 Windows 生命周期情况说明书。

操作系统版本 KMS 客户端安装程序密钥
Windows 10 专业版 W269N-WFGWX-YVC9B-4J6C9-T83GX
Windows 10 专业版 N MH37W-N47XK-V7XM9-C7227-GCQG9
Windows 10 专业工作站版 NRG8B-VKK3Q-CXVCJ-9G2XF-6Q84J
Windows 10 专业工作站版 N 9FNHH-K3HBT-3W4TD-6383H-6XYWF
Windows 10 专业教育版 6TP4R-GNPTD-KYYHQ-7B7DP-J447Y
Windows 10 专业教育版 N YVWGF-BXNMC-HTQYQ-CPQ99-66QFC
Windows 10 教育版 NW6C2-QMPVW-D7KKK-3GKT6-VCFB2
Windows 10 教育版 N 2WH4N-8QGBV-H22JP-CT43Q-MDWWJ
Windows 10 企业版 NPPR9-FWDCX-D2C8J-H872K-2YT43
Windows 10 企业版 N DPH2V-TTNVB-4X9Q3-TJR4H-KHJW4
Windows 10 企业版 G YYVX9-NTFWV-6MDM3-9PT4T-4M68B
Windows 10 企业版 G N 44RPN-FTY23-9VTTB-MP9BX-T84FV

WINDOWS 10 LTSC/LTSB 版本

Windows 10 LTSC 2019

操作系统版本 KMS 客户端安装程序密钥
Windows 10 企业版 LTSC 2019 M7XTQ-FN8P6-TTKYV-9D4CC-J462D
Windows 10 企业版 N LTSC 2019 92NFX-8DJQP-P6BBQ-THF9C-7CG2H

Windows 10 LTSB 2016

操作系统版本 KMS 客户端安装程序密钥
Windows 10 企业版 LTSB 2016 DCPHK-NFMTC-H88MJ-PFHPY-QJ4BJ
Windows 10 企业版 N LTSB 2016 QFFDN-GRT3P-VKWWX-X7T3R-8B639

Windows 10 LTSB 2015

操作系统版本 KMS 客户端安装程序密钥
Windows 10 企业版 2015 LTSB WNMTR-4C88C-JK8YV-HQ7T2-76DF9
Windows 10 企业版 2015 LTSB N 2F77B-TNFGY-69QQF-B8YKP-D69TJ

早期版本的 WINDOWS SERVER

Windows Server 版本 1803

操作系统版本 KMS 客户端安装程序密钥
Windows Server Datacenter 2HXDN-KRXHB-GPYC7-YCKFJ-7FVDG
Windows Server Standard PTXN8-JFHJM-4WC78-MPCBR-9W4KR

Windows Server 版本 1709

操作系统版本 KMS 客户端安装程序密钥
Windows Server Datacenter 6Y6KB-N82V8-D8CQV-23MJW-BWTG6
Windows Server Standard DPCNP-XQFKJ-BJF7R-FRC8D-GF6G4

Windows Server 2012 R2

操作系统版本 KMS 客户端安装程序密钥
Windows Server 2012 R2 Server Standard D2N9P-3P6X9-2R39C-7RTCD-MDVJX
Windows Server 2012 R2 Datacenter W3GGN-FT8W3-Y4M27-J84CP-Q3VJ9
Windows Server 2012 R2 Essentials KNC87-3J2TX-XB4WP-VCPJV-M4FWM

Windows Server 2012

操作系统版本 KMS 客户端安装程序密钥
Windows Server 2012 BN3D2-R7TKB-3YPBD-8DRP2-27GG4
Windows Server 2012 N 8N2M2-HWPGY-7PGT9-HGDD8-GVGGY
Windows Server 2012 单语言版 2WN2H-YGCQR-KFX6K-CD6TF-84YXQ
Windows Server 2012 特定国家/地区版 4K36P-JN4VD-GDC6V-KDT89-DYFKP
Windows Server 2012 Server 标准版 XC9B7-NBPP2-83J2H-RHMBY-92BT4
Windows Server 2012 MultiPoint 标准版 HM7DN-YVMH3-46JC3-XYTG7-CYQJJ
Windows Server 2012 MultiPoint 高级版 XNH6W-2V9GX-RGJ4K-Y8X6F-QGJ2G
Windows Server 2012 Datacenter 48HP8-DN98B-MYWDG-T2DCC-8W83P

Windows Server 2008 R2

操作系统版本 KMS 客户端安装程序密钥
Windows Server 2008 R2 Web 版 6TPJF-RBVHG-WBW2R-86QPH-6RTM4
Windows Server 2008 R2 HPC 版 TT8MH-CG224-D3D7Q-498W2-9QCTX
Windows Server 2008 R2 标准版 YC6KT-GKW9T-YTKYR-T4X34-R7VHC
Windows Server 2008 R2 企业版 489J6-VHDMP-X63PK-3K798-CPX3Y
Windows Server 2008 R2 Datacenter 74YFP-3QFB3-KQT8W-PMXWJ-7M648
面向基于 Itanium 系统的 Windows Server 2008 R2 GT63C-RJFQ3-4GMB6-BRFB9-CB83V

Windows 2008 Server

操作系统版本 KMS 客户端安装程序密钥
Windows Web Server 2008 WYR28-R7TFJ-3X2YQ-YCY4H-M249D
Windows Server 2008 标准版 TM24T-X9RMF-VWXK6-X8JC9-BFGM2
不带 Hyper-V 的 Windows Server 2008 标准版 W7VD6-7JFBR-RX26B-YKQ3Y-6FFFJ
Windows Server 2008 企业版 YQGMW-MPWTJ-34KDK-48M3W-X4Q6V
不带 Hyper-V 的 Windows Server 2008 企业版 39BXF-X8Q23-P2WWT-38T2F-G3FPG
Windows Server 2008 HPC RCTX3-KWVHP-BR6TB-RB6DM-6X7HP
Windows Server 2008 Datacenter 7M67G-PC374-GR742-YH8V4-TCBY3
不带 Hyper-V 的 Windows Server 2008 数据中心版 22XQ2-VRXRG-P8D42-K34TD-G3QQC
面向基于 Itanium 系统的 Windows Server 2008 4DWFP-JF3DJ-B7DTH-78FJB-PDRHK

早期版本的 WINDOWS

Windows 8.1

操作系统版本 KMS 客户端安装程序密钥
Windows 8.1 专业版 GCRJD-8NW9H-F2CDX-CCM8D-9D6T9
Windows 8.1 专业版 N HMCNV-VVBFX-7HMBH-CTY9B-B4FXY
Windows 8.1 企业版 MHF9N-XY6XB-WVXMC-BTDCT-MKKG7
Windows 8.1 企业版 N TT4HM-HN7YT-62K67-RGRQJ-JFFXW

Windows 8

操作系统版本 KMS 客户端安装程序密钥
Windows 8 专业版 NG4HW-VH26C-733KW-K6F98-J8CK4
Windows 8 专业版 N XCVCF-2NXM9-723PB-MHCB7-2RYQQ
Windows 8 企业版 32JNW-9KQ84-P47T8-D8GGY-CWCK7
Windows 8 企业版 N JMNMF-RHW7P-DMY6X-RF3DR-X2BQT

Windows 7

操作系统版本 KMS 客户端安装程序密钥
Windows 7 专业版 FJ82H-XT6CR-J8D7P-XQJJ2-GPDD4
Windows 7 专业版 N MRPKT-YTG23-K7D7T-X2JMM-QY7MG
Windows 7 专业版 E W82YF-2Q76Y-63HXB-FGJG9-GF7QX
Windows 7 企业版 33PXH-7Y6KF-2VJC9-XBBR8-HVTHH
Windows 7 企业版 N YDRBP-3D83W-TY26F-D46B2-XCKRJ
Windows 7 企业版 E C29WB-22CC8-VJ326-GHFJW-H9DH4

Qt实现弹出窗口,点击其他位置消失

Qt实现弹出窗口,点击其他位置消失

一开始时想的很美好,写button的点击事件,然后在弹窗界面中写eventFilter(QEvent *e)事件

1
2
3
4
5
6
7
8
if (event->type() == QEvent::ActivationChange)
{
if(QApplication::activeWindow() != this)
{
this->close();
}
}
return QWidget::event(event);

但是这样写发现一个问题,当我想实现点击按钮也可以关闭dialog时,不行了,因为EventFilter是第一触发事件,当我点按钮是,判断当前的活动窗口不是dialog,他先关闭,然后触发按钮的点击事件,再打开了dialog。很麻烦。

后来有了解到Qt有一个popup的属性

1
info->setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);

这样设置了之后,窗口是无边框且是点击其他位置关闭,但是还是会有上面的问题,没解决。

再后来,在一篇blog中看到了Qt::WA_NoMouseReplay属性
机翻一下:用于弹出窗口小部件。指示在弹出小部件关闭时不应重播最近的鼠标按下事件。该标志由小部件的作者设置,并在小部件每次接收到新的鼠标事件时由Qt内核清除。
大意就是它会拦截鼠标事件不会传递,专用于弹窗事件。
呜呜呜,原来Qt早就搞定了这个事件,找了半天,还是Qt不熟悉。
具体使用,在弹出的窗口里重写mousePressEvent(QMouseEvent *e)事件:

1
2
3
4
5
void InfoDialog::mousePressEvent(QMouseEvent *e)
{
setAttribute(Qt::WA_NoMouseReplay);
QDialog::mousePressEvent(e);
}

Qt的宏定义区分不同操作系统

程序中遇到需要区分操作系统时,可使用qt的宏定义:

中,定义了各个系统的宏定义

Q_OS_AIX
Defined on AIX.
Q_OS_ANDROID
Defined on Android.
Q_OS_BSD4
Defined on Any BSD 4.4 system.
Q_OS_BSDI
Defined on BSD/OS.
Q_OS_CYGWIN
Defined on Cygwin.
Q_OS_DARWIN
Defined on Darwin-based operating systems such as macOS, iOS, watchOS, and tvOS.
Q_OS_DGUX
Defined on DG/UX.
Q_OS_DYNIX
Defined on DYNIX/ptx.
Q_OS_FREEBSD
Defined on FreeBSD.
Q_OS_HPUX
Defined on HP-UX.
Q_OS_HURD
Defined on GNU Hurd.
Q_OS_IOS
Defined on iOS.
Q_OS_IRIX
Defined on SGI Irix.
Q_OS_LINUX
Defined on Linux.
Q_OS_LYNX
Defined on LynxOS.
Q_OS_MAC
Deprecated synonym for Q_OS_DARWIN. Do not use.
Q_OS_MACOS
Defined on macOS.
Q_OS_NETBSD
Defined on NetBSD.
Q_OS_OPENBSD
Defined on OpenBSD.
Q_OS_OSF
Defined on HP Tru64 UNIX.
Q_OS_OSX
Deprecated synonym for Q_OS_MACOS. Do not use.
Q_OS_QNX
Defined on QNX Neutrino.
Q_OS_RELIANT
Defined on Reliant UNIX.
Q_OS_SCO
Defined on SCO OpenServer 5.
Q_OS_SOLARIS
Defined on Sun Solaris.
Q_OS_TVOS
Defined on tvOS.
Q_OS_ULTRIX
Defined on DEC Ultrix.
Q_OS_UNIX
Defined on Any UNIX BSD/SYSV system.
Q_OS_UNIXWARE
Defined on UnixWare 7, Open UNIX 8.
Q_OS_WATCHOS
Defined on watchOS.
Q_OS_WIN32
Defined on 32-bit and 64-bit versions of Windows.
Q_OS_WIN64
Defined on 64-bit versions of Windows.
Q_OS_WIN
Defined on all supported versions of Windows. That is, if Q_OS_WIN32, Q_OS_WIN64, or Q_OS_WINRT is defined.
Q_OS_WINPHONE
Defined on Windows Phone 8.
Q_OS_WINRT
Defined for Windows Runtime (Windows Store apps) on Windows 8, Windows RT, and Windows Phone 8.

举例1:
记得需要有QObject的头文件,才可以

1
2
3
4
5
6
7
#include<QObject>

#ifdef Q_OS_WIN
#include <winsock2.h>
#endif

#include <mysql.h>

举例2:
对于标记了Q_OBJECT这个宏的类,其成员函数可以直接上面的区分操作系统的这些宏定义。

***.h文件:

1
2
3
4
5
6
7
8
9
10
11
class DetailStepInfo : public QObject
{
Q_OBJECT

public:

....................

...................

}

***.cpp文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool DetailStepInfo::**************()
{

#ifdef Q_OS_WIN

#endif

#ifdef Q_OS_LINUX
QString command = QString("chmod +rw /****************************");
QProcess proc;
proc.execute(command);
#endif

...........................

}

ACE Editor修改自动换号缩进

https://stackoverflow.com/questions/38323427/remove-word-wrap-offset-in-ace-editor

This is controlled by indentedSoftWrap setting in ace, you can turn it off by running

1
editor.setOption("indentedSoftWrap", false);

behaviours setting is completely unrelated and controls automatic insertion of closing brackets and tags.

So your code from the above would become

1
2
3
4
5
6
7
8
9
var editor = ace.edit("edittext");
editor.setOptions({
maxLines: Infinity, // this is going to be very slow on large documents
useWrapMode: true, // wrap text to view
indentedSoftWrap: false,
behavioursEnabled: false, // disable autopairing of brackets and tags
showLineNumbers: false, // hide the gutter
theme: "ace/theme/xcode"
});

Vmware ESXi 6.7常用设置备忘录

1. ESXi关闭超线程以提高安全性

在主机-配置-系统-高级系统设置-编辑;搜索参数:VMkernel.Boot.hyperthreadingMitigation,有两项配置,参数配置为true ,即为关闭超线程。

2. 虚拟机使用主机物理硬盘

如果是nvme固态硬盘可以在硬件列表中开启直通,然后在虚拟机设置中添加对应的pci设备。

如果是sata硬盘,则需要ssh登录,使用vmkfstools命令创建物理硬盘的映像:

1
vmkfstools -z /vmfs/devices/disks/<硬盘标识符> /vmfs/volumes/datastore1/<目标RDM磁盘名>.vmdk

硬盘标识符为存储、设备选项卡里面磁盘列表,点击对应磁盘,查看到的硬盘路径,类似:/vmfs/devices/disks/t10.ATA_____WDC__WUH721414ALE6L4____________________QBHB0G1T____________

3. 序列号

企业版参考序列号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
HV4WC-01087-1ZJ48-031XP-9A843 

NF0F3-402E3-MZR80-083QP-3CKM2

4F6FX-2W197-8ZKZ9-Y31ZM-1C3LZ

JZ2E9-6D2DK-XZQD0-632E4-33E7Z

MZ48M-DNK56-ZZJD0-RTCE2-9321X

0Y0AJ-4P29H-LZV81-59AQ2-C291V

0A4GL-D7JD7-LZR10-M30G0-A36JF

4C02A-DP2E2-AZ041-FH9X6-922J6

4. 查看硬盘健康状况

使用系统自带命令esxcli

列出硬盘

1
esxcli storage core device list

查看硬盘smart信息

1
esxcli storage core device smart get -d <disk> 

安装Jellyfin多媒体服务器

由于 Jellyfin 的 GPL 协议和 Intel 的 media-driver (iHD) Linux 驱动(部分开源)在协议上不兼容的缘故,Jellyfin 官方的 Docker 镜像:jellyfin/jellyfin 并不包含 Intel Linux 核显驱动。但是 Intel 的 QuickSync QSV 硬件加速依赖于该驱动,并且 Intel 10 代酷睿(Comet Lake)以及更新的处理器需要该驱动才能正常使用硬件加速。另外 VPP/OpenCL 色调映射也需要该驱动才能正常运作。

1 安装

开发者提供的中国特供版镜像
Docker 镜像: docker pull nyanmisaka/jellyfin:latest

Jellyfin中国特供版+Docker镜像,含驱动,免折腾开箱即用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
sudo docker run -d --name=Jellyfin -p 8096:8096 \  # --name=Jellyfin 将容器名定义为 Jellyfin 
-p 8920:8920 -p 7359:7359/udp -p 1900:1900/udp #这三个端口为可选项 \
-v /var/docker/jellyfin/library:/config -v /var/docker/jellyfin/cache:/cache -v /data/media:/media \
-e TZ=Asia/Shanghai -e PUID=0 -e PGID=0 \ #将容器的时区设为上海,使用窗口在运行时使用root权限
--device=/dev/dri:/dev/dri \ #直通显卡给 Docker 容器,用于硬解
--add-host=api.themoviedb.org:13.224.161.90 \ #为容器增加 host 指向,加速海报与影视元数据的搜刮
--add-host=api.themoviedb.org:13.35.8.65 \
--add-host=api.themoviedb.org:13.35.8.93 \
--add-host=api.themoviedb.org:13.35.8.6 \
--add-host=api.themoviedb.org:13.35.8.54 \
--add-host=image.tmdb.org:138.199.37.230 \
--add-host=image.tmdb.org:108.138.246.49 \
--add-host=api.thetvdb.org:13.225.89.239 \
--add-host=api.thetvdb.org:192.241.234.54 \
--restart unless-stopped \
jellyfin/jellyfin:latest

1.如果使用 linuxserver/jellyfin 镜像,就把最后一行替换为下行

1
lscr.io/linuxserver/jellyfin:latest

2.如果使用 nyanmisaka/jellyfin 镜像,最把最后一行替换为下行

1
nyanmisaka/jellyfin:latest

–add-host是指定容器中的域名解析ip,因为每个地方、不同时间ip访问稳定性不同,所以最好不在这里指定,而是用bind解析,详见 安装Bind9用于本地域名解析。已经添加了--add-host参数想删掉的话,可以进入容器配置目录,编辑hostconfig.json文件,把ExtraHosts值清空:

1
"ExtraHosts":[],

修改容器配置需要先停掉容器,容器配置目录默认位于/var/lib/docker/containers/,里面目录是容器的全id,查询容器的全ID使用命令docker inspect 容器ID |grep Id

端口说明:

端口号 用途 可选项
8096 默认http端口号 必须
8920 默认https端口号 可选
7359 让同一局域网中的客户端设备自动发现 可选
1900 DLNA的端口 可选

2 升级

拉取最新版镜像:

1
docker pull nyanmisaka/jellyfin:latest

重命名原容器名

1
docker rename Jellyfin Jellyfin-10.8.13

用最新镜像重新启动一个容器

1
docker run -d --name=Jellyfin -p 8096:8096 -p 8920:8920 -p 7359:7359/udp -p 1900:1900/udp -v /var/docker/jellyfin/library:/config -v /var/docker/jellyfin/cache:/cache -v /data/media:/media -e TZ=Asia/Shanghai -e PUID=0 -e PGID=0 --device=/dev/dri:/dev/dri --restart unless-stopped nyanmisaka/jellyfin:latest

删除原来的容器:

1
docker rm Jellyfin-10.8.13

清理冗余镜像:

1
docker image prune

ffmpeg的常用选项

基础指令

一.查看FFmpeg支持的编码器

1
ffmpeg configure -encoders

二.查看FFmpeg支持的解码器

1
ffmpeg configure -decoders

三.查看FFmpeg支持的通信协议

1
ffmpeg configure -protocols

四.查看FFmpeg所支持的音视频编码格式、文件封装格式与流媒体传输协议

1
ffmpeg configure --help

map选项

默认情况下只在输入文件中的每种流里选择第一个流。

需要指定放入输出文件的流就需要使用-map选项。

用法举例:

-map 0
From input index #0 (the 1st input) select all streams.

-map 1:a
From input index #1 (the 2nd input) select all audio streams.

-map 3:s:4
From input index #3 (the 4th input) select subtitle stream index #4 (the fifth subtitle stream)。

-map 0 -map -0:s
Will select all streams from input index #0 (the 1st input) except subtitles.

改变视频帧率

有两种方法可以改变输出文件的视频流帧率:

  1. 使用 -r 选项
  2. 使用 fps滤镜。
1
2
ffmpeg -i [inputfile] -r 30 [output]
ffmpeg -i [inputfiel] -filter:v fps=30 [output]

有两种方法可以改变输出文件的视频流帧率:

改变默认音轨

使用ffmpeg修改默认的音频轨道,先取消第二音轨的默认值,在设置为第一音轨。

1
ffmpeg -i input.mkv -map 0:0 -map 0:1 -map 0:2  -c copy -disposition:a:1 0 -disposition:a:0 default -y output.mp4

改变音频采样率

-ar 48k 44.1k 96k

1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k output.flac

改变音频通道数量

-ac

1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k -ac:0 2 -filter:0 aformat=channel_layouts=stereo output.flac
1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k -ac:0 6 -filter:0 aformat=channel_layouts=5.1 output.flac

改变音频位深 bit depth

-sample_fmt

1
ffmpeg -i input.dsf -map 0:0 -c:0 flac -ar:0 96k -sample_fmt s24 -ac:0 6 -filter:0 aformat=channel_layouts=5.1 output.flac

生成空白音频

可以使用anullsrc来生成静音音频:

1
ffmpeg -f lavfi -i anullsrc=r=48000:cl=stereo -t 0.875 /Users/redtux/Movies/converted/silent-audio.aac
  • sample_rate, r
    指定采样率,默认44100。

  • channel_layout, cl
    指定通道布局,详见 libavutil/channel_layout.cchannel_layout_map 定义,常见mono stereo 5.1

  • -t
    文件时长,单位事秒,不指定就一直生成。

截取视频片段

1
ffmpeg  -i 源文件名 -vcodec copy -acodec copy -ss 00:00:10 -to 00:00:15 目标文件名 -y

视频拼接

一.将4个视频拼接成一个很长的视频(无声音)

1
ffmpeg -i 0.mp4 -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex '[0:0][1:0] [2:0][3:0] concat=n=4:v=1 [v]' -map '[v]' output.mp4

二.将4个视频拼接成一个很长的视频(有声音)

1
ffmpeg -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex '[0:0][0:1] [1:0][1:1] [2:0][2:1] concat=n=3:v=1:a=1 [v][a]' -map '[v]' -map '[a]’  output.mp4

参数解释:

1
2
3
4
5
6
7
8
[0:0][0:1] [1:0][1:1] [2:0][2:1] 
分别表示第1个输入文件的视频、音频,第2个输入文件的视频、音频,第3个输入文件的视频、音频。

concat=n=3:v=1:a=1
表示有3个输入文件,输出一条视频流和一条音频流。

[v][a]
得到的视频流和音频流的名字,注意在 bash 等 shell 中需要用引号,防止通配符扩展。

四.竖向拼接2个视频

1
ffmpeg -i 0.mp4 -i 1.mp4 -filter_complex "[0:v]pad=iw:ih*2[a];[a][1:v]overlay=0:h" out_2.mp4

五.横向拼接3个视频

1
ffmpeg -i 0.mp4 -i 1.mp4 -i 2.mp4 -filter_complex "[0:v]pad=iw*3:ih*1[a];[a][1:v]overlay=w[b];[b][2:v]overlay=2.0*w" out_v3.mp4

六.竖向拼接3个视频

1
ffmpeg -i 0.mp4 -i 1.mp4 -i 2.mp4 -filter_complex "[0:v]pad=iw:ih*3[a];[a][1:v]overlay=0:h[b];[b][2:v]overlay=0:2.0*h" out_v4.mp4

七.4个视频2x2方式排列

1
ffmpeg -i 0.mp4 -i 1.mp4 -i 2.mp4 -i 3.mp4 -filter_complex "[0:v]pad=iw*2:ih*2[a];[a][1:v]overlay=w[b];[b][2:v]overlay=0:h[c];[c][3:v]overlay=w:h" out.mp4

视频帧操作

一.查看每帧的信息

1
ffprobe -v error -show_frames gemfield.mp4 

从pict_type=I可以看出这是个关键帧,然后key_frame=1 表示这是IDR frame,如果key_frame=0表示这是Non-IDR frame。

二.截取视频中的某一帧
把gemfield.mp4视频的第1分05秒的一帧图像截取出来。

1
2
3
4
# input seeking
ffmpeg -ss 00:1:05 -i gemfield.mp4 -frames:v 1 out.jpg
# output seeking
ffmpeg -i gemfield.mp4 -ss 00:1:05 -frames:v 1 out1.jpg

参数解释:

-frame:v 1,在video stream上截取1帧。
input seeking使用的是key frames,所以速度很快;而output seeking是逐帧decode,直到1分05秒,所以速度很慢。

重要说明:

ffmpeg截取视频帧有2种 seeking 方式,对应有2种 coding 模式:transcoding 和 stream copying(ffmpeg -c copy)。

transcoding 模式:需要 decoding + encoding 的模式,即先 decoding 再encoding。

stream copying 模式:不需要decoding + encoding的模式,由命令行选项-codec加上参数copy来指定(-c:v copy )。在这种模式下,ffmpeg在video stream上就会忽略 decoding 和 encoding步骤。

三.查看视频总帧数

1
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_frames -of default=nokey=1:noprint_wrappers=1 gemfield.mp4

四.查看 key frame 帧数

1
ffprobe -v error -count_frames -select_streams v:0 -show_entries stream=nb_read_frames -of default=nokey=1:noprint_wrappers=1 -skip_frame nokey gemfield.mp4

五.查看 key frame 所在的时间

1
ffprobe -v error -skip_frame nokey -select_streams v:0 -show_entries frame=pkt_pts_time -of csv=print_section=0 gemfield.mp4

六.查看 key frame 分布的情况

1
ffprobe -v error -show_frames gemfield.mp4 | grep pict_type

七.查看 key frame 所在的帧数

1
ffprobe -v error -select_streams v -show_frames -show_entries frame=pict_type -of csv gemfield.mp4 | grep -n I | cut -d ':' -f 1

八.重新设置 key frame interval

1
ffmpeg -i gemfield.mp4 -vcodec libx264 -x264-params keyint=1:scenecut=0 -acodec copy out.mp4

九.查看视频波特率

1
ffprobe -v error -select_streams v:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 gemfield.mp4

调整播放速度

调整视频文件中视频速度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//2倍速播放
ffmpeg -i test.mp4 -an -filter:v "setpts=0.5*PTS" out_test.mp4

-i test.mp4是输入文件名

-an 将音频禁掉 (可以不加)

-filter:v 对视频进行处理

"setpts=0.5PTS" 设置时间戳参数PTS为原先的一半,可接受调整范围为[0.25,4]

out_test.mp4 输出视频文件

还可以在命令中加上指定fps(-r 60),使得不会丢帧

调整视频文件中音频速度:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ffmpeg -i test.mp4 -filter:a "atempo=2.0" -vn out_test.mp4
-i 后满test.mp4是输入文件名

-filter:a 对音频进行处理

"atempo=2.0" 设置播放速度是原来的2倍 , 倍率调整范围[0.5, 2.0]

-vn 将视频禁掉 (可以不加)

out_test.mp4 输出视频文件

需要调整到4倍可以采取以下方法:

ffmpeg -i test.mp4 -filter:a "atempo=2.0,atempo=2.0" -vn out_test.mp4

同时调整视频文件的视频、音频:

1
ffmpeg -i test.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]"  out_test.mp4

如果不想丢帧就同时设置 -r ,把帧率设置为原帧率的2倍。

metadata的使用

1
ffmpeg -i /Users/redtux/Movies/Becoming.Jane.2007.1080p.BluRay.x265.2Audio.mp4  -metadata title="Becoming.Jane.2007.1080p.BluRay.x265.2Audio-RARBG" -map 0:0 -c:0 copy -metadata:s:0 title='Becoming.Jane.2007.1080p.BluRay.x265-RARBG' -metadata:s:0 handler='Becoming.Jane.2007.1080p.BluRay.x265-RARBG' -map 0:1 -c:1 copy -metadata:s:1 title="国语配音" -metadata:s:1 handler="国语配音" -metadata:s:1 language="chi"  -disposition:1 default -map 0:2 -c:2 copy -metadata:s:2 title="English" -metadata:s:2 handler="English" -metadata:s:2 language="eng"  -disposition:2  0 /Users/redtux/Movies/Becoming.Jane.2007.1080p.BluRay.x265.2Audio-RARBG.mp4

提取mp4文件中的封面

1
ffmpeg -i input.mp4 -map 0:v -map -0:V -c copy ./cover.jpg

设置mp4文件的封面

1
ffmpeg -i video.mp4 -i image.png -map 1 -map 0 -c copy -disposition:0 attached_pic out.mp4

综合实例

1
2
3
4
5
6
7
8
9
10
11
12
13
ffmpeg   -y -i  input.mp4  \
-i ~/cover.jpg \
-metadata title="[繁花].Blossoms.Shanghai.2023.S01E27.1080p.WEBRip.x265-haoduo" \
-metadata comment="[繁花].Blossoms.Shanghai.2023.S01E27.1080p.WEBRip.x265-haoduo" \
-filter_complex "[0:0]select='between(t,131.32,1106.48)+between(t,1132.92,2647.88)',setpts=N/FRAME_RATE/TB,scale=1920:-8:flags=lanczos,setsar=1:1,hqdn3d=luma_spatial=2:chroma_spatial=1.5:luma_tmp=3:chroma_tmp=2.25[v]" \
-map "[v]" -metadata:s:0 title="Blossoms.Shanghai.2023.S01E27.1080p.WEBRip.x265-haoduo" -c:0 libx265 -pix_fmt yuv420p \
-x265-params "aq-mode=3:repeat-headers=0:strong-intra-smoothing=1:bframes=4:b-adapt=2:frame-threads=0:colorprim=bt709:transfer=bt709:colormatrix=bt709:chromaloc=0" \
-crf:0 30 -preset:0 slow -tag:0 hvc1 -g 250 \
-map 0:1 -metadata:s:1 title="Mandarin" -metadata:s:1 handler="Mandarin" -metadata:s:1 language=chi \
-c:1 libfdk_aac -profile:1 aac_he -b:1 64k -af:1 "aselect='between(t,131.32,1106.48)+between(t,1132.92,2647.88)',asetpts=N/SR/TB" \
-map 1 -c:2 copy -disposition:2 attached_pic \
-movflags +faststart -map_metadata -1 -map_chapters 0 \
"/Users/redtux/Movies/Blossoms.Shanghai.2023.S01E27.1080p.WEBRip.x265-haoduo.mp4"

ffprobe命令

ffprobe是ffmpeg中一个查看多媒体文件信息的模块。此模块可以用来查看多媒体文件格式以及编码。ffprobe的命令较多,这里只简单的列举了一些比较常用的命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-L 显示协议
-h/-?/-help/--help topic 帮助可以选择话题
-version 显示版本
-buildconf 展示编译配置选项
-formats 显示支持的编码
-muxers 展示支持的封装器
-demuxers 展示支持的解封装器
-devices 展示支持的设备
-codecs 展示支持的编码
-decoders 显示支持的解码器
-encoders 显示支持的编码器
-bsfs 显示支持的比特流过滤器
-protocols 展示支持的协议
-filters 展示支持的过滤器
-pix_fmts 显示支持的像素格式
-layouts 展示支持的声道格式
-sample_fmts 显示支持的采样格式
-colors 展示支持的颜色名称
-loglevel loglevel 设置日志级别
-v loglevel 设置日志级别
-report 生成报告
-max_alloc bytes 设置单个已分配块的最大大小
-cpuflags flags 指定cpu标志
-hide_banner hide_banner 不显示程序横幅
-sources device 列出源的输出设备
-sinks device 列出输出设备的接收器
-f format 指定格式
-unit 显示显示值的单位
-prefix 对显示的值使用SI前缀
-byte_binary_prefix 对字节单位使用二进制前缀
-sexagesimal 对时间单位使用六十进制格式 HOURS:MM:SS.MICROSECONDS
-pretty 美化显示输出的值,让人可读
-print_format format 设置打印格式 (available formats are: default, compact, csv, flat, ini, json, xml)
-of format -print_format的编码
-select_streams stream_specifier 选择指定的stream
-sections 打印节结构和节信息,然后退出
-show_data 显示数据包信息
-show_data_hash 显示数据包hash值
-show_error 显示探测中的错误
-show_format 显示格式/容器信息
-show_frames 显示帧信息
-show_format_entry entry 显示格式/容器信息中的特定条目
-show_entries entry_list 显示一组指定的项
-show_log 显示log
-show_packets 显示packet信息
-show_programs 显示程序信息
-show_streams 显示stream的信息
-show_chapters 显示chapters的信息
-count_frames 每个stream中的帧数
-count_packets 每个stream中的包数量
-show_program_version ffprobe的版本
-show_library_versions 库的版本
-show_versions 程序和库的版本号
-show_pixel_formats 展示像素格式描述
-show_private_data 显示私有数据
-private 和显示私有数据一样
-bitexact 强制提取bit输出
-read_intervals read_intervals 设置读取间隔
-default 默认所有选项
-i input_file 读取指定文件
-print_filename print_file 重新显示输入的文件名
-find_stream_info 读取并解码流,用启发式方法填充缺失的信息

举个例子说明一下:

1
ffprobe -show_format test.mp4 -print_format json

输出为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"format": {
"filename": "test.mp4",
"nb_streams": 2,
"nb_programs": 0,
"format_name": "mov,mp4,m4a,3gp,3g2,mj2",
"format_long_name": "QuickTime / MOV",
"start_time": "0.000000",
"duration": "319.204000",
"size": "20393093",
"bit_rate": "511098",
"probe_score": 100,
"tags": {
"major_brand": "isom",
"minor_version": "512",
"compatible_brands": "isomiso2avc1mp41",
"artist": "张国荣",
"comment": "163 key(Don't modify):LxuL/bhXK+eaR4o7F6vxHKbEgEKNGLg65XxJgCCH5+sQLLnuMmlonrU27WIYQt6PVE6HRptXEzkJGEUUyqRRgCQJCfXHsb5uK0bdi2lw2dOZM6en1xhkYzGV5bS0uqXI0ZMFdk2CrlfK9x7cKCXIO7AU63YasnAy7YR4dW6boVAOaUvjIWxu5CuHaCLpAF8idLjG4VlAtUpZDj2WEXQJRcZZUFLtJWNVzfbnajHn6j7qG9e0XlNSmOu6RlE/X4MR",
"title": "追 ",
"encoder": "Lavf56.4.101"
}
}
}

Select滤镜常用场景

select滤镜的使用方法见官网

1.提取视频关键帧

1
ffmpeg  -i input.mp4 -an -vf select='eq(pict_type\,I)' -vsync 2  -c:v png  image-%03d.png

2.创建缩略图

1
ffmpeg -i input.mp4 -vf select='gt(scene\,0.4)',scale=-1:200,tile=4x10 -frames:v 1 preview.png

3.裁剪视频文件的多个部分,并将其合并成一个新的视频

使用选择滤镜

1
ffmpeg -i video -vf "select='between(t,4,6.5)+between(t,17,26)+between(t,74,91)',setpts=N/FRAME_RATE/TB" -af "aselect='between(t,4,6.5)+between(t,17,26)+between(t,74,91)',asetpts=N/SR/TB" out.mp4

select及其对应的滤波器分别应用于视频和音频,选择的分段是4到6.5秒,17到26秒,最后是74到91秒;时间戳与设置点及其对应的过滤器保持连续。