Technology Sharing

Use ffmpeg to convert mkv video files in a directory into mp4 format

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

I recently learned how to edit videos. The video resources I downloaded from BT seeds are all in mkv format and cannot be directly imported into video editing software. In this case, you need to use some format conversion tools to convert them, or you can use ffmpeg for editing.

ffmpeg is a command line tool used to edit local audio and video software. This is also the first time I use ffmpeg. Although I know its powerful functions, I installed it on my computer after understanding it before, but I haven't touched it. I took this opportunity to try it.

First, I used Thunder to download the film and television resources to the following directory:
insert image description here
There are two ways to use ffmpeg to process a file:

  • ffmpeg -i 要转换的视频地址 转换后的视频存放地址
  • ffmpeg -i 要转换的视频地址 -c copy 转换后的视频存放地址
    The difference between the two methods is that the former will re-encode the original video before converting, while the latter will convert directly without encoding. So the latter is faster. Here we choose the second method to try to convert the first mkv file in the above picture, and the output directory is my desktop.

After entering the first part of the command, drag the file directly to the space on the command line to automatically paste the directory, and continue to enter your own storage path. I save it to the desktop first, /Users/guolianxin/Desktop, as follows:
insert image description here

I found an error because the storage path does not refer to a folder, but to the converted format.
insert image description here

I re-added the file name and format suffix that the content was to be saved as, and successfully generated the target file on the desktop.insert image description here

insert image description here

There was a problem opening the file: the message "Cannot open file or stream" appeared.
insert image description here

The error is as follows:
[mp4 @ 0x12f607870] track 1: codec frame size is not set [mp4 @ 0x12f607870] flac in MP4 support is experimental, add '-strict -2' if you want to use it. Could not write header for output file #0 (incorrect codec parameters ?): Experimental feature Error initializing output stream 0:1 --

After searching online, I found that using flac when encoding to mp4 is experimental, and that I need to add the command to allow it, and that there is a problem with the file header being written.

I don't know how to do it either, so I found other commands:
ffmpeg -i 输入路径 -c:v copy -c:a aac -strict -2 输出路径
Then it took more than ten seconds to process and successfully exported:
insert image description here