旧开发板之废物利用

Posted on Sat 13 July 2024 in Journal

Abstract 深度学习笔记
Authors Walter Fan
 Category    learning note  
Status v1.0
Updated 2024-07-13
License CC-BY-NC-ND 4.0

仓库里有若干个旧的 Jetson Nano 开发板, 内存和硬盘都不够用, 放在柜子里吃灰. 最近把它们又从仓库里翻了出来, 用它们做自动播放视频, 用来模拟超声机的接入, 还是挺好用的.

  1. 准备好一些事先录制好的超声视频

  2. 写一个脚本, 读取视频文件, 然后按照指定次数播放

  3. playback.sh

cd /home/walter/test/script
video_path="/home/walter/test/video"
video_file="20240529.mp4"
loop_count=864000000

if [ $# -gt 0 ]; then
    video_file=$1
    echo "video file : $video_path/$video_file"
fi

if [ $# -gt 1 ]; then
    loop_count=$2
    echo "loop_count : $loop_count"
fi
echo "playback $video_path/$video_file $loop_count"
DISPLAY=:0 python3 media_player.py $video_path/$video_file $loop_count
  • media_play.py
import gi

gi.require_version('Gst', '1.0')
from gi.repository import Gst, GLib

Gst.init(None)
num_loops = 0

def play_media(file_path):
    pipeline_str = "playbin uri=file://" + file_path
    pipeline = Gst.parse_launch(pipeline_str)

    message_bus = pipeline.get_bus()
    loop = GLib.MainLoop()

    def on_message(bus, message):
        global num_loops
        t = message.type
        if t == Gst.MessageType.EOS:
            if num_loops > 0:
                print(f"left run count: {num_loops}")
                pipeline.set_state(Gst.State.READY)
                pipeline.set_state(Gst.State.PLAYING)
                num_loops -= 1
            else:
                print(f"quit because left run count is {num_loops}")
                loop.quit()
        elif t == Gst.MessageType.ERROR:
            err, debug = message.parse_error()
            print("Error: %s" % err)
            loop.quit()

    message_bus.add_signal_watch()
    message_bus.connect("message", on_message)

    pipeline.set_state(Gst.State.PLAYING)

    try:
        loop.run()
    except KeyboardInterrupt:
        print("Interrupted by user")

if __name__ == "__main__":
    import sys
    if len(sys.argv) < 3:
        print("Usage: python play_media.py <file_path> <num_loops>")
    else:
        print("note: the file_path should use absolute path")
        file_path = sys.argv[1]
        num_loops = int(sys.argv[2])
        play_media(file_path)
  1. 将脚本放到自动启动目录中, 一开机就自动启动并播放

  2. 创建 auto_play.desktop 文件, 内容如下

[Desktop Entry]
Type=Application
Exec=/home/walter/test/script/playback.sh
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=/home/walter/test/script/playback.sh
Name=/home/walter/test/script/playback.sh
Comment[en_US]=
Comment=
  • 复制上述文件到 ~/.config/autostart/ 目录中, 即可自动播放指定的视频
cp auto_play.desktop ~/.config/autostart/

本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。