#3 视频编码

2021-08-07

压缩技术

  • 1929:首次出现帧间压缩
  • 1952:差分脉冲编码调制
  • 1959:使用时间压缩的帧间预测编码
  • 1967:行程长度编码
  • 20世纪70年代:早期数字视频算法
  • 1972:数字视频的首次压缩
  • 1973:DCT技术成为一种图像压缩算法
  • 1974:混合编码的发展过程
  • 1975:混合编码的进一步发展
  • 1977:更快的DCT算法
  • 1979~1981:运动补偿DCT视频压缩
  • 1984:首个数字视频压缩标准——H.120
  • 1988:用H.261举行视频会议
  • 1992:使用Motion JPEG(MJPEG)的PC多媒体应用
  • 1993:使用MPEG-1的视频CD
  • 1994:使用H.262和MPEG-2的电视广播和DVD
  • 图像采样
  • I帧,P帧和B帧
  • 1995:使用DV存储数字视频
  • 1996:使用 H.263 的新一代视频会议标准
  • 1999:使用 MPEG-4 第二部分的互联网视频
  • 2003:使用 H.264/MPEG-4 AVC 的蓝光、DVD、视频直播和广播电视
  • 2013:使用 H.265/HEVC 的 360° 沉浸式视频、AR和VR
  • 2013:谷歌 VP9 (H.265 的竞争对手)
  • 2018:使用 AV1 的高质量网络视频
  • 2020:使用 H.266/VVC 实现商业上可行的 4K、8K
  • 我国拥有自主知识产权的第三代 “信源编码” 标准——AVS3

编码格式

或者说编解码技术,可能一种技术编码很复杂,但是解码简单。

  • 国际电联的 H.261、H.263、H.264
  • 运动静止图像专家组 (M-JPEG)
  • ISO 运动图像专家组 (Moving Picture Experts Group, MPEG)
  • Real-Networks
  • RM (Real Media)
  • RMVB, 相比较于 RM,支持非固定压缩比,在画面变化小时,更好的压缩文件。
  • 微软公司
  • WMV
  • AVI(Audio Video Interleaved,音频视频交错)
  • ASF(Advanced Streaming Format,高级流格式)
  • 苹果公司
  • QuickTime, MOV 格式
  • 北大和国内企业共同推进的 AVS 系列标准

封装格式

  • 元数据
  • 视频数据
  • 音频数据
  • 字幕数据
  • 可能还有一些版权保护方面的技术。

有一些标准既包括编码格式,也把包括封装格式,如 rm,rmvb,wmv

有一些封装格式可以容纳不同类型的数据,如 mkv

  • 3GP(第三代合作伙伴项目):国际电信行业共同推出,MPEG-4 Part 14(MP4)简化版本,流媒体,常用于移动设备,支持自适应速率。过去很常见,似乎现在没见过了。
  • flv(Flash Video): 采用 H.263 编码,相同质量的前提下,文件更小,资源消耗更少
  • f4v: 采用 H.264 编码

参考资料与拓展阅读

#2 Linux 工具箱: exiftool

2021-05-28
# 查看 Exif 信息:
exiftool      media/images/django.jpg
exiftool -X   media/images/django.jpg  # XML 格式
exiftool -csv media/images/django.jpg  # CSV 格式

exiftool    media/images/
exiftool -r media/images/  # 递归遍历子目录

# 清除文件 Exif 信息:
exiftool -all= -overwrite_original media/images/django.jpg
exiftool -all= -overwrite_original media/images/
exiftool -all= -overwrite_original -ext png media/images/

# 清除指定 Exif 信息
exiftool -gps:all= *.jpg

#1 Linux 工具箱 2:convert (图像处理)

2021-04-24

关于 convert 的文章,之前已经写过两篇:

安装 ImageMagick

sudo apt install imagemagick

convert -list type
convert -list font # 支持的字体

获取图片信息

identify markjour.png
identify -verbose markjour.png
identify -format "Size: %w x %h\n" markjour.png

# Exif 信息
identify -format '%[Exif:*]' ~/Pictures/Photos/2019-09-14-18-48-22.jpg

# sudo apt install exif
exif ~/Pictures/Photos/2019-09-14-18-48-22.jpg

清除所有 Exif 信息

convert -strip input.jpg out.jpg
convert +profile "*" input.jpg out.jpg

图片切割

<宽> x <高> + <X轴坐标> + <Y轴坐标>

  1. 如果没有指定坐标,那就是切割图片。
  2. 宽高可以是百分比(不能混用,只要一个百分号那就都是比例)。
convert -crop 300x400+10+10 src.jpg dest.jpg

# 指定中心位址为基准点:
convert -gravity center -crop 300x400+0+0 src.jpg dest.jpg

convert -crop 25%x100% src.jpg dest.jpg

图片合并

之前(convert 图片转换的一次示例)合并图片用的就是这个命令。

# 横向
convert +append markjour-l.jpg markjour-c.jpg markjour-r.jpg markjour.jpg
# 纵向
convert -append markjour-t.jpg markjour-c.jpg markjour-b.jpg markjour.jpg

图片旋转

convert -rotate 90 input.jpg output.jpg  # 顺时针
convert -rotate -90 input.jpg output.jpg # 逆时针

# 左右反转,镜像效果
convert -flop input.jpg output.jpg

# 上下反转,这个和旋转 270 效果还是不一样的
convert -flip input.jpg output.jpg

图片缩放

# 限宽我很常用,控制页面图片尺寸
convert -resize 108x markjour.png markjour-108.png

convert -sample 50%  markjour.png markjour-new.png
convert -sample 200% markjour.png markjour-big.png

PS: 放大时 resize 会自动采样插值,而 sample 不会

图片压缩

convert input.jpg -quality 50 output.jpg

颜色

# 灰度,就是常见的黑白照片效果
convert -colorspace gray input.jpg output.jpg

# 分离 RGB 三个通道,输出三张图片,不知道为什么都是灰色
convert -separate input.png output.png

convert -threshold 40% input.png output.png # 也是一种常见效果,不知道叫什么
convert -negate input.png output.png # 反色

# 黑白(非灰度)sRGB -> Gray 2c
convert -monochrome input.png output.png

# 重新转成 sRGB 模式(但是颜色还是留在黑白两色)
convert -define png:color-type=2 input.png output.png
convert -colorspace sRGB -type truecolor input.jpg output.jpg

# 效果很奇特,可以试试:
convert -remap pattern:gray60 input.png output.png

# 替换
convert -fuzz 15% -fill white -opaque "rgb(143,141,250)" -opaque "rgb(216,217,62)" input.png output.png

滤镜

convert -blur 70 input.png output.png
# 后面的数字对模糊程度有着决定性作用
convert -blur 70x15 input.png output.png

convert -charcoal 2  input.png output.png # 铅笔画风格
convert -noise    3  input.png output.png
convert -paint    4  input.png output.png # 油画风格
convert -spread   50 input.png output.png # 毛玻璃
convert -swirl    60 input.png output.png # 扭曲

convert -paint 4 -raise 5x5 input.png output.png

# 调整透明度
# 先确保图片有 Alpha 通道
convert -define png:format=png32 input.png output.png
convert -channel alpha -fx "0.5" output.png output2.png

边框

# 加边框
convert -mattecolor "#000" -frame 60x60 input.png output.png
convert -mattecolor "#fff" -frame 60x60 input.png output.png

# 相同效果
convert -bordercolor "#fff" -border 60x60 input.png output.png

# 再配合上 raise:
convert -bordercolor "#fff" -border 10x10 input.png output.png
convert -raise 5x5 output.png output2.png

# 去掉边框:
convert -trim -fuzz 10% input.png output.png

水印

convert -fill "#1770CC" \
-font Ubuntu-Mono -pointsize 50 -draw 'text 130,50 "©"' \
-font 楷体_GB2312 -pointsize 40 -draw 'text 50,50 "码厩"' \
-gravity southeast \
input.png output.png

# 改用 RGBA 模式
convert -fill "rgba(23,112,204,0.25)" \
-font Ubuntu-Mono -pointsize 50 -draw 'text 130,50 "©"' \
-font 楷体_GB2312 -pointsize 40 -draw 'text 50,50 "码厩"' \
-gravity southeast \
input.png output.png

# 这个不错,京东那里学来的:
convert -size 100x100 -fill "#1770CC" -gravity center \
-font Ubuntu -pointsize 30 -draw 'rotate -45 text 0,0 "markjour"' \
xc:none miff:- | composite -tile -dissolve 25 - input.png output.png

# 图片水印
convert -size 150x50 -fill "#1770CC" -gravity center \
-font Ubuntu -pointsize 30 -draw 'text 0,0 "markjour"' \
xc:none /tmp/mark.png
convert input.png -gravity southeast -compose over /tmp/mark.png -composite output.png

其他

# 查看图片
# GNOME 桌面好像都是 eog
eog markjour.png

# 或者使用 ImageMagick 自带图片查看工具:
display markjour.png

# 查看颜色信息
convert xc:"#fff" -colorspace sRGB -format "%[pixel:u.p{0,0}]\n" txt:
convert xc:"#fff" -colorspace sRGB -format "%[pixel:u.p{0,0}]\n" info:

convert xc:"#fff" -colorspace sRGB -format "rgb(%[fx:int(255*r)],%[fx:int(255*g)],%[fx:int(255*b)])\n" info:

# 获取指定像素点的颜色(RGB)
convert "input.png[1x1+100+100]" -format "rgb(%[fx:int(255*r)],%[fx:int(255*g)],%[fx:int(255*b)])\n" info:

# 创建一张新图片
convert -size 1000x1000 xc:none /tmp/image.png
convert -size 1000x1000 xc:transparent /tmp/image.png
convert -size 1000x1000 xc:white /tmp/image.png

webp

sudo apt install -y webp
  • cwebp 转成 WEBP 格式
  • dwebp 转成别的格式
cwebp -o xxx.png xxx.webp
dwebp -o xxx.webp xxx.png

参考资料与拓展阅读