To transcode a HEVC (H.265) video file to H.264 using FFmpeg with CPU-only encoding for compatibility with Unity, you can use the following command:
ffmpeg -i input_hevc.mp4 -c:v libx264 -preset medium -crf 23 -pix_fmt yuv420p -profile:v high -level 4.2 -c:a aac -b:a 128k output_h264.mp4
-i input_hevc.mp4 : Specifies the input file (in HEVC format).
-c:v libx264 : Specifies using the x264 encoder to output H.264 video.
-preset medium : Adjusts the speed/quality trade-off (you can also choose slow, fast, etc., depending on your needs).
-crf 23 : Sets the Constant Rate Factor for quality vs. file size. The range is from 0 (best quality) to 51 (worst quality), where 23 is a good balance. You can adjust this based on your needs.
-pix_fmt yuv420p : Forces the pixel format to yuv420p, which is widely compatible with Unity and most playback systems.
-profile:v high : Sets the H.264 profile to high, which gives better quality while maintaining wide compatibility.
-level 4.2 : Ensures the H.264 level is compatible with most devices and Unity’s requirements. Level 4.2 is typically sufficient for 1080p video.
-c:a aac : Sets the audio codec to AAC, which is widely compatible with Unity.
-b:a 128k : Sets the audio bitrate to 128 kbps, a commonly used rate that ensures reasonable audio quality.
output_h264.mp4 : Specifies the name of the output file in H.264 format.
- Unity generally prefers standard H.264 encoded video files for smooth compatibility across platforms.
- If the input file is very high in quality, you can adjust
-crf to a lower number (e.g., 18) to maintain higher quality.
- The command uses only CPU encoding and not any hardware acceleration like
nvenc or qsv.
- Make sure your
input_hevc.mp4 file path and output_h264.mp4 file path are set correctly for your specific case.
This command should result in a video format compatible with Unity, ensuring compatibility across various target devices for your application.