Windows uses ffmpeg to double the speed and crop the video

Table of contents

Download FFmpeg

Video double speed

Operating procedures

code explanation

screen cropping

specify cropping area

Cutting time

report error

permission denied

Run PowerShell with administrator privileges

Create output files in other directories

Modify directory permissions

Summarize


To speed up MP4 videos using PowerShell, you can use the FFmpeg tool. Here are the steps to use PowerShell and FFmpeg on Windows:

Download FFmpeg

Before using PowerShell on Windows, you need to download FFmpeg first. The version suitable for your operating system can be downloaded from the official website https://ffmpeg.org/download.html.

The download address I use:

https://www.gyan.dev/ffmpeg/builds/ffmpeg-git-essentials.7z

Unzip it to a folder after downloading.

Video double speed

Operating procedures

Open PowerShell

On Windows, you can open PowerShell by pressing Win+X and then selecting "Windows PowerShell".

Enter the FFmpeg directory

In PowerShell, use the cd command to enter the bin directory of FFmpeg. For example, if FFmpeg is installed in C:\ffmpeg, you can use the following command to enter the bin directory:

cd C:\ffmpeg\bin

Speed ​​up the video twice with the following command:

./ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2[a]" -map "[v]" -map "[a]" -r 30 output.mp4

code explanation

The meaning of the parameters in this command is as follows:

- `-i input.mp4`: Specify the input file as input.mp4.

- `-filter_complex`: Specify complex video filters.

- `[0:v]setpts=0.5*PTS[v]`: Speed ​​up the video stream twice, but keep it at 30 frames per second. `[0:v]` means the video stream of the input file, `setpts=0.5*PTS` means to halve the PTS (Presentation Time Stamp) of the video stream, `[v]` means the output video stream.

- `[0:a]atempo=2[a]`: Speed ​​up the audio stream twice. `[0:a]` means the audio stream of the input file, `atempo=2` means double the speed of the audio stream, `[a]` means the output audio stream.

- `-map "[v]" -map "[a]"`: Specifies that the output file contains video and audio streams.

- `-r 30`: Specify the frame rate of the output file to be 30 frames per second.

- `output.mp4`: Specify the output file name as output.mp4.

After running the command, FFmpeg will speed up the input file input.mp4 twice and save the output file as output.mp4.

screen cropping

Operating procedures:

For example, crop the 1280 frame width to 640

Open powershell in the ....\ffmpeg\bin directory and execute:

./ffmpeg -i input.mp4 -filter_complex "[0:v]crop=640:trunc(ih/2)*2[v]" -map "[v]" -map 0:a -r 30 output.mp4

Code explanation:

The meaning of the parameters in this command is as follows:

- `-i input.mp4`: Specify the input file as input.mp4.
- `-filter_complex`: Specify complex video filters.
- `[0:v]crop=640:trunc(ih/2)*2[v]`: crop the video stream to a width of 640 pixels. `[0:v]` indicates the video stream of the input file, `crop=640:trunc(ih/2)*2` indicates that the video stream is cropped to a width of 640 pixels, and the height maintains the original ratio. `trunc(ih/2)*2` means to adjust the height to an even number, this is because some video codecs require an even number of heights. `[v]` indicates the output video stream.
- `-map "[v]" -map 0:a`: Specifies that the output file contains the video stream and the audio stream of the input file.
- `-r 30`: Specify the frame rate of the output file to be 30 frames per second.
- `output.mp4`: Specify the output file name as output.mp4.

After running the command, FFmpeg will crop the input file input.mp4 to a width of 640 pixels, and save the output file as output.mp4. Note that cropped videos may lose some frame content.

specify cropping area

Operating procedures:

To use PowerShell and FFmpeg to crop a video to a specified rectangular area, for example, to crop a certain rectangular area in a 1280*720 video.

Open powershell in the ....\ffmpeg\bin directory and execute:

./ffmpeg -i input.mp4 -filter_complex "[0:v]crop=640:360:320:180[v]" -map "[v]" -map 0:a -r 30 output.mp4

The meaning of the parameters in this command is as follows:

- `-i input.mp4`: Specify the input file as input.mp4.
- `-filter_complex`: Specify complex video filters.
- `[0:v]crop=640:360:320:180[v]`: Crop the video stream to the specified rectangular area. `[0:v]` indicates the video stream of the input file, `crop=640:360:320:180` indicates that the video stream is cropped to a width of 640 pixels, a height of 360 pixels, and the coordinates of the starting point are (320, 180) rectangular area. `[v]` indicates the output video stream.
- `-map "[v]" -map 0:a`: Specifies that the output file contains the video stream and the audio stream of the input file.
- `-r 30`: Specify the frame rate of the output file to be 30 frames per second.
- `output.mp4`: Specify the output file name as output.mp4.

After running the command, FFmpeg will crop the input file input.mp4 into the specified rectangular area, and save the output file as output.mp4. Note that when specifying the crop area, the aspect ratio of the video should be maintained to avoid distortion.

Cutting time

Crop a specified period of time in the middle of a long video.

For example, use PowerShell and FFmpeg to crop a 20-second video to the 3rd to 13th second part, open powershell in the ....\ffmpeg\bin directory, and execute:

./ffmpeg -i input.mp4 -ss 00:00:03 -t 00:00:10 -c copy output.mp4

The meaning of the parameters in this command is as follows:

- `-i input.mp4`: Specify the input file as input.mp4.
- `-ss 00:00:03`: Specifies to start cropping from the 3rd second. Start seconds
- `-t 00:00:10`: Specify to crop 10 seconds of video. duration
- `-c copy`: Specifies to use the "copy" codec to quickly copy video and audio streams without re-encoding.
- `output.mp4`: Specify the output file name as output.mp4.

After running the command, FFmpeg will cut out the 3rd to 13th seconds of the input file input.mp4, and save the output file as output.mp4. Note that when cropping a video, the aspect ratio of the video should be maintained to avoid distortion.

If you want to trim the video to other time periods, you can change the "-ss" and "-t" parameters in the command to other values. For example, to crop the video to the part from the 5th to the 15th second, you can use the following command: ./ffmpeg -i input.mp4 -ss 00:00:05 -t 00:00:10 -c copy output.mp4, this The command is similar to the previous command, except that the "-ss" parameter is set to 5 seconds and the "-t" parameter is set to 10 seconds.

Please note that when cropping a video, you should ensure that the cropped time period does not exceed the total duration of the video.

report error

permission denied

prompt error

[out#0/mp4 @ 00000284f9411ac0] Error opening output ./output.mp4: Permission denied

This error message indicates that FFmpeg does not have sufficient permissions to create output files in the current directory. In order to solve this problem, you can try the following methods:

Run PowerShell with administrator privileges

On Windows, you can right-click the PowerShell icon and select "Run as administrator" to run PowerShell with administrator privileges. Doing so may resolve the insufficient permissions issue.

Create output files in other directories

If the current directory does not have sufficient permissions to create the output file, you can try to create the output file in another directory. For example, the output file can be saved in the C:\temp directory with the following command:

./ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2[a]"-map "[v]" -map "[a]" -r 30 C:\temp\output.mp4

Modify directory permissions

If none of the above methods can solve the problem, you can try to modify the permissions of the current directory. You can right-click the current directory, select Properties, and add write permissions for the current user in the Security tab. Doing so may resolve the insufficient permissions issue.

Note that you need to be careful when modifying directory permissions so that you don't accidentally delete or modify other files.

Summarize

Download ffmpeg, decompress;

Open powershell in the ....\ffmpeg\bin directory and execute the command.

The output file address can be saved in another address.

Guess you like

Origin blog.csdn.net/weixin_56337147/article/details/130873372