Log in

View Full Version : Trim with audio and video


App_rone
4th February 2017, 05:03
I'm recently new to Avisynth. I know how to open plugins and trim, but a problem I've having is the audio. I converted my .avs trim script to mp4 in MeGUI, but then I have no audio. Not even adding audio=true works.

I've heard that I have to use MKVToolNix for the audio and video together.

How do I do so?

FranceBB
4th February 2017, 10:02
Use FFMpegSource2("video.mkv", atrack=-1)

Then, send video information to x264 or to your preferred video codec and audio information to your preferred audio codec, like AAC.
In order to do so, make a batch file like this one:

@echo off
@avs4x264mod.exe "AVS Script.avs" --x264-binary "x264.exe" --crf 22 --preset medium --level 4.1 --tune animation --profile High --vbv-maxrate 25000 --vbv-bufsize 32000 --deblock -1:-1 --aq-mode 3 --aq-strength 1.0 --overscan show --colormatrix bt709 --range tv --log-level info --thread-input --opencl --transfer bt709 --colorprim bt709 --aud --videoformat component --nal-hrd vbr --bluray-compat --nr 200 --psy-rd 0.6:0 --output raw_videoHD.h264
@echo+
@Bepipe --script "Import(^AVS Script.avs^)" | neroAacEnc.exe -lc -br 320000 -if - -of "audioHD.m4a"
@echo+
@mp4box -add raw_videoHD.h264 -add audioHD.m4a final_outputHD.mp4
@echo+
pause


You'll need:. avs4x264mod, x264.exe, NeroAAC.exe, BePipe, and mp4box.
I can make a folder and upload it for you if you want.

Oh, almost forgot: do not use meGUI, use command line and codecs.

lansing
4th February 2017, 10:07
If your source audio is not a WAV, then there's no point to do the audio trimming in Avisynth because that would mean re-encoding.

Instead you should look for programs that do audio trimming losslessly(no re-encoding). Avidemux is a top choice here because it has a UI and editing controller, just load the video, do the trimming, set audio output to "copy", and save the audio.

real.finder
4th February 2017, 11:53
If your source audio is not a WAV, then there's no point to do the audio trimming in Avisynth because that would mean re-encoding.

Instead you should look for programs that do audio trimming losslessly(no re-encoding). Avidemux is a top choice here because it has a UI and editing controller, just load the video, do the trimming, set audio output to "copy", and save the audio.

I have this http://forum.doom9.org/showthread.php?t=169961 :D

App_rone
4th February 2017, 22:16
Use FFMpegSource2("video.mkv", atrack=-1)

Then, send video information to x264 or to your preferred video codec and audio information to your preferred audio codec, like AAC.
In order to do so, make a batch file like this one:

@echo off
@avs4x264mod.exe "AVS Script.avs" --x264-binary "x264.exe" --crf 22 --preset medium --level 4.1 --tune animation --profile High --vbv-maxrate 25000 --vbv-bufsize 32000 --deblock -1:-1 --aq-mode 3 --aq-strength 1.0 --overscan show --colormatrix bt709 --range tv --log-level info --thread-input --opencl --transfer bt709 --colorprim bt709 --aud --videoformat component --nal-hrd vbr --bluray-compat --nr 200 --psy-rd 0.6:0 --output raw_videoHD.h264
@echo+
@Bepipe --script "Import(^AVS Script.avs^)" | neroAacEnc.exe -lc -br 320000 -if - -of "audioHD.m4a"
@echo+
@mp4box -add raw_videoHD.h264 -add audioHD.m4a final_outputHD.mp4
@echo+
pause


You'll need:. avs4x264mod, x264.exe, NeroAAC.exe, BePipe, and mp4box.
I can make a folder and upload it for you if you want.

Oh, almost forgot: do not use meGUI, use command line and codecs.

But I heard that I have to use MKVToolNix for this

FranceBB
4th February 2017, 23:15
mkvtoolnix if you wanna mux in .mkv, mp4box if you wanna mux in .mp4.

I've uploaded everything you need in a folder; you just have to move your video into such a folder, edit the AVS Script with the name of the file instead of "video.mkv" and insert the trim you need. Save the AVS Script, then double click on "Command Line H.264.bat" and the audio/video encode with the trim will automatically start ;)
Oh, you can encode the audio only as well, you just have to delete the video part from the .bat file, of course ;)
Last but not least, as to the settings of both x264 and NeroAAC, feel free to change/adjust them to meet your needs.

Link: x264/AAC Audio/Video Encoder (https://mega.nz/#F!KU8giTYS!CUBLz2gziyvxgAUmDWo23w)

Cheers.

App_rone
5th February 2017, 06:28
I made the script already but thanks

I need an audio stream

And raw video

App_rone
5th February 2017, 17:58
The audio and video encoding with trim didn't work but I figured out my own way.

However, the audio is out of sync of the video, but yet the same length.

How do I fix this?

FranceBB
6th February 2017, 00:51
The audio and video encoding with trim didn't work but I figured out my own way.

However, the audio is out of sync of the video, but yet the same length.

How do I fix this?

If it's out of sync, make sure you specify fpsnum and fpsden in FFMpegSource2:

FFMpegSource2("video.mkv", fpsnum=24000, fpsden=1001)

For 23.976.
30000/1001 for 29.970
25000/1000 for 25.

EDITED:
@app_rone

By the way, there's no way that such a folder doesn't work. If that's because you don't have an AVX capable CPU, just replace my x264 with another one or simply use NeroAAC to trim audio, or use Virtual Dub, save audio only and then mux it with the video. There are many ways to save a trimmed audio file.

hello_hello
6th February 2017, 19:53
I'm recently new to Avisynth. I know how to open plugins and trim, but a problem I've having is the audio. I converted my .avs trim script to mp4 in MeGUI, but then I have no audio. Not even adding audio=true works.
How do I do so?

MeGUI has an "AVS Cutter" under the Tools menu.
Create a script without any Trims, open it with the AVS Cutter and apply your edits (it just adds Trims for you using a preview). The AVS Cutter can save a "cuts file" that can be loaded into the audio section. The cuts file ensures the audio is re-encoded to match the video "cuts". The cuts file is just a text file so you can open it with Notepad for a look-see and edit it if need be (if for example, you change a Trim in the video script).

There's also an Audio Cutter under the Tools menu that can load the same cuts file and split/splice/append audio to match the video without re-encoding it. You have to extract the audio first either using the File Indexer or by other means.

MeGUI always handles the video and audio individually and muxes the two into a single file when they're done, or you can do it yourself with MKVToolNixGUI etc. MeGUI's File Indexer will extract the audio from some containers (vob files. MKV etc) and load it into the audio section for you. For file types where it can't extract the audio, the File Indexer will create a script to re-encode the audio and load it into the audio section instead. You should be able to load the script you created with both audio and video into MeGUI's audio section.

Many common containers (AVI, MKV etc) can also be loaded directly into MeGUI's audio section, and MeGUI will create a script for encoding "behind the scenes", or you can create your own script which includes audio and load it into the audio section. I don't know how it'd handle Trims in a script though, I can't remember as I generally use the "cuts file" method.

When you have a script loaded into the video section and audio or a script loaded into the audio section, use the "Auto Encode" button if you want MeGUI to output a completed file. "Auto Encode" also lets you add existing streams for muxing such as audio you don't want to re-encode and subtitles etc. If you want to add an existing audio stream for muxing instead of re-encoding it, clear MeGUI's audio section by clicking the "x" button before clicking "Auto Encode". When AutoEncode opens you can add existing streams to the final output file.

Tip: Auto Encode lets you use the current video encoder settings or select a file size. If you happen to need a particular file size, you can still leave the video encoder configured for single pass encoding (quality based, CRF encoding for x264). If you do that, when you select a file size or bitrate using the Auto-Encode function, MeGUI will work out the required video bitrate to give you the specified file size/bitrate and automatically switch to 2 pass encoding for you. The video bitrate calculated takes the audio bitrate into account so video + audio = specified file size/bitrate.

App_rone
7th February 2017, 02:46
MeGUI has an "AVS Cutter" under the Tools menu.
Create a script without any Trims, open it with the AVS Cutter and apply your edits (it just adds Trims for you using a preview). The AVS Cutter can save a "cuts file" that can be loaded into the audio section. The cuts file ensures the audio is re-encoded to match the video "cuts". The cuts file is just a text file so you can open it with Notepad for a look-see and edit it if need be (if for example, you change a Trim in the video script).

There's also an Audio Cutter under the Tools menu that can load the same cuts file and split/splice/append audio to match the video without re-encoding it. You have to extract the audio first either using the File Indexer or by other means.

MeGUI always handles the video and audio individually and muxes the two into a single file when they're done, or you can do it yourself with MKVToolNixGUI etc. MeGUI's File Indexer will extract the audio from some containers (vob files. MKV etc) and load it into the audio section for you. For file types where it can't extract the audio, the File Indexer will create a script to re-encode the audio and load it into the audio section instead. You should be able to load the script you created with both audio and video into MeGUI's audio section.

Many common containers (AVI, MKV etc) can also be loaded directly into MeGUI's audio section, and MeGUI will create a script for encoding "behind the scenes", or you can create your own script which includes audio and load it into the audio section. I don't know how it'd handle Trims in a script though, I can't remember as I generally use the "cuts file" method.

When you have a script loaded into the video section and audio or a script loaded into the audio section, use the "Auto Encode" button if you want MeGUI to output a completed file. "Auto Encode" also lets you add existing streams for muxing such as audio you don't want to re-encode and subtitles etc. If you want to add an existing audio stream for muxing instead of re-encoding it, clear MeGUI's audio section by clicking the "x" button before clicking "Auto Encode". When AutoEncode opens you can add existing streams to the final output file.

Tip: Auto Encode lets you use the current video encoder settings or select a file size. If you happen to need a particular file size, you can still leave the video encoder configured for single pass encoding (quality based, CRF encoding for x264). If you do that, when you select a file size or bitrate using the Auto-Encode function, MeGUI will work out the required video bitrate to give you the specified file size/bitrate and automatically switch to 2 pass encoding for you. The video bitrate calculated takes the audio bitrate into account so video + audio = specified file size/bitrate.

The audio is off

lansing
7th February 2017, 03:37
The audio and video encoding with trim didn't work but I figured out my own way.

However, the audio is out of sync of the video, but yet the same length.

How do I fix this?

Your description has no meaning. What didn't work? What do you mean by "figure out my own way"?

Giving out lengthy advice at this point is pointless because we didn't even know what you did. You have to tell people what exactly did you do, what is the script and what is your procedures up to this point.

StainlessS
7th February 2017, 08:00
Is there an audio delay in the source ? (MediaInfo).

hello_hello
7th February 2017, 13:04
The audio is off

I wrote an essay and that's your description as to the steps you took?
The more info you provide, the more you help others help you. ;)

What does "off" mean? Out of sync by the same amount throughout? Does it change. How did you extract the audio or convert it etc? Nobody has a crystal ball, and there's many, many reasons the audio could end up out of sync. We don't even know the type of source file you're working with, or if the audio stays in sync when you don't use Trim. The devil's in the details. :)

Also.... posting a copy of any script you're using is generally helpful and can provide info you don't know you're providing, and if you're using MeGUI, pasting a copy of the entire log file is a very good idea. There's generally informative stuff within.

hello_hello
7th February 2017, 13:10
Oh, almost forgot: do not use meGUI, use command line and codecs.

But MeGUI only uses command line codecs.

FranceBB
8th February 2017, 00:36
@hello_hello... yes, but don't have the full control of what's going on under the hood, that's why I prefer command line.

Anyway, I don't wanna get off topic. :)

App_rone
8th February 2017, 00:43
Your description has no meaning. What didn't work? What do you mean by "figure out my own way"?

Giving out lengthy advice at this point is pointless because we didn't even know what you did. You have to tell people what exactly did you do, what is the script and what is your procedures up to this point.

I did the AVS trim in MeGUI, audio was same length as video but the audio starts before the video

hello_hello
8th February 2017, 07:17
@hello_hello... yes, but don't have the full control of what's going on under the hood, that's why I prefer command line.

Anyway, I don't wanna get off topic. :)

Yeah, I agree with that. There's the odd occasion when I want to try something, but MeGUI just isn't equipped for the appropriate command line, or the appropriate command line for a particular encoder etc, so you have to do it yourself, but it was just the "don't use MeGUI" line I had a bit of an issue with, as I'll confess I use it regularly and find 99.9% of the time it helps make tasks a little easier, but yes, there's definitely situations when it can't.

Plus wasn't MeGUI originally doom9's love child, back when "Me" meant it was a Mencoder GUI? You might upset him. ;)

I don't think there's a problem with getting a little off topic at this stage. We might as well talk about motor racing till App_rone supplies the requested info, which seems to be the forum equivalent of pulling teeth, although at a guess it sounds like he just needs to open the output file with MKVMToolNixGUI, apply an appropriate delay to the audio stream and remux.

poisondeathray
8th February 2017, 16:03
The audio and video encoding with trim didn't work but I figured out my own way.

However, the audio is out of sync of the video, but yet the same length.

How do I fix this?

You add a AV delay during muxing

For example if audio comes early 200ms, and is constant the whole way through (not progressively changing), you add a +200ms delay to the audio



You didn't post your script, but in megui , if you use a script that demuxes the audio, the delay value is written into the name . That value is automatically used when you put it into the audio section . If you used another method, then you can add it during muxing

Another approach is to add the audio to the script using audiodub(). This means you re-encode the audio, but the benefit is audio and video are trimmed together . You can check for sync in a player like mpchc by playing the avs. You can make adjustments shifting the audio with delayaudio()

hello_hello
9th February 2017, 15:44
You didn't post your script, but in megui , if you use a script that demuxes the audio, the delay value is written into the name . That value is automatically used when you put it into the audio section . If you used another method, then you can add it during muxing

Just to clarify (I think this is what you meant)...
Any audio delay in the file name is definitely taken into account when re-encoding extracted audio, and I'd assume when MeGUI creates a script for encoding the audio instead of extracting it, it still works the same way (I rarely use MeGUI for audio encoding so I can't remember) but the delay is always removed when re-encoding. ie If the extracted audio has "Delay 200ms" in the file name, MeGUI adds 200ms of silence to the beginning of the audio instead and the encoded audio will have no delay written to the file name, or it'll be "Delay 0ms" if it still writes one to the output file (I can't remember).

Of course App_rone could have extracted the audio from his top secret source files with a program that doesn't care about any audio delay. I don't think he extracted it with MeGUI, but apparently that's still a secret too.

App_rone
18th February 2017, 04:16
Tried with MP4 format and audio was in sync... how strange.

But anyways, the file I am trying to trim is in MPG, total length is 2:00:00.

I am trimming from the 10:00 to 11:10 mark. The video begins where it's supposed to be but the audio begins somewhere minutes before the 10:00 mark.

Also: the MPG is made of merged VOBs from a DVD.