How can I convert .264 file to .mp4
I want to convert an elementary stream(.264) to container format(.mp4). Can someone please help me on this? How can I use ffmpeg to do this task? What are all the other methods that could accomplish the same task?
3 Answers 3
ffmpeg -framerate 24 -i input.264 -c copy output.mp4
- This simply stream copies (re-muxes) the video so there is no unnecessary re-encoding occurring; therefore the quality is preserved and the whole process is quick.
- Frame rate is by default assumed to be 25. You can change this with the -framerate input option. Typical values are 30000/1001 , 25 (default), 24000/1001 , 24 , or frame rate aliases such as ntsc , ntsc-film , or pal .
- If you don’t know the frame rate, you can perform the conversion using your best guess as to the frame rate, and then compare the running duration of the output file with the input file running duration and then calculate the actual frame rate. e.g. assume 24 fps and actual running time of 1:00:00 (60 mins) if resulting file has running time of 1:02:30 (62.5 mins) then actual frame rate is 25 fps (24 * 62.5 / 60)
One should note that raw H.264 streams don’t carry a rate information, so FFmpeg will display a warning and default to 25 frames/second. If you want a different frame rate you can use the -r switch, e. g. -r 30 for 30 frames/second.
You’re correct. I should have read the entire paragraph in the FFmpeg documentation: “If in doubt use -framerate instead of the input option -r .” For some reason -framerate isn’t documented in this manual here though.
That of ffmpeg(1) (Ctrl+F for -framrate ). There’s documentation for it in the avfoundation input device section but it doesn’t look like it applies to other cases.
@MotsManish I recommend asking a new question. In the question you should provide the complete output of ffmpeg -i input where input is the file from the camera.
sudo apt-get install x264 x264 raw_stream.264 -o playable_video.mp4
I wrote a simple bash script to convert all the files in a directory. Make sure the directory only contains the source files since the operation will run on all files in a given directory.
touch ./convert chmod +x ./convert
#!/bin/bash for f in *; do if [ -f "$f" ] && [ "$f" != "convert" ]; then ffmpeg -framerate 25 -i "$f" -c copy "$f.mp4" fi done
Drop in a directory with only the source files, double click and choose run
This script assumes ffmpeg is set up on you system. Not sure which libs are needed, this is what I installed before running: sudo apt install ffmpeg x264 x265 h264enc mencoder mplayer
Only run it once
AbsoluteDestiny / 01. Download Locations for FFmpeg.md
Also, be sure to change input.mkv and outputFile as needed for your file.
-i input.mkv -pix_fmt yuv420p -c:v libx264 -crf 0 -g 1 -an outputFile.mp4
-i input.mkv -pix_fmt yuv420p -c:v libx264 -crf 14 -g 1 -preset faster -an outputFile.mp4
-i input.mkv -pix_fmt yuv422p10le -c:v prores_ks -profile:v 3 -vendor ap10 -an outputFile.mov
Prores Ultra (for VFX and keeping Alpha Channel)
-i input.mkv -pix_fmt yuv444p10 -c:v prores_ks -profile:v 4 -vendor ap10 -an outputFile.mov
DNxHD 1080p 23.976 or 24fps
-i input.mkv -pix_fmt yuv422p -c:v dnxhd -b:v 175M -an outputFile.mov
-i input.mkv -pix_fmt yuv422p -filter:v "scale=(sar*iw)*min(1920/(sar*iw)\,1080/ih):ih*min(1920/(sar*iw)\,1080/ih), pad=1920:1080:(1920-(sar*iw)*min(1920/(sar*iw)\,1080/ih))/2:(1080-ih*min(1920/(sar*iw)\,1080/ih))/2" -c:v dnxhd -b:v 175M -an outputFile.mov
DNxHD 720p 23.976 or 24fps
-i input.mkv -pix_fmt yuv422p -c:v dnxhd -b:v 90M -an outputFile.mov
-i input.mkv -pix_fmt yuv422p -filter:v "scale=(sar*iw)*min(1280/(sar*iw)\,720/ih):ih*min(1280/(sar*iw)\,720/ih), pad=1280:720:(1280-(sar*iw)*min(1280/(sar*iw)\,720/ih))/2:(720-ih*min(1280/(sar*iw)\,720/ih))/2" -c:v dnxhd -b:v 90M -an outputFile.mov
-i input.mkv -c:v utvideo -an outputFile.avi
-i input.mkv -c:v mjpeg -q:v 1 -an outputFile.avi
Convert only a part of the file
—ss is the start time and -t is the duration of the clip. Alternatively you can use -to to set the end timestamp of the clip.
Quick seeking (inaccurate, snaps to nearest keyframe):
--ss 00:01:23.123 -i input.mkv -t 20 [. remaining commands]
--ss 00:01:23.123 -i input.mkv -to 00:01:43.123 [. remaining commands]
-i input.mkv --ss 00:01:23.123 -t 20 [. remaining commands]
-i input.mkv --ss 00:01:23.123 -to 00:01:43.123 [. remaining commands]
Split file based on its keyframes
-i input.mp4 -f segment -c:v copy -c:a copy -reset_timestamps 1 -map 0 outputFile%d.mp4
Split file into time-based segments
Example below splits into 5 minute chunks
-i input.mp4 -f segment -segment_time 0:05:00 -c:v copy -c:a copy -reset_timestamps 1 -map 0 outputFile%d.mp4
Detailed Source Conversion Guide
Breakdown of an FFMpeg Command
ffmpeg -i inputfile1.mkv [Source Settings] [Video Settings] [Audio Settings] [Container Settings] output.ext
-pix_fmt — forces the colorspace you wish to work in
Not usually required — ffmpeg will often choose the correct option for the codec(s) involed but some encoders can support multiple colorspaces so you sometimes need to be specific. Here are some common ones:
- -pix_fmt yuv420p — same as DVD and BLu-ray footage. Common in most compressed streaming media. Use this for encoding for the web
- -pix_fmt yuv422p — More chroma samples, used for common intermediate and editing codecs
- -pix_fmt yuv444p — ‘Lossless’ colorspace used for high quality editing codec profiles when using raw footage.
- -pix_fmt yuv444p10 — as above but using 10bits for the luma channel instead of 8. Better for VFX work — smoother gradients etc.
Resizing source video before encoding
Before scaling, set the rsize algorithm: -sws_flags spline or -sws_flags lanczos will do.
Then, resize either with the scale video filter: -filter:v scale
Scale to a particular width, auto-calculate the appropriate height:
Scale to a particular height, auto-calculate the appropriate width:
Scale to fit into a 1920×1080 frame and add borders if needed (useful for DXnHD etc):
-filter:v "scale=(sar*iw)*min(1920/(sar*iw)\,1080/ih):ih*min(1920/(sar*iw)\,1080/ih), pad=1920:1080:(1920-(sar*iw)*min(1920/(sar*iw)\,1080/ih))/2:(1080-ih*min(1920/(sar*iw)\,1080/ih))/2"
Generic pattern, for any size — just replace XXXX with the width and YYYY with the Y
-filter:v "scale=(sar*iw)*min(XXXX/(sar*iw)\,YYYY/ih):ih*min(XXXX/(sar*iw)\,YYYY/ih), pad=XXXX:YYYY:(XXXX-(sar*iw)*min(XXXX/(sar*iw)\,YYYY/ih))/2:(YYYY-ih*min(XXXX/(sar*iw)\,YYYY/ih))/2"
Choosing a video codec in ffmpeg generally uses the format -c:v followed by the codec
Here is a summary of the most important codecs for conversion:
- libx264 — The open-source encoder for h264 encoding (mpeg4), usually for web distribution but also can be set as a high quality lossy intermediate codec for editing on some systems.
- prores and prores_ks — ffmpeg’s implementation of Apple’s ProRes codec. Fairly industry standard for Final Cut usage — works in all pro editing systems. Main downside is the ffmpeg implementation is slow as an encoder so processing source may take longer than other options.
- dxnhd — Avid’s editing codec. Just as well supported as ProRes and encodes faster with ffmpeg, which is a plus. However, this is profile-based so will only allow certain resolution, fps and colorspace combinations. If you source isn’t exactly 720p or 1080p or what have you you will need to add borders to be able to convert using this codec.
- utvideo — Lossless codec, excellent quality but only really supported by Windows version of video editors. Can be sometimes slow to read making quick editing trickier but the compressed size is fairly similar to high quality lossy codecs!
- mjpeg — low quality traditional format that reads and writes really fast so good as a proxy source for video editors that allow low-quality proxies to be used.
The main aspects here are -crf which controlls quality (0 being lossless and 22 being the default). -g controls GOP length so setting this to 1 makes every frame a keyframe which is ideal for editing.
Lossy encoding with every frame a keyframe
or, if you don’t care about the file size that much and would rather it encode quicker:
-c:v libx264 -crf 14 -preset faster -g 1
16 is medium quality but lower numbers would be very good quality. The lower the number, the larger the file until you reach lossless.
Note the -vendor ap10 part below is only needed if working with Final Cut, but it does no harm otherwise.
High Quality 4444 — usually overkill unless doing VFX work
-pix_fmt yuv444p10 -c:v prores_ks -profile:v 4 -vendor ap10
High Quality 422 (84 Mbit/s) — use this one
-c:v prores_ks -profile:v 3 -vendor ap10
Standard Quality 422 — or this if you are low on space and need something ‘good enough’
-c:v prores_ks -profile:v 2 -vendor ap10
Proxy Quality — For proxy files only, looks crappy but edits fast and is small
-c:v prores_ks -profile:v 0 -vendor ap10
First look up the bitrate you need to set on this table based on the framesize, fps and desired quality level. For each there is a standard and a high quality version. The high quality version is preferred:
Project Format | Resolution | Frame Size | Bits | FPS | |
---|---|---|---|---|---|
1080i / 59.94 | DNxHD 220 | 1920 x 1080 | 8 | 29.97 | 220M |
1080i / 59.94 | DNxHD 145 | 1920 x 1080 | 8 | 29.97 | 145M |
1080i / 50 | DNxHD 185 | 1920 x 1080 | 8 | 25 | 185M |
1080i / 50 | DNxHD 120 | 1920 x 1080 | 8 | 25 | 120M |
1080p / 25 | DNxHD 185 | 1920 x 1080 | 8 | 25 | 185M |
1080p / 25 | DNxHD 120 | 1920 x 1080 | 8 | 25 | 120M |
1080p / 25 | DNxHD 36 | 1920 x 1080 | 8 | 25 | 36M |
1080p / 24 | DNxHD 175 | 1920 x 1080 | 8 | 24 | 175M |
1080p / 24 | DNxHD 115 | 1920 x 1080 | 8 | 24 | 115M |
1080p / 24 | DNxHD 36 | 1920 x 1080 | 8 | 24 | 36M |
1080p / 23.976 | DNxHD 175 | 1920 x 1080 | 8 | 23.976 | 175M |
1080p / 23.976 | DNxHD 115 | 1920 x 1080 | 8 | 23.976 | 115M |
1080p / 23.976 | DNxHD 36 | 1920 x 1080 | 8 | 23.976 | 36M |
1080p / 29.7 | DNxHD 45 | 1920 x 1080 | 8 | 29.97 | 45M |
720p / 59.94 | DNxHD 220 | 1280×720 | 8 | 59.94 | 220M |
720p / 59.94 | DNxHD 145 | 1280×720 | 8 | 59.94 | 145M |
720p / 50 | DNxHD 175 | 1280×720 | 8 | 50 | 175M |
720p / 50 | DNxHD 115 | 1280×720 | 8 | 50 | 115M |
720p / 23.976 | DNxHD 90 | 1280×720 | 8 | 23.976 | 90M |
720p / 23.976 | DNxHD 60 | 1280×720 | 8 | 23.976 | 60M |
Then add the bitrate to this command (using 1080p 24fps as an example):
UTVideo, being lossless, can generally handle any input format, so the command is simply:
Where qscale is the quantizer to use, 1 being the highest. Still not great quality. For shitty proxy files you can drop qscale to 4 or lower to make small but ugly and fast files.
Simply add this shorthand
Stereo Bitrates: 128k, 160k, 192k, 224k, 320k
224 is what iTunes uses if that gives any idea
Container Settings and Extension
You can determine a container format either by specifying it on the commandline with -f otherwise ffmpeg will use the filename to determine the container:
-f mp4 or simply outputFilname.mp4
-f mov or outputFilename.mov
-f mxf or outputFilename.mxf or -f mov or outputFilename.mov
Encoding UTVideo or MJpeg
-f avi or outputFilename.avi
FFMpeg Encoding for online sharing
Basic no-brainer encoding options
The -crf 18 option here is very high quality. 20 would be lower quality. The default is actually 23. You can also use fractional amounts e.g. 18.5
ffmpeg -i inputfile.avi -pix_fmp yuv420p -c:v libx264 -crf 18 -preset slower -c:a aac -b:a 224k -movflags +faststart output.mp4
This is where I usually start, though for 1080p encodes this often creates a file that’s way too big so I then reduce the number and find the sweet spot where it’s not too large and not too bad-looking.
High quality but with some bitrate limits
It’s possible you may have a target device (like, I dunno, some streaming box) that chokes on high birtates you can set rate limits in addition to keeping your -crf value.
ffmpeg -i inputfile.avi -pix_fmp yuv420p -c:v libx264 -crf 18 -maxrate 7M -bufsize 10M -preset slower -c:a aac -b:a 224k -movflags +faststart output.mp4
Adding Metadata (Title, Author, etc)
ffmpeg [settings above except output.mp4] -metadata title="Video Title" -metadata author="Author Name" output.mp4
ffmpeg -i inputfile.avi -c:v libvpx-vp9 -crf 30 -b:v 0 -c:a libopus -b:a 224k output.webm