Posts

FFmpeg去水印

ffmpeg -i input.mp4 -vf "delogo=x=10:y=20:w=150:h=50" -c:a copy output.mp4 x: 水印左上角的水平位置(以像素为单位). y: 水印左上角的垂直位置(以像素为单位). w: 水印框的宽度. h: 水印框的高度. -c: 直接复制原始音频,无需重新编码,从而节省时间.

#pragma once的作用

#pragma once 作用: 1. 当一个头文件被多个源文件或其他头文件直接或间接 #include 时,该指令能确保它只被编译一次。 2. 避免宏名冲突:传统的 #ifndef 保护需要为每个文件设计独特的宏名(如 __MY_HEADER_H__), 若不小心在不同文件用了相同的宏名会导致代码失效;而 #pragma once 基于物理文件路径,彻底避免了命名污染和冲突。 3. 书写简洁:只需在头文件最上方写一行即可,无需写 #ifndef、#define 和 #endif 4 与#ifndef的区别: 4.1 标准性:#ifndef 是 C++ 标准支持的传统方法(也称 Include Guard); 而 #pragma once 虽然是非标准指令,但现代绝大多数主流编译器(如 GCC、Clang、MSVC)都提供了完美支持。 4.2 控制粒度:#ifndef 可以防范同一文件内局部的 代码片段 重复,而 #pragma once 只能针对 整个物理文件 。

Strategy Pattern

  Comparison of Modern C++ Approaches Implementation Type Mechanism Resolution Time Memory Overhead Use Case Runtime (Functional) std::function & Lambdas Runtime Small heap allocation (if lambda captures exceed small-buffer optimization) When algorithms must change on the fly during program execution. Compile-time (Static) Templates & Concepts ( requires ) Compile-time Zero runtime overhead (inlines perfectly) High-performance systems where strategies are known at compile time. Example 1: #include <bits/stdc++.h> class SortingStrategy { public: virtual void sort(std::vector<int>& arr) = 0; }; class BubbleSort : public SortingStrategy { public: void sort(std::vector<int>& arr) override { // Implement Bubble Sort algorithm } }; class QuickSort : public SortingStrategy { public: void sort(std::vector<int>& arr) override { // Implement Quick Sort algorithm } }; // Add more sorting algorithms as ne...

Flyweight Pattern

The Flyweight design pattern is a structural pattern designed to minimize memory usage by sharing as much data as possible with other similar objects. It divides an object's data into two categories: intrinsic state(heavy, constant, and shareable data) and extrinsic state (lightweight, unique, context-specific data) #include <iostream> #include <string> #include <string_view> #include <unordered_map> #include <vector> #include <memory> #include <shared_mutex> // 1. INTRINSIC STATE (The Flyweight) // This class contains heavy, immutable data shared across thousands of instances. class TreeModel { public: TreeModel(std::string name, std::string color, std::vector texture_data) : name_(std::move(name)), color_(std::move(color)), texture_bytes_(std::move(texture_data)) { std::cout texture_bytes_; // Simulates heavy mesh/texture data }; // 2. FLYWEIGHT FACTORY // Manages the shared pool of flyweights. It ensures obje...

Design Patterns

Creational: Factory Method (class) Abstract Factory (object) Builder  (object) Prototype  (object) Singleton  (object) Structral: Adapter (class/object) Bridge (object) Composite (object) Decorator  (object) Facade  (object) Flyweight   (object) Proxy  (object) Behavioral: Interpreter (class) Template Method (class) Chain of Responsibility (object) Command (object) Iterator (object) Mediator (object) Memento (object) Observer (object) State (object) Strategy (object) Visitor (object)

Adding Chinese watermarks to images using ffmpeg on Windows 10

Code: ffmpeg -i i1.jpg -vf "drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=168:fontcolor=white@0.5:x=(w-text_w)/2:y=(h-text_h)/2" output.jpg Multi Line Version: ffmpeg -i i1.jpg -vf "drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.1:y=h*0.2,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.4:y=h*0.2,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.7:y=h*0.2,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.1:y=h*0.5,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x=w*0.4:y=h*0.5,drawtext=text='仅为开户使用':fontfile='C\:/Windows/Fonts/msyh.ttc':fontsize=w*0.03:fontcolor=white@0.7:x...

Using spdlog in a C++ project

Image
Tree structure of the project. The code of main.cc. #include <iostream> #include "spdlog/spdlog.h" #include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/rotating_file_sink.h" void do_work_in_another_file(); void rotating_example() { try { // Create a file rotating logger with 5 MB size max and 3 rotated files auto max_size = 1048576 * 5; auto max_files = 3; // Set output time pattern // %f: Time accurate to microseconds // %n: Output mode "some_logger_name" // %l: Prints the severity level of the log message. Example include info, warn, error, debug or critical. // %v: Prints the actual text or payload of the log message that you passed to the function. spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%n] [%l] %v"); // Set it as default so you can spdlog::info() directly. auto logger = spdlog::rotating_logger_mt("som...

vim的智能配置

" 显示行号 set number " 将Tab键自动转换为空格 set expandtab " 设置Tab键本身的宽度为4个空格 set tabstop=4 " 设置按退格键(BackSpace)时一次删除4个空格 set softtabstop=4 " 设置使用>或<进行缩进时的宽度为4个空格 set shiftwidth=4 " 开启自动缩进(新行继承上一行的缩进) set autoindent " 开启C/C++语言智能缩进 set cindent " 搜索优化 set incsearch set ignorecase " 语法高亮 syntax on " 开启256色支持(在终端使用时非常重要) set t_Co=256

Installing vim via msys2 and config vim on windows10

1. Installing vim Open msys2 ucrt64 terminal and enter command as follows: ``` pacman -S vim ``` 2. Configure vim Open msys2 ucrt64 terminal and enter command as follows: ``` cd ~ touch .vimrc ``` Adding the content into .vimrc which you want to. For example: ``` set number ```

Constructing C++ Projects via CMake on Windows in git bash

Image
1. Installing CMake CMake Download 2. Building Source Code Files hello.cc #include <iostream> #include "spdlog/spdlog.h" int main(void) { spdlog::info("Welcome to spdlog!"); std::cout << "Hello World!" << std::endl; return 0; } 3. Building CMakeLists.txt File # 指定CMake最低版本 cmake_minimum_required(VERSION 3.10) # 必须在项目名称前指定C++编译器 set (CMAKE_CXX_COMPILER "g++.exe") # 指定项目名称和使用的编程语言 project(MyProject LANGUAGES CXX) # 添加spdlog目录,让spdlog可用 add_subdirectory(third_party/spdlog) # 指定C++标准 set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) # 指定生成的可执行文件及对应的源文件 add_executable(my_app main.cc) # 使用spdlog头文件模式 target_link_libraries(my_app PRIVATE spdlog::spdlog_header_only) 4. Building Project # 1. 配置项目(生成 Makefile 或 Ninja 文件) # -G "MinGW Makefiles" 指定使用MinGW而不是Windows默认的nmake, # 当然要提前安装MinGW到C:/MinGW安装路径不要有空格否则可能报错 # 且C:/MinGW/bin要放到Windows的环境路径上 # -G Ninja 指定使用Ninja,同样Ninja也需要提前准备好 #...

FFmpeg

FFmpeg extract audio mp3 from mp4 video ffmpeg -i video.mp4 -b:a 192K -vn music.mp3 How to merge audio and video file in ffmpeg ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac output.mp4

Generate ctags on FreeBSD

Install ctags # pkg install ctags Execute ctags Go to the directory where you want to generate ctags file and run command as follows: $ /usr/local/bin/exctags -R

Update the value of column value in clickhouse database

The command is as follows: :) ALTER TABLE tablename UPDATE column_name = column expresion1 WHERE  expression; Example: :) ALTER TABLE tablename UPDATE time = time / 100 WHERE id = 0;

YCM error. The ycmd server SHUT DOWN (restart wit...the instructions in the documentation) In FreeBSD

Open .py file with vim I got error message as following: " YCM error. The ycmd server SHUT DOWN (restart wit...the instructions in the documentation) " Solution: 1. Install cmake # pkg install cmake 2. Compile $ cd /home/j/.vim/bundle/YouCompleteMe $ python3 install.py

Install v2ray on FreeBSD

Install # pkg install v2ray Configuration Set v2ray system startup. Add content as following below to /etc/rc.conf # sysrc v2ray_enable=yes set v2ray's config.json of client. {   "log": {     "access": "",     "error": "",     "loglevel": "warning"   },   "inbounds": [     {       "tag": "socks",       "port": 10808, // port of client for socks       "listen": "127.0.0.1",       "protocol": "socks",       "sniffing": {         "enabled": true,         "destOverride": [           "http",           "tls"         ],         "routeOnly": false       },       "settings": {         "auth": "noauth",         "udp": true,         "allowTransparen...

Error message "Unknown function: togglebg#map" when open vim on FreeBSD

 Solution: $ mkdir ~/.vim/autoload $ cp ~/.vim/bundle/vim-colors-solarized/autoload/togglebg.vim ~/.vim/autoload

Cannot find color scheme 'zenburn' when open vim on FreeBSD

Solution: $ mkdir ~/.vim/colors $ cp ~/.vim/bundle/Zenburn/colors/zenburn.vim ~/.vim/colors/   

How to make VIM an IDE for Python on FreeBSD

Install VIM # pkg install vim VIM Extensions The first thing you need is a good extension manager. Vundle VIM has serveral extension managers, but I strongly recommend Vundle . Let's get Vundle installed: $ git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim This command downloads the Vundle plugin manager and chucks it in your VIM bundles directory. Now you  can manage all your extensions from the .vimrc configuration file . $ touch ~/.vimrc Now set up Vundle in your .vimrc by adding the following to the top of the file: set nocompatible              " required filetype off                  " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " alternatively, pass a path where Vundle should install plugins "call vundle#begin('~/some/path/here') " let Vundle manage Vundle, required Plugin 'gmarik/Vundle.vim' " ...

curl/curl.h file not found in FreeBSD

When I use clang to compile test.c, the following error occurs. fatal error : 'curl/curl.h' file not found The compile command is: $ clang test.c -o test I did some research. Then I know I need to change the compile command as follows: $ clang -I/usr/local/include -L/usr/local/lib -lcurl test.c -o test

fopen vs open in C

1. fopen is defined in the C standard library , but open is not. Therefore fopen is more portable than open. 2. fopen provides you with buffering IO that may turn out to be a lot faster than what you're doing with open.