ffmpeg learning

Code address: https://gitee.com/panmingshuai/ffmpeg-demo.git

After the code is swung down, there is a ffmpeg.zip compressed package in the resource directory, unzip it and put it under E:\ffmpeg:

Then we use java to call ffmpeg commands to perform video related operations:

Convert video to flv format

public static void processFLV() throws IOException {
		List<String> commend = new ArrayList<String>();
		commend.add("E:\\ffmpeg\\ffmpeg"); // ffmpeg文件的位置
		commend.add("-i");
		commend.add("E:\\qwe.mp4");
		commend.add("-ab");
		commend.add("56");
		commend.add("-ar");
		commend.add("22050");
		commend.add("-qscale");
		commend.add("8");
		commend.add("-r");
		commend.add("15");
		commend.add("-s");
		commend.add("600x500");
		commend.add("E:\\qwenew.flv");

		ProcessBuilder builder = new ProcessBuilder();
		builder.command(commend);
		builder.start();
	}

Here -i represents the path of the file to be operated, and the last path of the command represents the path and file name of the processed file, remember to write .flv.

Conversion of the specified format

// 指定格式转换 但是这里没有设置其他参数,所以生成的文件以ffmpeg内置的生成
	// MP4转avi c:\ffmpeg\ffmpeg -i c:\ffmpeg\input\c.mp4 -f avi
	// c:\ffmpeg\output\a.avi
	public static void process() throws IOException {
		List<String> commend = new ArrayList<String>();
		commend.add("E:\\ffmpeg\\ffmpeg"); // ffmpeg文件的位置
		commend.add("-i"); // 指定要处理的文件
		commend.add("E:\\qwe.mp4");
		commend.add("-f"); // 指定转换格式
		commend.add("avi");
		commend.add("E:\\qwe2.avi"); // 最后指定文件输出的路径

		ProcessBuilder builder = new ProcessBuilder();
		builder.command(commend);
		builder.start();
	}

The -f here indicates what format to specify for forced conversion, and the popular avi format is selected here.

Video screenshot:

// 截图
	// ffmpeg -i test.asf -y -f image2 -t 0.001 -s 352x240 a.jpg
	public static void cutScreen() throws IOException {
		List<String> commend = new ArrayList<String>();
		commend.add("E:\\ffmpeg\\ffmpeg"); // ffmpeg文件的位置
		commend.add("-i");
		commend.add("E:\\qwe.mp4");
		commend.add("-y");// 输出覆盖路径,即如果已存在下面指定路径的文件,则覆盖掉
		commend.add("-f");
		commend.add("image2");
		commend.add("-ss"); // 指定在哪截图
		commend.add("5");
		commend.add("-t"); // 指定要记录的时间,因为是截图所以是0.001s
		commend.add("0.001");
		commend.add("-s");
		commend.add("1920x1080");
		commend.add("E:\\qwe3.jpg");

		ProcessBuilder builder = new ProcessBuilder();
		builder.command(commend);
		builder.start();
	}

The -ss here indicates where to take the screenshot in the video, and its unit is seconds, that is, it can also be in the form of HH:mm:ss. There is also a method of taking screenshots by how many frames, but it doesn't feel practical. Most people say how many times to take screenshots, but I have never heard of anyone asking how many frames to take screenshots. -t indicates how long the video capture time lasts, and the unit is also seconds. Because it is a screenshot, it is set to 0.001 seconds. -s indicates the length and width of the screenshot.

Take a video clip:

// 截取视频
	public static void cutVedio() throws IOException {
		List<String> commend = new ArrayList<String>();
		commend.add("E:\\ffmpeg\\ffmpeg"); // ffmpeg文件的位置
		commend.add("-i");
		commend.add("E:\\qwe.mp4");
		commend.add("-vcodec");//视频使用原来一样的视频编解码器。
		commend.add("copy");
		commend.add("-acodec");//音频使用原来一样的音频编解码器。
		commend.add("copy");
		commend.add("-ss");
		commend.add("00:00:25");
		commend.add("-t");
		commend.add("10");
		commend.add("E:\\qwe563.mp4");
		commend.add("-y");

		ProcessBuilder builder = new ProcessBuilder();
		builder.command(commend);
		builder.start();
	}

This passage has been introduced before, so I won't say more.

The following lists some commonly used ffmpeg commands, which can be called as above

-L license

-h help

-fromats Show available formats, codecs, protocols. . .

-f fmt force format fmt

-I filename input file

-y overwrite output file

-t duration Set the recording time hh:mm:ss[.xxx] format recording time is also supported

-ss position Search to the specified time [-]hh:mm:ss[.xxx] format is also supported

-title string set title

-author string set author

-copyright string set copyright

-comment string set comment

-target type Set the target file type (vcd, svcd, dvd) 

Video options:

-b bitrate Set the bit rate, the default is 200kb/s

-r fps set frame rate default 25

-s size Set the frame size format to WXH default 160X128. The following abbreviations can also be used directly:
Sqcif 128X96 qcif 176X144 cif 252X288 4cif 704X576

-aspect aspect set aspect ratio 4:3 16:9 or 1.3333 1.7777

-vn do not do video recording

-bt tolerance Set the video bit rate tolerance kbit/s

-vcodec codec Force the use of codec codec. If copy is used to indicate that the original codec data must be copied.

-pass n Select the number of processing passes (1 or 2). Two-pass encoding is very useful. The first pass generates statistics, the second pass generates the exact requested bit rate

Audio options:

-ac channels set channels default to 1

-an disable audio recording

-acodec codec use codec codec

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324887331&siteId=291194637