In unity 2019.4.18f1
, videoplayer
is used to dynamically play mp4 videos. In win10 environment, whether it is Editor
or the packaged PC package, it can be played normally. But it doesn't play on win7 computer.
Intuitively suspect it is a decoding problem. The video is mp4 recorded with screen recording software, first use MP4Box
to view the video information.
mp4box -info G:\muweb\trunk\project\Assets\AssetSources\video\500545.mp4
# Movie Info - 2 tracks - TimeScale 1000
Duration 00:00:01.200
Fragmented: no
Major Brand isom - version 512 - compatible brands: isom iso2 avc1 mp41
Created: UNKNOWN DATE
iTunes Info:
tool: Lavf57.2.102
# Track 1 Info - ID 1 - TimeScale 15360
Media Duration 00:00:01.200
Track has 1 edits: track duration is 00:00:01.200
Track flags: Enabled In Movie
Media Info: Language "Undetermined (und)" - Type "vide:avc1" - 36 samples
Visual Sample Entry Info: width=488 height=550 (depth=24 bits)
Visual Track layout: x=0 y=0 width=488 height=550
AVC/H264 Video - Visual Size 488 x 550
AVC Info: 1 SPS - 1 PPS - Profile Baseline @ Level 3
NAL Unit length bits: 32
Chroma format YUV 4:2:0 - Luma bit depth 8 - chroma bit depth 8
SPS#1 hash: EF6F62B13BA263A64FB8D30358F6037AF8DD720F
PPS#1 hash: B45CE09D3615D8C80B755072D5E3307FDF18CB4E
RFC6381 Codec Parameters: avc1.42C01E
Only one sync sample
Max sample duration: 512 / 15360
# Track 2 Info - ID 2 - TimeScale 48000
Media Duration 00:00:00.630
Track has 1 edits: track duration is 00:00:00.597
Track flags: Enabled In Movie
Media Info: Language "Undetermined (und)" - Type "soun:mp4a" - 30 samples
Alternate Group ID 1
MPEG-4 Audio AAC LC (AOT=2 implicit) - 2 Channel(s) - SampleRate 48000
RFC6381 Codec Parameters: mp4a.40.2
All samples are sync
Max sample duration: 1024 / 48000
It can be seen that the video parameter is RFC6381 Codec Parameters: avc1.42C01E
, but not all MP4s have this parameter, some are RFC6381 Codec Parameters: mp4v.20.1
. We are not familiar with this codec
, and the requirements for several formats of MP4 on Microsoft's official website are only win7:
The same is true, these videos can be played on win7
, for example, using the Windows Media Player
that comes with win7
, but not in unity
, it means that the system itself does not support it, but The reason for unity
.
Then I saw a blog, which mentioned the upgrade of win7
to sp1
. It was confirmed that the win7
used in the test was originally sp1
, and the above picture also shows that win7
supports MP4
, has nothing to do with the system.
What about changing the video encoding to vp8
? After testing, it is indeed feasible, whether it can be played on win10 or win7.
So, the solution is to write a script to handle this automatically and change the encoding to vp8
when importing MP4, as follows:
using UnityEngine;
using UnityEditor;
using System.IO;
public class AssetPostprocessorEx : AssetPostprocessor
{
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
for (int i = 0; i < movedAssets.Length; i++)
{
AssetDatabase.ImportAsset(movedAssets[i]);
}
}
void OnPreprocessAsset()
{
var type = assetImporter.GetType();
if (type == typeof(VideoClipImporter))
{
VideoClipImporter import = (VideoClipImporter)assetImporter;
var setting = import.defaultTargetSettings;
setting.enableTranscoding = true;
setting.codec = UnityEditor.VideoCodec.VP8;
import.defaultTargetSettings = setting;
}
else
{
// ... Handling other types of resources
}
}