关于树莓派屏幕配置文件的说明(记录一下)

There are two modes that the Raspberry Pi supports. The two modes are abbreviated as:

  • CEA: Consumer Electronics Association
  • DMT: Display Monitor Timings

The CEA mode is the standard mode for displays such as TVs. The DMT mode is the standard mode of computer monitors. Choosing between the modes can be done through the tvservice commands on the Linux terminal. The commands to indicate the available modes for the display are as follows:

/opt/vc/bin/tvservice -m CEA

/opt/vc/bin/tvservice -m DMT

To check the resolution, mode and settings that are currently in use enter the tvservice command:

/opt/vc/bin/tvservice -s

An export log is available of the processes the Raspberry Pi goes through in determining the resolution of the connected display. This can be done through terminal commands that show the EDID (Extended Display Identification Data) file of the determined display. The EDID data file is a more detailed resource for finding a display resolution compatible with the connected display. The following command will show the EDID file.

/opt/vc/bin/tvservice -d edid.dat;

/opt/vc/bin/edidparser edid.dat

The default firmware will use the EDID parser to try and identify the HDMI connected display and then will select the corresponding video mode and resolution. Through this command you will see the selection process of the parser and the alternative available modes. You will need to use a virtual connection to view the Linux kernel command line or have a view of the terminal.

Choose a mode that is the closest to the screen resolution specified on your displays data sheet. The tvservice command will offer the available resolutions for the display and the corresponding refresh rate. A previous application note further explains how to set the DMT mode resolution. In a similar process the CEA mode can be set to define the display resolution. This is done by adding the following settings in the config.txt file located in the boot file of the Raspberry Pi. The following example would be for an 800x480 resolution display (E70RC-FW850-R).

hdmi_group=1

hdmi_mode=65 #65 is for custom modes

hdmi_cvt=800 480 60 6 1 0 0                                                    

The hdmi_cvt parameter requires the following information about the display.

hdmi_cvt=

      width: width of display in pixels

    height: height of display in pixels

    FR: frame rate in Hz

    aspect: aspect ratio

    (1: 4/3, 2: 14/9, 3: 16/9, 4: 5/4, 5: 16/10, 6: 15/9)

    margins: 0=margins disabled, 1=margins enabled

    interlace: 0=progressive, 1=interlaced

    rb: 0=normal, 1=reduced blanking

After the config.txt file is edited and saved, the Raspberry Pi can be rebooted into the new configuration mode and should display in the correct dimensions. If the display is not recognized after these changes have been made there are a few additional changes to consider. The first would be to boost the HDMI signal through the following command. Certain HDMI cables can occasionally be unreliable when the Linux kernel is identifying the display mode.

config_hdmi_boost=11 

In some cases, the custom HDMI mode “hdmi_cvt” function does not work for the first group of CEA displays. To customize the parameters to fit the display you will pick the closest CEA mode and alter the overscan and framebuffer values. For example, the 480x800 display has a default CEA mode closest to mode 3 which has a resolution of 480x853. This mode can be chosen and adjusted using the overscan parameters to add or remove pixels from the edges to fit the display.

hdmi_group=1                    #CEA group 1

hdmi_mode=3                     #480x853

disable_overscan=1              #remove default overscan values

overscan_right=26               #remove overflow pixels right

overscan_left=26                #remove overflow pixels left

The negative overscan parameters that are used to remove black borders will often not affect the display when using the HDMI interface. To remove the black borders around the edges of the display you can increase the frame buffer parameters in the config.txt file.

framebuffer_width=480

framebuffer_height=800

This will force the Raspberry Pi to define a frame buffer and resolution of 480x800. The true frame buffer value that the Raspberry Pi uses is the resolution combined with the defined or undefined overscan values. You can increase the framebuffer width and height values to force a negative overscan value which will remove the black borders around the display.

在 Raspberry Pi 上使用 HDMI-CEC

什么是HDMI-CEC

HDMI-CEC 是一种特殊协议,专为电视通过 HDMI 电缆与其他设备进行通信而设计。该协议允许电视控制另一个设备,同时还允许该设备控制电视。

大多数现代电视都支持此协议,但通常必须在电视设置中启用。搜索您的电视型号应该可以帮助您找到此设置的名称。

使用 HDMI-CEC 协议,您可以使用 Raspberry Pi 以各种不同的方式控制电视,例如关闭和打开电视或更改音量。

将 cec-client 安装到 Raspberry Pi

cec-client 是我们将在 Raspberry Pi 上使用的软件包,用于通过 HDMI-CEC 协议控制设备。

软件包安装

1.我们的首要任务是更新 Raspberry Pi 上的软件包列表以及升级当前安装的软件包。

我们可以通过运行以下两个命令来完成此任务。

sudo apt update 
sudo apt -y upgrade 
sudo apt install cec-utils -y

软件的使用方法

  1. 使用 Raspberry Pi 通过 HDMI-CEC 与电视交互

第一个是使用echo和管道(|)。将命令直接传递给cec-client软件,而无需启动它。

此外,我们在软件中同时使用-s和-d选项cec-client。

该-s选项告诉软件我们将发出一个命令。软件将启动,发出命令然后立即退出。

该-d 1选项设置软件的调试级别。通过将其设置为1,cec-client软件将只显示错误。

从 Raspberry Pi 扫描 HDMI-CEC 设备

echo 'scan' | cec-client -s -d 1

2.通过此命令,您应该会看到您的 Raspberry Pi 现在可以访问的设备列表。

您需要识别要与之交互的设备。通常,“ os string:”和“ vendor:”字段将帮助您识别要与之交互的设备。

识别出正确的设备后,记下“ address:”或设备编号。

输出有可能类似下面的内容:

opening a connection to the CEC adapter...
requesting CEC bus information ...
CEC bus information
===================
device #0: TV
address:       0.0.0.0
active source: no
vendor:        Sony
osd string:    TV
CEC version:   1.4
power status:  standby
language:      eng

device #1: Recorder 1
address:       1.0.0.0
active source: no
vendor:        Pulse Eight
osd string:    CECTester
CEC version:   1.4
power status:  on
language:      eng

device #4: Playback 1
address:       3.0.0.0
active source: no
vendor:        Sony
osd string:    PlayStation 4
CEC version:   1.3a
power status:  standby
language:      ???

请注意,device #1: Recorder 1此示例中的“ ”是 Raspberry Pi 自己的 CEC 连接,因此我们可以放心地忽略它。

3.例如,如果我们想控制我们的“索尼电视”,我们可以看到设备号是“ 0”,设备的地址是“ 0.0.0.0”。

获得设备编号或设备地址后,您就可以开始向其发送命令了。

通过 HDMI-CEC发送“ on ”命令

echo 'on <DEVICEADDRESS>' | cec-client -s -d 1

通过 HDMI-CEC发送“待机”命令

echo 'standby <DEVICEADDRESS>' | cec-client -s -d 1

通过 HDMI-CEC关闭设备的示例

使用此命令相对简单。

要将我们的索尼电视置于待机状态,我们需要做的就是发送“ standby”,然后发送我们的设备地址“ 0.0.0.0”。

echo 'standby 0.0.0.0 | cec-client -s -d 1

通过 HDMI-CEC 获取电源状态

echo 'pow <DEVICEADDRESS>' | cec-client -s -d 1

通过 HDMI-CEC 获取电源状态的示例

echo 'pow 0.0.0.0' | cec-client -s -d 1

如果您的设备处于待机状态,您将在终端中看到类似于我们下面的内容。
opening a connection to the CEC adapter...
power status: standby

检索其他 CEC 客户端命令

echo 'h' | cec-client -s -d 1

该命令的作用是检索cec-client软件知道如何处理的可用命令。

从这个命令中,你应该得到一个命令列表,如下所示。

================================================================================
Available commands:

[tx] {bytes}              transfer bytes over the CEC line.
[txn] {bytes}             transfer bytes but don't wait for transmission ACK.
[on] {address}            power on the device with the given logical address.
[standby] {address}       put the device with the given address in standby mode.
[la] {logical address}    change the logical address of the CEC adapter.
[p] {device} {port}       change the HDMI port number of the CEC adapter.
[pa] {physical address}   change the physical address of the CEC adapter.
[as]                      make the CEC adapter the active source.
[is]                      mark the CEC adapter as inactive source.
[osd] {addr} {string}     set OSD message on the specified device.
[ver] {addr}              get the CEC version of the specified device.
[ven] {addr}              get the vendor ID of the specified device.
[lang] {addr}             get the menu language of the specified device.
[pow] {addr}              get the power status of the specified device.
[name] {addr}             get the OSD name of the specified device.
[poll] {addr}             poll the specified device.
[lad]                     lists active devices on the bus
[ad] {addr}               checks whether the specified device is active.
[at] {type}               checks whether the specified device type is active.
[sp] {addr}               makes the specified physical address active.
[spl] {addr}              makes the specified logical address active.
[volup]                   send a volume up command to the amp if present
[voldown]                 send a volume down command to the amp if present
[mute]                    send a mute/unmute command to the amp if present
[self]                    show the list of addresses controlled by libCEC
[scan]                    scan the CEC bus and display device info
[mon] {1|0}               enable or disable CEC bus monitoring.
[log] {1 - 31}            change the log level. see cectypes.h for values.
[ping]                    send a ping command to the CEC adapter.
[bl]                      to let the adapter enter the bootloader, to upgrade
                          the flash rom.
[r]                       reconnect to the CEC adapter.
[h] or [help]             show this help.
[q] or [quit]             to quit the CEC test client and switch off all
                          connected CEC devices.
=======================================================================

至此,您现在应该已经学会了如何cec-client在您的 Raspberry Pi 上使用来控制支持该HDMI-CEC协议的设备。

防止树莓派自动黑屏

前言

树莓派在运行桌面环境的时候,长时间不操作就会自动黑屏,是电源管理的一个节能的功能,有时候挺烦人,可以关闭它。

暂时关闭

在终端中输入:

sudo xset s off
sudo xset -dpms
sudo xset s noblank

永久关闭

  1. 通过sudo raspi-config 命令
    找到Display Options 然后选择Screen Blanking, 问是否启用,选择NO, 然后完成后重启树莓派。
  2. 通过编辑/etc/lightdm/lightdm.conf
    将xserver-command=X 前面的#去掉,然后改成:

    xserver-command=X -s 0 -dpms

    保存后退出重启系统。

    总结

    屡试不爽!

[Raspberry Pi 新系统]树莓派更新失败处理方法

故障现象:

pi@raspberrypi:~ $ sudo apt-get update
Get:1 http://archive.raspberrypi.org/debian buster InRelease [32.6 kB]
Get:2 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB]
Reading package lists... Done
E: Repository 'http://raspbian.raspberrypi.org/raspbian buster InRelease' changed its 'Suite' value from 'stable' to 'oldstable'
N: This must be accepted explicitly before updates for this repository can be applied. See apt-secure(8) manpage for details.
E: Repository 'http://archive.raspberrypi.org/debian buster InRelease' changed its 'Suite' value from 'testing' to 'oldstable'
N: This must be accepted explicitly before updates for this repository can be applied. See apt-secure(8) manpage for details.

修复方法:

sudo apt-get -y update --allow-releaseinfo-change

结果如下:

Get:1 http://archive.raspberrypi.org/debian buster InRelease [32.6 kB]
Get:2 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB]
Get:3 http://raspbian.raspberrypi.org/raspbian buster/contrib Sources [78.5 kB]
Get:4 http://archive.raspberrypi.org/debian buster/main armhf Packages [393 kB]
Get:5 http://raspbian.raspberrypi.org/raspbian buster/main Sources [11.4 MB]
Ign:5 http://raspbian.raspberrypi.org/raspbian buster/main Sources
Get:6 http://raspbian.raspberrypi.org/raspbian buster/non-free Sources [139 kB]
Get:7 http://raspbian.raspberrypi.org/raspbian buster/rpi Sources [1,132 B]
Get:8 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages [13.0 MB]
21% [8 Packages 443 kB/13.0 MB 3%]

顺利解决~

浪派-造物志

何谓:浪派?

所谓浪派,其实是我和几个朋友一起搞的一个树莓派小车的项目,其核心内容是想在疫情期间无法面基就只好通过网络交互来实现原先的自由自造的折腾行为.其中涉及到原型设计,芯片选型,PCB设计,打样焊接,程序调试等一系列技术栈,在设计之初为了能够让大家都有参与进来的兴趣,还专门采用了kicad设计软件来进行pcb设计...参与的小伙伴各显神通,但是也遭遇过一些小小的滑铁卢...

回忆杀


未完待续,手机更新大拇指压力太大了....

Retropie 设置中文

RetroPie复古游戏模拟系统设置中文

步骤简述

    1. 下载最新的Retropie镜像,别问我从哪里下载。
    1. 烧录到TF卡,也别问我怎么烧录。
    1. 开机启动后连wifi或者网络, 更别问我怎么连wifi。
    1. 进系统后直接按F4进入终端,在确保联网的情况下输入:
      sudo apt-get update 
      sudo apt-get -y install fonts-droid-fallback
      sudo reboot 

      然后你安装的游戏就算是有中文也没有问题了。

树莓派4B跑个WS2812灯带

WS2812灯带在树莓派4B上的变化

今天尝试了一下,好多代码都变化了。
现在用的基本上就是要将树莓派的RPi.GPIO库都丢了。

sudo apt purge -y RPi.GPIO
sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel
sudo python3 -m pip install --force-reinstall adafruit-blinka

然后就安装这两个库够了。
灯带的5V接树莓派5V,当然,如果灯的数量多,建议额外供电,我就8个灯的灯带,够用。
GND接树莓派GND。
信号口D+接到树莓派物理接口12号,BCM的18号口上。
然后就可以写代码测试了。

import board
from time import sleep
import neopixel

pixels = neopixel.NeoPixel(board.D18, 8)

pixels.fill((0,255,0))

pixels[0] = (255,0,0)

while True:
    for x in range(0, 8):
        pixels[x] = (255, 0, 0)
        sleep(0.1)
        pixels.fill((0,255,0))

    for x in range(0, 8):
        pixels[x] = (255, 0, 0)
        sleep(0.1)
        pixels.fill((0,255,255))

    for x in range(0, 8):
        pixels[x] = (255, 0, 0)
        sleep(0.1)
        pixels.fill((0,0,255))

    for x in range(0, 8):
        pixels[x] = (0, 255, 0)
        sleep(0.1)
        pixels.fill((255,255,127))

    for x in range(0, 8):
        pixels[x] = (0, 255, 0)
        sleep(0.1)
        pixels.fill((255,255,0))

代码非常简单,不屑注释了。

import board
from time import sleep
import neopixel

pixels = neopixel.NeoPixel(board.D18, 8)

pixels.fill((0,255,0))

这段代码是初始化的部分。
运行以下代码以导入必要的模块,并初始化带有8个LED的NeoPixel灯带,如果你的NeoPixels连接到其他引脚,别忘了更改引脚,如果灯的数量不一样,也要改。
实例化后我用了fill方法来做操作了,有三个方法如下:

  • brightness - The overall brightness of the LED
  • fill - Color all pixels a given color.
  • show - Update the LED colors if auto_write is set to False.
    一般brightness是调亮度,fill就是填充颜色,show更新颜色。

    pixels[0] = (255,0,0)

    file
    就是点亮第一个灯,颜色为红色,(255,0,0) 就是 (R,G,B)三色的定义,每个灯都可以由3种颜色组合成N多种颜色,自己配置。

    pixels.fill((0, 255, 0))

    就是全部填充绿色。
    file
    就这么简单,其他自己琢磨吧。
    哦,最后给一张特写图:
    file
    file



    嗯,就这样。开心去浪吧!

树莓派串口编程

树莓派串口编程

简单做个笔记记录一下串口编程的方法。

  1. 树莓派需要释放蓝牙所占用的的串口。
  2. 编辑/boot/config.txt 或者通过raspi-config启用串口(serial) ,编辑文件的话直接加入enable_uart=1 然后重启。
  3. 添加 dtoverlay = pi3-miniuart-bt 或者:dtoverlay = pi3-disable-bt

    基本思路

    操作示例代码

    • C 语言
      #include <stdio.h>
      #include <string.h>
      #include <errno.h>
      #include <wiringPi.h>
      #include <wiringSerial.h>
      int main ()
      {
      int serial_port ;
      char dat;
      if ((serial_port = serialOpen ("/dev/ttyS0", 9600)) < 0) /* open serial port */
      {
      fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ;
      return 1 ;
      }
      if (wiringPiSetup () == -1)                   /* initializes wiringPi setup */
      {
      fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno)) ;
      return 1 ;
      }
      while(1){   
      if(serialDataAvail (serial_port) )
      { 
      dat = serialGetchar (serial_port);      /* receive character serially*/ 
      printf ("%c", dat) ;
      fflush (stdout) ;
      serialPutchar(serial_port, dat);        /* transmit character serially on port */
        }
      }
      }

      编译测试

      gcc -o serialtest -lwiringPi serialtest.c 
      ./serialtest

      结果:

树莓派超声波测距

树莓派超声波测距

超声波传感器

就是传说中的Ultrasonic传感器,学名:HC-SR04 , 某宝上非常cheap, 自己搜.

测距原理

  • 超声波距离传感器设计为使用超声波来测量源与目标之间的距离。
  • 我们之所以使用超声波,是因为超声波在短距离内相对准确,并且不会引起人耳无法听到的干扰。

特性说明

HC-SR04是用于2cm至400cm距离非接触距离测量的常用模块。
它使用声纳(如蝙蝠和海豚)以高精度和稳定的读数来测量距离。
它由超声波发射器,接收器和控制电路组成。
发射器发射短脉冲,该短脉冲被目标反射并被接收器拾取。
计算超声波信号的发送和接收之间的时间差。
使用声速和“ 速度=距离/时间 ”等式,可以轻松计算源与目标之间的距离。

引脚说明

HC-SR04超声波距离传感器模块具有四个引脚:

  • VCC – 5V,输入电源

  • TRIG –触发输入

  • 回声 –回声输出

  • GND –接地

    实际工作原理

  • 1.向TRIG输入提供触发信号,它需要至少10μS持续时间的高电平信号。

  • 2.这使模块能够发送八个40KHz超声波脉冲串。

  • 3.如果模块前面有障碍物,它将反射那些超声波

  • 4.如果信号返回,则模块的ECHO输出将在发送和接收超声信号所花费的时间内保持为高电平。脉冲宽度的范围为150μS至25mS,具体取决于障碍物与传感器之间的距离,如果没有障碍物,则脉冲宽度约为38ms。

    接线方式

    非常关键的分压步骤

    ECHO输出为5v而Raspberry Pi GPIO的输入引脚的额定电压为3.3V,因此,无法将5V直接提供给不受保护的3.3V输入引脚,除非你想把树莓派干掉...
    因此,我们得使用分压电路,使用适当的电阻将电压降至3.3V。

    以下公式可用于计算电阻值,
    “ Vout = Vin x R2 /(R1 + R2)”
    这里我用的是常用的4.7K和10K电阻做的分压电路,中间抽头给树莓派GPIO引脚,安全可靠。

    连接实物模拟图

    画图软件是frizing, 不要再私信问我了...

    计算方法

  • 距离计算
    脉冲所花费的时间实际上是超声波信号往返的时间,而我们只需要一半的时间。因此,时间被视为时间/ 2。

  • 距离=速度时间/ 2
    而海平面声速= 343 m / s或34300 cm / s
    因此,距离= 17150
    时间(单位厘米)

*校准
为了获得准确的距离读数,可以使用尺子校准输出。在下面的程序中,添加了0.5 cm的校准。
你如果不想校准也可以尝试不加,就是大致有点儿偏移量。

测试代码:

import RPi.GPIO as GPIO                    #Import GPIO library
import time                                #Import time library
GPIO.setmode(GPIO.BCM)                     #Set GPIO pin numbering 

TRIG = 23                                  #Associate pin 23 to TRIG
ECHO = 24                                  #Associate pin 24 to ECHO

print "Distance measurement in progress"

GPIO.setup(TRIG,GPIO.OUT)                  #Set pin as GPIO out
GPIO.setup(ECHO,GPIO.IN)                   #Set pin as GPIO in

while True:

  GPIO.output(TRIG, False)                 #Set TRIG as LOW
  print "Waitng For Sensor To Settle"
  time.sleep(2)                            #Delay of 2 seconds

  GPIO.output(TRIG, True)                  #Set TRIG as HIGH
  time.sleep(0.00001)                      #Delay of 0.00001 seconds
  GPIO.output(TRIG, False)                 #Set TRIG as LOW

  while GPIO.input(ECHO)==0:               #Check whether the ECHO is LOW
    pulse_start = time.time()              #Saves the last known time of LOW pulse

  while GPIO.input(ECHO)==1:               #Check whether the ECHO is HIGH
    pulse_end = time.time()                #Saves the last known time of HIGH pulse 

  pulse_duration = pulse_end - pulse_start #Get pulse duration to a variable

  distance = pulse_duration * 17150        #Multiply pulse duration by 17150 to get distance
  distance = round(distance, 2)            #Round to two decimal points

  if distance > 2 and distance < 400:      #Check whether the distance is within range
    print "Distance:",distance - 0.5,"cm"  #Print distance with 0.5 cm calibration
  else:
    print "Out Of Range"                   #display out of range

测试

python3 ultrasonic_distance_detect.py

效果很明显。

总结

反射角度如果大于15度有可能会出现值不准,如果附近噪声很大也许会有干扰,其他的没啥,很简单的一个模块,可以结合树莓派或者别的设备做一个超声波控制音量的玩意儿,树莓派爬音乐网站,播放音乐,由超声波传感器来进行检测,距离小于多少就降低音量,否则就开大音量, 自己想办法玩儿吧! 转载记得标识出处!
谢谢观看,我是骑驴玩儿漂移,甩你三条街~ 白了个白~

Live555直播

树莓派上用 Live555 直播摄像头数据

最近在群里听到有朋友推荐使用 live555 来在树莓派上进行rtsp 服务器的搭建,非常简单做个记录防忘记.
Live555是一个开源的 C++源码编写的流媒体推流软件合集,功能好多,还咩有玩儿透彻,但是你也看得懂源码的.非常激动人心.

软件包下载

链接: http://www.live555.com/liveMedia/public/

通过命令解压并执行 makefile 的生成.

tar -xf live555-latest.tar.gz
cd live/
./genMakefiles linux

编辑当前目录下面的live/testProgs/testOnDemandRTSPServer.cpp

修改这部分内容:

树莓派上创建一个 fifo 文件,先进先出管道.

mkfifo /tmp/rpicam

然后编译并安装.

make 
sudo make install

进入 testProgs 目录执行二进制文件.

./testOnDemandRTSPServer &

树莓派开启摄像头并执行:

raspivid -rot 180 -o /tmp/rpicam -f -t 0

VLC 测试或者 opencv 测试:

  1. vlc 在打开网络流的URL 里面输入设备流地址: rtsp://192.168.3.20:8554/liv0 就可以观看了.
  2. 使用 opencv 打开,自己创建一个文件:
    如图,截图中缩进层级有点儿变化,主要看后面的代码.
    图为:

    代码走一波

    # get streaming video via opencv
    import cv2
    import numpy as np
    cap = cv2.VideoCapture("rtsp://192.168.3.20:8554/liv0")
    try:
    while True:
        ret, frame = cap.read()
        if ret == True:
            smallFrame = cv2.resize(frame, (480, 240))
    
            key = cv2.waitKey(1)
            if key == 27:
                break
            cv2.imshow("small size Frame", smallFrame)
    except KeyboardInterrupt:
    print("quit")
    cap.release()
    cv2.destroyAllWindows()

    执行查看:

    python3 streaming_get_from_opencv.py

    效果如图:

    好了,还有一个我在蘑菇云参加一个 piday4fun 的活动地址在: https://github.com/piday4fun

    还楞啥,燥起来吧!!!

    转发记得标明出处, 否则小心菊花万人捅~

ffmpeg小技巧总结

ffmpeg强大的爆,直接记录一些使用小技巧。

视频转换

比如avi文件想转mp4,或者mp4想转为ts。

ffmpeg -i test.avi output.mp4
ffmpeg -i test.mp4 output.ts

如果想指定解码方式,比如h265可以使用libx264

ffmpeg -i test.avi -vcodec libx264 output.mp4

直接获取网络上的视频,自动下载并且转码并保存,该方式需要带上-y,按顺序读取

ffmpeg -i "http://www.yoyojacky.com/video/test.avi" -y -vcodec libx264 output.mp4

分离并提取音频存为AAC格式

ffmpeg -i test.mp4 -acodec copy -vn output.aac

默认mp4的audio codec是aac,如果不是,可以都转为最常见的aac,苹果手机支持,想做铃声的自己可以抓视频然后采集声音。

ffmpeg -i test.mp4 -acodec aac -vn output.aac

提取视频

ffmpeg -i test.mp4 -vcodec copy -an output.mp4

视频截图

ffmpeg -i test.mp4 -r 1 -q:v 2 -f image2 snapshot.jpg

-r 表示每一秒几帧
-q:v表示存储jpg的图像质量,一般2是高质量。
这条命令ffmpeg会把test.mp4,每隔一秒,存一张图片下来。假设有60s,那会有60张。
如果你想,也可以设置开始的时间,和你想要截取的时间。

ffmpeg -i test.mp4 -ss 00:00:30 -t 20 -r 1 -q:v 2 -f image2 pic-1.jpg

-ss 表示开始时间
-t 表示共要多少时间。
以上的命令,ffmpeg会从test.mp4的第30s时间开始,往下20s,即30~50s这20秒钟之间,每隔1s就抓一帧,总共会抓20帧。

视频剪切

下面的命令,可以从时间为00:00:45开始,截取10秒钟的视频。

ffmpeg -ss 00:00:45 -t 00:00:10 -i test.mp4 -vcodec copy -acodec copy output.mp4

-ss表示开始切割的时间,-t表示要切多少。上面就是从45秒开始,截取10秒钟的视频。

码率控制

码率控制对于在线视频比较重要,因为在线视频需要考虑带宽。
那么什么是码率?so easy:
bitrate = file size / duration
比如一个文件20.8M,时长1分钟,那么,码率就是:
biterate = 20.8M bit/60s = 20.810241024*8 bit/60s= 2831Kbps
一般音频的码率只有固定几种,比如是128Kbps,
那么,video的就是
video biterate = 2831Kbps -128Kbps = 2703Kbps。

 那么ffmpeg如何控制码率?

ffmpg控制码率有3种选择:-minrate -b:v -maxrate
-b:v主要是控制平均码率。
比如一个视频源的码率太高了,有10Mbps,文件太大,想把文件弄小一点,但是又不破坏分辨率。

ffmpeg -i test.mp4 -b:v 2000k output.mp4

上面把码率从原码率转成2Mbps码率,这样其实也间接让文件变小了,目测接近一半。
不过,ffmpeg官方wiki比较建议,设置b:v时,同时加上 -bufsize
-bufsize 用于设置码率控制缓冲器的大小,设置的好处是,让整体的码率更趋近于希望的值,减少波动。(简单来说,比如1 2的平均值是1.5, 1.49 1.51 也是1.5, 当然是第二种比较好)

ffmpeg -i test.mp4 -b:v 2000k -bufsize 2000k output.mp4

-minrate -maxrate就简单了,在线视频有时候,希望码率波动不要超过一个阈值,可以设置maxrate。

ffmpeg -i test.mp4 -b:v 2000k -bufsize 2000k -maxrate 2500k output.mp4

视频编码格式转换

比如一个视频的编码是MPEG4,想用H264编码,咋办?

ffmpeg -i test.mp4 -vcodec h264 output.mp4

相反也一样

ffmpeg -i test.mp4 -vcodec mpeg4 output.mp4

当然了,如果ffmpeg当时编译时,添加了外部的x265或者X264,那也可以用外部的编码器来编码。(不知道什么是X265,可以 Google一下,简单的说,就是她不包含在ffmpeg的源码里,是独立的一个开源代码,用于编码HEVC,ffmpeg编码时可以调用它。当然 了,ffmpeg自己也有编码器)

ffmpeg -i test.mp4 -c:v libx265 output.mp4 
ffmpeg -i test.mp4 -c:v libx264 output.mp4

将视频进行m3u8切片

ffmpeg -i 本地视频.ts -c copy -map 0 -f segment -segment_list 视频索引.m3u8 -segment_time 5 前缀-%03d.ts

其中segment 就是切片,-segment_time表示隔几秒进行切一个文件,上面命令是隔5s,你也可以调整成更大的参数。
上面是网上抄的,下面是我经常用的:

ffmpeg -i test.mp4 -c copy hls_list_size 0 output.m3u8 hls_time 5

上面的hls_list_size表示目录文件中需要播放的文件数量,0表示整个视频,hls_time表示每个切片的长度,上面设置的5秒

只提取视频ES数据

ffmpeg –i test.mp4 –vcodec copy –an –f m4v output.h264

过滤器的使用

动态设置视频分辨率

将输入的1920x1080缩小到800:600输出:

ffmpeg -i test.mp4 -vf scale=800:600 output.mp4

ps: 如果600不写,写成-1,即scale=800:-1, 那也是可以的,ffmpeg会通知缩放滤镜在输出时保持原始的宽高比。

ffmpeg -i input.mp4 -vf scale=800:-1 output.mp4

为视频添加logo

比如我有一个图片想要贴到一个视频上:

ffmpeg -i test.mp4 -i logo.png -filter_complex overlay output.mp4
####要贴到其他地方?看下面: 
右上角: 
```bash
ffmpeg -i test.mp4 -i logo.png -filter_complex overlay=W-w output.mp4

左下角:

ffmpeg -i test.mp4 -i logo.png -filter_complex overlay=0:H-h output.mp4

右下角:

ffmpeg -i input.mp4 -i logo.png -filter_complex overlay=W-w:H-h output.mp4

去掉视频的logo

语法:-vf delogo=x:y:w:h[:t[:show]]
x:y 离左上角的坐标
w:h logo的宽和高
t: 矩形边缘的厚度默认值4
show:若设置为1有一个绿色的矩形,默认值0。

ffmpeg -i test.mp4 -vf delogo=0:0:220:90:100:1 output.mp4 

序列帧与视频的相互转换

把test.[001-100].jpg序列帧和music.mp3音频文件利用mpeg4编码方式合成视频文件test.avi:

ffmpeg -i music.mp3 -i test.%3d.jpg -s 800x600 -author yoyo -vcodec mpeg4 test.avi

还可以把视频文件导出成jpg序列帧:

ffmpeg -i test.avi pic.%d.jpg

好了,后面有空继续添加小技巧。

树莓派配置 vim 支持 python 代码补全

首先在树莓派中安装vim:

sudo apt-get update
sudo apt-get install vim  

克隆rkulla 的pydiction项目到本地:

mkdir -p ~/.vim/bundle
cd ~/.vim/bundle
git clone https://github.com/rkulla/pydiction.git
cp -r ~/.vim/bundle/pydiction/after/ ~/.vim   # 这步非常关键

如果有新的软件包想要支持补全.例如 RPi.GPIO
那么先安装包

sudo apt-get -y install RPi.GPIO

然后进入目录
cd ~/.vim/bundle/pydiction/
python3 pydiction.py RPi.GPIO
就可以生成全新的 complete-dict 了.

配置.vimrc文件:
" 启用文件类型插件

filetype plugin on

" 配置pydiction插件路径

let g:pydiction_location = '~/.vim/bundle/pydiction/complete-dict'

" 设置pydiction补全菜单的高度,默认是8

let g:pydiction_menu_height = 20
set syntax on

wiringPi updated to 2.52 for the Raspberry Pi 4B

前言

不知道你卖树莓派4B了么?不知道你们是否尝试在树莓派4b上用wiringpi的库,反正是官方的不好用。
那么你该怎么办?

场景展示

  • 如果你用sudo apt -y install wiringpi, 然后执行gpio readall的时候,提示你OPPS, the libararies is fucked.
    那么你就赶紧卸载掉吧!

    sudo apt -y purge wiringpi
    hash -r 

    下载软件包并且安装

    cd /tmp
    wget https://project-downloads.drogon.net/wiringpi-latest.deb
    sudo dpkg -i wiringpi-latest.deb

    检查:

    gpio readall 
    gpio -v 

    正常显示

    补充一下,源码下载的官方站点有点儿不稳定,我下载下来放在我博客上了,有需要的直接下载我这里的deb包安装效果一样的。

    wiringpi-latest.deb.tar
    下载后上传树莓派,通过命令

    tar -xf wiringpi-latest.deb_.tar.gz
    dpkg -i wiringpi-latest.deb

树莓派安装google拼音和录屏软件

今天在树莓派使用过程中要用到中文,以前都是全英文操作,现在突然要用中文,措手不及,赶紧装个软件。

  • 命令方式非常简单,打开一个终端然后输入:
    sudo apt-get install fcitx fcitx-googlepinyin fcitx-module-cloudpinyin fcitx-sunpinyin
    sudo reboot
    接着就可以看到桌面菜单上的键盘图标了,右击选择google输入法就好了。
    因为要做个视频,所以希望录下来桌面的操作情况,本来想用VNC,然后再电脑上录制,但是觉得直接在树莓派上录像也不是没有软件,遂尝试之。
    GOOGLE后发现各种软件太多了。
    这里逐一列出5个,共勉。

    Kazam

    Kazam可从Ubuntu存储库获得,支持ARM和PowerPC设备以及32位和64位,是一种高效的桌面视频捕获工具。
    Kazam能够录制全屏,所有桌面屏幕,单个窗口和桌面区域,还可以从扬声器和麦克风中捕获音频。

  • 安装方法:
    sudo apt-get install kazam

    然后kazam -f 试试,需要桌面环境才可以用。然而并不好用。
    继续尝试换了一个:

    Vokoscreen

    名字真TM怪,倭寇屏。。vokoscreen,发音难道是: "哇靠死可rain?" ????
    说明一下,这个是我用过的最好用的录屏软件没有之一。。。
    凭借简单的用户界面和丰富的功能,Vokoscreen是Linux的另一种屏幕录制选项。
    能够录制和流式传输您的桌面,您将需要一个高端游戏装备,以获得高清流媒体的好处。
    通过GitHub可以通过添加PPA通过命令行安装Vokoscreen。

  • 安装方法:
    sudo add-apt-repository ppa:vokoscreen-dev/vokoscreen
    sudo apt-get update
    sudo apt-get install vokoscreen

    Vokoscreen一旦安装完成就可以立即使用了。可以从整个桌面,窗口,区域或指针周围的指定放大率进行录制。
    这对于特写视频录制非常有用。可以选择视频和音频编解码器,同时还可以更改音频驱动程序,视频格式和帧速率。
    还可以在屏幕录制中包含网络摄像头中的素材,易于使用的按钮让您可以录制,停止,暂停和播放屏幕截图。
    唯一的缺陷是:需要密切关注文件大小!Vokoscreen总体上来说是一款非常方便的屏幕录制工具,易于设置和使用,老李在这里强烈
    最后一个是我不想尝试的,你们如果尝试了请告诉我好用不。

    SimpleScreenRecorder

    它可能称自己为“简单”,但这个应用程序实际上比这里列出的其他一些功能更多。
    启动后,您将看到一个屏幕,有很多选项。这些包括预期的全屏录制,录制选择,跟随光标,以及录制视频游戏录制GL的选项。
    为了节省时间,你还可以创建适合不同屏幕录制任务的配置文件。还可以录制音频,并通过“ 继续”按钮找到的后续屏幕更改视频类型。
    准备就绪后,点击开始录制 - 默认情况下,生成的视频将保存在你当前用户的主目录中。
    同样,如果您使用的是基于Ubuntu的发行版,则需要先安装PPA存储库,然后才能安装Simple Screen Recorder。

    sudo add-apt-repository ppa:maarten-baert/simplescreenrecorder
    sudo apt-get update
    sudo apt-get install simplescreenrecorder

    如果您正在运行64位系统并想要记录32位OpenGL应用程序,请安装:

    sudo apt-get install simplescreenrecorder-lib:i386

  • 差点儿忘记一个,老牌子的屏幕录制工具

    RecordMyDesktop

    适用于Linux的原始屏幕捕获工具RecordMyDesktop几乎可以在任何系统上运行,包括Raspberry Pi。
    虽然您可以通过命令行使用基本版本,但使用以下命令安装:

    sudo apt-get install recordmydesktop

    还可以使用该工具选择两个GUI前端,gtk-recordmydesktop和qt-recordmydesktop。

    安装完成后,这个简单的工具在高级菜单中提供了一些附加选项,以及选择整个窗口或仅选择部分的功能
    请记住在捕获后给它时间对视频进行编码。
    视频将输出到当前用户的主目录。

    结束语

    好了,到这里应该你已经有了自己的选择,那么就这样吧,我是骑驴玩儿漂移,青山常在,绿水长流,白了个白~