H.264 + MP4 container, no audio

ffmpeg -i in.avi -vcodec h264 out.mp4
ffmpeg -i in.avi -ss 00:12:34 -t 01:23:45 out.mp4

-ss : the starting timecode, hh:mm:ss.xxx -t : the duration to transcode after the starting timecode, If omitted, will encode to the end.

-vcodec copy -acodec copy -vn : ignore video -an : ignore audio


To encode an image sequence to a video file:

ffmpeg -framerate 25 -i “image_%03d.png” -vcodec h264 -b:v 10485760 out.mp4
ffmpeg -framerate 24 -start_number 12 -i “image_%03d.png” -vcodec h264 -b:v 10485760 out.mp4

-b:v : the video bitrate of the output file


Transcode a video to an image sequence:

ffmpeg -i in.mp4 -vocdec png -an “image_%03d.png”

 


Rotate video

0 = 90CounterClockwise and Vertical Flip (default)

1 = 90Clockwise

2 = 90CounterClockwise

3 = 90Clockwise and Vertical Flip

To rotate 180 degress, instead use “transpose=2,transpose=2”

ffmpeg -i in.mp4 -vf “transpose=1” -codec:a copy out.mp4

 

'C, C++ > FFmepg' 카테고리의 다른 글

Cutting Videos Based on Time Range using FFmpeg  (0) 2022.11.23
FFmpeg CLI Tips  (0) 2021.09.01

※ 행렬 데이터 생성 = 메모리를 새로 생성하지 않고 공유함

// y-행에 대한 행렬 헤더 생성

cv::row(int y)

 

// x-열에 대한 행렬 헤더 생성

cv:col(int x)

 

// y-행에 대한 행렬 헤더 생성
cv::row(int y)

// x-열에 대한 행렬 헤더 생성
cv:col(int x)

// 부분 행렬 헤더 생성
cv::Mat cv::colRange(int startcol, int endcol) const
cv::Mat cv::rowRange(int startrow, int endrow) const
 
 
// 전치 행렬 변환
cv::MatExpr cv::Mat::t() const

 

// 이미지 파일 읽기
cv::Mat image_color = cv::imread("image.jpg");
cv::Mat image_gray  = cv::imread("image.jpg", 0);

// 생성

// 1채널 unsigned char
cv::Mat mat(h, w, CV_8UC1);
// 3채널 unsigned char
cv::Mat mat(h, w, CV_8UC3);

cv::Mat mat = cv::Mat::zeros(h, w, CV_32FC1);
cv::Mat mat = cv::Mat::ones(h, w, CV_64FC3);

unsigned char *pbBuffer; // 이미지 버퍼
cv::Mat mat(h, w, CV_8UC3, pbBuffer); // 메모리 공유

// 원소 초기화
cv::Mat mat(h, w, CV_8UC1);
// 모든 원소값을 3으로 초기화
mat = cv::Scalar(3);

cv::Mat mat2 = mat;	// 메모리 공유
cv::Mat mat3 = mat.clone();	// 별도 메모리
cv::Mat mat4;
mat.copyTo(mat4);	// 별도 메모리

// 형변환 복사
cv::Mat matf(h, w, CV_32FC1);
cv::Mat matb;
matf.convertTo(matb, CV_8U);

// color-gray 변환
cv::cvtColor(color, gray, CV_BGR2GRAY);
cv::cvtColor(gray, color, CV_GRAY2BGR);

// ROI
cv::Rect roi;
cv::Mat mat_roi = mat(roi);
cv::Mat mat_roi = mat(roi).clone();

// Resize
cv::resize(src, dst, cv::Size(w2, h2));
// Scale(sx, sy)
cv::resize(src, dst, cv::Size(), sx, sy);

// Flip
cv::flip(src, dst, 0);	// vertical flip
cv::flip(src, dst, 1);	// horizontal flip
cv::flip(src, dst, -1);	// vertical & horizontal flip

// Drawing
cv::Rect rc(x, y, w, h);
cv::Scalar color(B, G, R);
int thickness = 1;

cv::line(img, cv::Point(x1, y1), cv::Point(x2, y2), color, thickness);

cv::Rectangle(img, rc, color, thickness);
cv::Rectangle(img, rc.tl(), rc.br(), color, thickness);
cv::Rectangle(img, rc, color, CV_FILLED);

cv::Point center(rc.x + rc.width / 2, rc.y + rc.height / 2);
cv::Size radius(rc.width / 2, rc.height / 2);

double rot_deg = 0;		// rotation of ellipse
double s_deg = 0;		// start angle of arc
double e_deg = 360;		// end angle of arc
cv::ellipse(img, center, radius, rot_deg, s_deg, e_deg, color, thickness);
cv::ellipse(img, center, radius, rot_deg, s_deg, e_deg, color, CV_FILLED);

int circle_radius = 10;
cv::circle(img, center, circle_radius, color, thickness);
cv::circle(img, center, circle_radius, color, CV_FILLED);

cv::putText(img, "text", cv::Point(x, y), FONT_HERSHEY_SIMPLEX, 1, color, thickness);
cv::putText(img, "text", cv::Point(x, y), FONT_HERSHEY_DUPLEX, 1, color, thickness);

// display
cv::namedWindow("name");	// auto resized
cv::namedWindow("name", CV_WINDOW_NORMAL);	// manual resize

cv::imshow("name", img);
char ch = cv::waitKey();	// 무한대기
char ch = cv::waitKey(10);	// 10 ms 대기

cv::destroyWindow("name");
cv::destroyAllWindows();

 

cv::VideoCapture vc(0);           // webcam
cv::VideoCapture vc("video.mp4"); // video file

if (!vc.isOpened())
    return; // failed

vc.set(CV_CAP_PROP_FRAME_WIDTH, 640);
vc.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

cv::Mat img;
for (;;)
{
    vc >> img;
    if (img.empty())
        break;

    cv::imshow("image", img);
    if (cv::waitKey(10) == 27) // ESC
        break;
}

cv::destroyAllWindows();

 

double fps = 30;
int fourcc = CV_FOURCC('X', 'V', 'I', 'D'); // XVID codec
bool isColor = true;

cv::VideoWriter* video = new cv::VideoWriter;
if (!video->open("video.avi", fourcc, fps, cv::Size(w, h), isColor))
{
    delete video;
    return;
}

cv::Mat img;
for (;;)
{
    ;
    *video << img;
    ;
}

delete video;

 

// cv::Mat -> cv::IplImage
cv::Mat img;
cv::IplImage* pIplImg = &IplImage(img);

// cv::IplImage -> cv::Mat
IplImage* pIplImg;
cv::Mat img(pIplImg);

// 3 channels image
for (int r = 0; r < img.rows; ++r)
{
    for (int c = 0; c < img.cols; ++c)
    {
        img.at<Vec3b>(r, c)[0] += 10;
        img.at<Vec3b>(r, c)[1] += 20;
        img.at<Vec3b>(r, c)[2] += 30;
    }
}

// cv::Mat_<T>
cv::Mat_<unsigned char> img2 = img;
img2(r, c) = v;
#include <cstdlib>
#include <iostream>
#include <chrono>
#include <ctime>
#include <queue>
using namespace std;
 
int main()
{
    // your code goes here
 
    std::chrono::steady_clock::time_point t0, t1, tp;
    int isNotFirst = 0;
    double elapsed_s = 0, g_samples_s = 0;
    std::queue<double> g_fps;
    size_t nb_samples;
 
    for (int i = 0; i < 10; ++i)
    {
        t0 = std::chrono::steady_clock::now();
 
        int r = (std::rand() % 10) + 25; // 25 ~ 34
        std::this_thread::sleep_for(std::chrono::milliseconds(r));
 
        t1 = std::chrono::steady_clock::now();
        elapsed_s = (double)r / 1000;//std::chrono::duration_cast<std::chrono::duration<double>(t1 - t0).count();
 
        double fps = 0, avg_fps = 0;
 
        if (isNotFirst)
        {
            nb_samples = g_fps.size();
            elapsed_s = r;//std::chrono::duration_cast<std::chrono::duration<double>>(t0 - tp).count();
 
            g_samples_s += elapsed_s;
            g_fps.push(elapsed_s);
 
            if (nb_samples >= 10)
            {
                g_samples_s -= g_fps.front();
                   g_fps.pop();
            }
            else nb_samples++;
 
            fps = 1 / elapsed_s;
            avg_fps = nb_samples / g_samples_s;
        }
 
        tp = t0;
        isNotFirst = 1;
 
        printf("%d. %.2f/%.2f fps\r\n", i, fps, avg_fps);
    }
 
    return 0;
}

'C, C++' 카테고리의 다른 글

xlnt - XLSX 파일 다루기  (0) 2022.12.22
To install the MinGW-w64 toolchain  (0) 2022.10.28
문자열 구분자로 분리  (0) 2021.10.20
VSCode + vcpkg  (0) 2021.10.19
Get DLL path at run time  (0) 2021.10.05

+ Recent posts