Sunday, January 21, 2018
Unity3d Video Editing
How to use Unity3D 2017.x+ to make *.mp4s with overlaid text.Update: The next day I stumbled on an Asset Store tool that does the exporting to an *.mp4 for you.
Why I didn’t go with Blender
I wanted to make a Facebook style video to run an ad. Which means take an existing video and overlay some text so that the video had some meaning when it was played without audio. Initially I tried to use Blender as I thought it’d be a great way for me to learn that tool. Plus, there are tons of tutorials on how to use Blender for video editing. Halfway through my first attempt I decided to find a way other than Blender due to:
- Blenders text overlay feature is difficult to work with. Overlaid strings of text can not be multi-line.
- I couldn’t figure out how to copy the animated graph window parts text objects, meaning my 4 pieces of text which each had 4 lines of text would require redoing my animated graph fade work 16 times.
- I once found a way Blender scrubbing a video wouldn’t be choppy on a low end computer, I was unable to re-find this method. Upping Blenders RAM usage wasn’t enough.
Why I went with Unity3D
Unity3D I was already familiar with. Yet I couldn’t find a single guide on how to use it to create *.mp4s. I realized I could use a screen recorder, like ObsStudio, to encode the output to an *.mp4. The more I thought about it the better Unity3D seemed to fit:
- Unity3D has pretty good UI tools.
- Unity3D 5.6 introduced a new VideoPlayer component.
- Unity3D 2017.1 introduced a new TimeLine tool.
Setting up Unity3D
All we’re going to do is overlay text ontop of an already composed video. The first step is to make sure our video plays correctly in Unity3D. These steps were taken from a section of this youtube video: https://youtu.be/V8rwCWiRLWI?t=2m53s
- Start a new Unity3D project / scene
- Drop an *.mp4 source video file in the Unity3d project window. This will be the background video our text we be overlaid upon.
- Add a VideoPlayer component to an empty game object.
- Drag *.mp4 clip onto video player component.
- On the VideoPlayer component change Render Mode to “Camera Far Plane”.
- Drag the Main Camera into the Camera slot in VideoPlayer.
- Add an AudioSource component to the same VideoPlayer game object.
- Drag that AudioSource component into the Audio Source field of the VideoPlayer component.
Creating Text Objects
I wanted text to fade in over half a second, display for 5.5 seconds, then fade out over half a second.
- Add GameObject > UI > Text.
- To set the text to use the full view, open the Rect Transform’s red cross-hair positioning icon. Hold shift+alt and click the popup windows bottom right icon.
- In the Text component set the font size and Paragraph Alignment to what you prefer. Set the color to white.
- Add an Outline component to help the text stand out from your video. Set the alpha of the outlines’ black to 255.
- Add a Canvas Group component to this text.
- Make sure the Text object is selected in your hierarchy. Then open the Animation window and click “Create”.
- Click the red circle record button in the Animation window.
- Make sure the play head is at 0 and change the Texts’ Canvas Group components Alpha value to 0.
- Move the play head to 0:30 (half a second) and put the Alpha value back to 1.
- Scroll out until you see 6:30 on the animation track.
- In the Canvas Group.Alpha row right click under 6:00 and “Add Key”.
- Move the play head to 6:30 and set the Alpha back to 0.
- Turn off record mode and press the play preview button to make sure the fade works correctly.
- Find the saved *.anim file in the Project explorer. Click it to uncheck “Loop Time” in the inspector. This will make sure the same text isn’t shown again after it’s played once.
- Copy the text component as many times as you need with Ctrl+D. Change each one’s text to match what you want it to say.
- Disable all text gameobjects, they’ll be enabled by the timeline below.
Setting when text gets shown
The timeline feature of Unity 2017 is great for timing which objects are shown when. With how our text objects are setup all we need to do is enable them at the correct time.
- In Unity3D go to Window > Timeline
- Create a new empty gameobject called “Timeline”
- Click “Create” in the Timeline window
- Save it as TextTiming.playable
- Delete its only row, which should have a blue cap on the left (animation track) and contain a reference to the Timeline game object
- Add an Activation track to the timeline.
- Click the settings cog in the upper right of the Timeline window and change the view to Seconds instead of Frames.
- Resize the Activation track’s Active to be 8 seconds long
- Select the Activation track and press Ctrl+D for each copy of the Text object you created.
- Drag one Text object to each Activation track
- Move their Active components to start when the previous ends. It should look like a staircase.
- Extend the last Texts Active track to be as long as your video. Otherwise all text gameobjects activate.
Now when you press the Unity Editors play button you should see your video play with text being overlaid. You’re ready to record an *.mp4!
Setting up ObsStudio
To setup the video resolution in ObsStudio go to Settings > Video > Base Resolution. Here I also set “Common FPS Values” to “30”.
Next decide how the file will be saved in Settings > Output by setting its’ “Recording Path” and “Recording Format”
Recording from the “game” view in the Unity3D editor worked great for me. I made sure the Unity3D game window was also locked to the correct resolution by changing the resolution from “Free Aspect” to match the value I put in ObsStudios’ “Base Resolution”. More details on the game view control bar at https://docs.unity3d.com/Manual/GameView.html
Under Sources add a Display Capture. This shouldn’t show your entire monitor’s contents as long as you set the Base Resolution smaller than your monitors resolution. You do not want to scale the video with the red box handles, instead you want to pan it to the right part of the monitor by left clicking any part of the display view and dragging.
- Made sure the Unity3D game window was bigger than the set video resolution.
- Set the edge of the game view against the right edge of my monitor.
- In ObsStudio made sure the game window was centered in the display
- Pressed the “Start Recording” button in ObsStudio.
- Pressed play in Unity3D
- Waited until it played through what I wanted
- Pressed “Stop Recording” in ObsStudio
I used Windows 10 Photos to trim the video. Open the *.mp4 with Windows 10 Photos then use the edit tools in the upper right to easily define the trim. More details at http://www.tech-recipes.com/rx/56780/trim-videos-windows-10-photos/
Subscribe to Posts [Atom]