Cutting the beginning and end off a recorded file
Added by Andy Lear over 13 years ago
Hi,
This may well be a daft question, but is there a nice easy one line command that will allow me to take off the first 5 minutes say of a recording and the last 2 minutes whilst maintaining the rest of the file, ie audio and video and subtitles just copy for the section I want?
I have tried things like this:
mencoder -ss 00:05:44 -oac copy -ovc copy -idx recorded_file.mkv -o output.mkv
but get really strange results.
eg. in mplayer the sound is fine but the video is really sped up, however in vlc the video is fine, but no sound!
Any help is much appreciated
Andy
Replies (26)
RE: Cutting the beginning and end off a recorded file - Added by Hein Rigolo over 13 years ago
it is a mkv ... so I would use mkvmerge .. and use the :
File splitting, linking and appending (more global options):
--split specification
Splitting after specific timecodes.
Syntax: --split timecodes:A[,B[,C...]]
Example: --split timecodes:00:45:00.000,01:20:00.250,6300s
The parameters A, B, C etc must all have the same format as the ones used for the duration (see above). The list of timecodes is separated by commas. After the input stream has reached the current split point's timecode a new file is created. Then the next split point given in this list is used.
The 'timecodes:' prefix must not be omitted.
see http://www.bunkus.org/videotools/mkvtoolnix/doc/mkvmerge.html
Hope this helps, did not try it myself.
Hein
RE: Cutting the beginning and end off a recorded file - Added by JayJay - over 13 years ago
Hello,
yes splitting works fine here. Not frame accurate, but for me its ok. I cut a few minutes before an after a record like "00:02:21.7,01:35:04.2".
The i have only the Movie.
JJ
RE: Cutting the beginning and end off a recorded file - Added by Andy Lear over 13 years ago
Thanks Guys - works a treat - now if only I can work out how to strip out the video make it mpeg4 and reinsert it with the audio and subtitles
Andy
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet over 13 years ago
Any recommendation to split recordings with a GUI?
Otherwise I find it hard to determine the timecodes fr or splitting.
RE: Cutting the beginning and end off a recorded file - Added by Stéphane Bidoul over 13 years ago
Hi Markus,
Last year I also tried to find a solution to cut mkv files easily and never found something that was suiting my tastes.
So I resolved to write mine. It's working quite nicely to fit my needs, but I never got the time to package it.
Since you seem to have just the same requirements, I just pushed the code on github (https://github.com/lesbi/edledit)
What you'll find there is a small GUI program (edledit/edledit.py) to preview a mkv file, and create cuts. There is a variety of shortcuts to start/end cuts, move by adjustable steps in the movie (including shortcuts to reduce the step size and move forward or backwards in one operation, to quickly zero-in on the point where you want to cut).
edledit saves the cutlist as an edl file (http://www.mplayerhq.hu/DOCS/HTML/en/edl.html), which can be interpreted natively by mplayer and XBMC.
If you want to definitively remove the cut parts, you can then run the included mkvedlmerge which will read the edl file and call mkvmerge to split the mkv and merge it again without the cuts. Be careful: while edledit is most probably harmless, mkvedlmerge may wreck havoc with your precious movies, so please test it before doing anything serious with it...
Let me know what you think.
Best regards,
-sbi
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet over 13 years ago
Hi Stephane,
sorry for answering late, I was quiet busy in the past weeks.
I downloaded your project but I am not able to get it started.
I tried to start edledit/edledit.py with no luck:
[markus@fedora-desktop edledit]$ ./edledit.py
Traceback (most recent call last):
File "./edledit.py", line 31, in <module>
from edledit_ui import Ui_MainWindow
ImportError: No module named edledit_ui
I am running Fedora14 on x86_64, if there is any dependency.
Let me know how to call it right.
Markus
RE: Cutting the beginning and end off a recorded file - Added by Stéphane Bidoul over 13 years ago
Hi Markus,
I'm back online.
The generated ui and translation file were missing in the repository. They could be regenerated with make, but then you'd need the python-qt4 development tools, so I've added them to the repository.
Try "git pull" to update, and run edledit/edledit.py again. If you get an import error on phonon, you need to install python-qt4-phonon (at least that's how the package is called in Ubuntu; in Fedora, I believe the PyQt4 package provides phonon too).
Best regards,
-sbi
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet about 13 years ago
Hi Stephane,
finally I tried the latest revision from the repository.
edledit starts now and SD recordings can be edited fine.
But as usual, HD recordings are not seekable, the file seems to be corrupt.
Are you able to process HD (H264) recordings?
I assume that HD-like MKV recordings are not written correctly by tvheadend, as every way I tried to cut those files ended in a disaster.
Markus
RE: Cutting the beginning and end off a recorded file - Added by Hein Rigolo about 13 years ago
Tvheadend creates its MKV files based on the unmoddified data stream that it receives from the adapter. This means that sometimes packages are received that are out of order. This is no big problem within the mkv (there is PTS in there) but it makes seeking harder. Also there is (as far as I can tell) no cueing data in the mkv because that data is not in the data stream. If you want to make the mkv's seekable then you need to process the mkv first with the mkv tools in order to add this data and reorder the frames to put them in the right order according to their PTS.
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet about 13 years ago
Hein, thanks a lot for your explanations. This helps me a lot understanding the problem.
Two questions occur:
- Why are SD (MPEG2) recordings not affected in the same way? I can process them all fine w/o exception.
- Which mkv tool and how shall I use this to process the MKV file? I'd like to be able to cut off the begin and the end of a recording. Currently I haven't be able to do so, neither Linux nor Windows based tools.
Thanks
Markus
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet about 13 years ago
I'd like to refresh this topic.
Until today, I was not able to cut any of my recorded HD MKVs produced by tvheadend.
So one simple question: Is anybody able to process HD recordings and if so, which tool do you use?
RE: Cutting the beginning and end off a recorded file - Added by Eric Valette about 13 years ago
Ther was a problem with HD files because H264 was plain wrong. Now it has been fixed and mkvmerge does work. ffmpeg also.
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet about 13 years ago
OK, so I picked up andoma-tvheadend-6bb8d14 and did some HD recordings with it.
Hopefully it's not too obvious.. but how do I cut with ffmpeg or mkvmerge? Can't find anything so far.
For SD recordings I used to convert to a TS file and cut with ProjectX or avidemux.
Any more hints will be highly appreciated.
Thanks
Markus
RE: Cutting the beginning and end off a recorded file - Added by Eric Valette about 13 years ago
without being rude I would say RTM. A simple man would point you to the --slpit option for mkvmerge and to -ss and -t option to ffmpeg.
RE: Cutting the beginning and end off a recorded file - Added by Hein Rigolo about 13 years ago
Well, i can understand that if you think about "cutting" you do not look for "split" ......
But ...
One of the first replies in this thread already talks about the --split funtion of mkvmerge...
Hein
RE: Cutting the beginning and end off a recorded file - Added by Stéphane Bidoul about 13 years ago
Markus,
Hopefully it's not too obvious.. but how do I cut with ffmpeg or mkvmerge? Can't find anything so far.
Have a look at my post 5 months ago in this thread.
If you can now create the .edl file with edledit, then you can remove the cut parts with mkvedlmerge (which you can find in the same git repo). mkvedlmerge simple uses the --split option of mkvmerge then mkvmerge again to join the parts.
Best regards,
-sbi
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet about 13 years ago
Sorry, maybe I wasn't very clear with my questions.
I am very aware that I am able to cut/split the files with mkvmerge and ffmpeg. I know the options.
But honestly I need something easy to use. How shall I determine correctly the timestamps before I need them to pass to ffmpeg etc.
That's why I was refering to ProjectX or avidemux where this is done within a single step.
I was more looking into a solution like this.
But Stephane: Thanks, I will give it another try with the new recordings. Hopefully it'll succeed this time.
RE: Cutting the beginning and end off a recorded file - Added by Stéphane Bidoul about 13 years ago
Hi Markus,
I just tried to cut a couple of HD recordings from tvheadend git-89374fa, without much success.
Both edledit and totem segfault on these files.
I don't know the source of the problem, but I suspect the mkv's are incorrect, because the duration provided by mkvinfo is wrong (8h, where the actual duration is about 2h30'). I attach the mkvinfo, in case anyone can provide hints on how to debug this.
-sbi
mkvinfo.txt (2.63 KB) mkvinfo.txt |
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet about 13 years ago
Interesting, as I stated earlier I downloaded andoma-tvheadend-6bb8d14 on 2011-12-03 and now it works for me.
I was able to set split points with your tool and mkvmerge did the job.
Besides the problems with the recordings, I'd like to give some feedback on the tool, if you like to know:
- For my personal flavor I don't like the video to play immediately when I open a file
- It would be awesome to include some "Process" button which calls the mkvmerge process (some settings fields might be required to specify the output file)
- I am a bit confused about the skips I can have during a file. Isn't cutting/splitting restricted to key frames w/o having to re-encode? I would appreciate to have direct buttons to skip forward and backwards through the stream, aligned to the next possible cut point (key frame).
All in all, extending your tool a bit into the direction like ProjectX handles cutting files, this would be awesome.
Until then: Thanks a lot for your tool so far! It's the first time I am able to cut HD recordings.
RE: Cutting the beginning and end off a recorded file - Added by Stéphane Bidoul about 13 years ago
Hi Markus,
Thanks for the feedback. It's good to know it works for you.
The "Process" button is a good idea, although it may require some extra UI work to make it useable (ie providing progress feedback to the user, etc).
Regarding the keyframes, mkvmerge splits "right before the next key frame after the split point". Unfortunately the Qt Phonon VideoPlayer widget I use does not allow to seek precisely to keyframes, so it will remain approximate.
Best regards,
-sbi
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet about 13 years ago
About the progress output: it would sufficient for me to have some extra output attached to the window showing the text output you usually get when mkvmerge is called in the shell. No progress bar or such thing is necessary.
About the key frames: Isn't it possible to pre-read the key frames from the file? So there would be something like an internal lookup-table for the file, holding the information that key frame xy is at position mm:ss:ms? This way the exact position for cutting can be determined? Maybe the ffmpeg libraries will provide something useful.
Kind regards
Markus
RE: Cutting the beginning and end off a recorded file - Added by Markus Bonet about 13 years ago
Stephane,
let me know if I can assist you anyhow. Testing etc.
Best regards
Markus
RE: Cutting the beginning and end off a recorded file - Added by Atsea Confused over 9 years ago
You may also use this method to trim video to cut the beginning and end off a recorded file at http://www.idealshare.net/trim-video-mp4-mkv-avi-mov-wmv.html
RE: Cutting the beginning and end off a recorded file - Added by Nick Sillito over 9 years ago
Sorry for coming to this late, but in answer to your question is there a GUI programme to cut the start and finish off a x264 encoded recording? I use avidemux (I am running on linux mint). You need version 2.6.8 or higher to support x264 video (the stock version in mint 17 / ubuntu 14.04 does not support this function)
RE: Cutting the beginning and end off a recorded file - Added by K Shea almost 8 years ago
I think it can be done using ffmpeg, but you would need to specify times from the start of the file:
ffmpeg -i "original file.ts" -ss 00:05:00 -to 01:02:00 -c copy "output file.ts"
Would copy from 5 minutes from the start to 1 hour and 2 minutes from the start of the file.
See https://trac.ffmpeg.org/wiki/Seeking and https://lists.ffmpeg.org/pipermail/ffmpeg-user/2016-January/030223.html (on that last one you might want to look at the entire thread). If there is a specific option in ffmpeg to trim a certain amount of time from the end of a file, I haven't found it. It looks to me like people are using ffprobe to get the duration, and then using that in a script to calculate the option values to be sent to ffmpeg. If the command really has to be all on one line for some reason, but it doesn't matter how long the line is, and if you have perl installed, you could try asking in one of the perl sites - for some reason some perl users get their kicks creating complex one-liners, so maybe one of them would be willing to tackle this for you.
I will note that ffmpeg doesn't always do well in preserving subtitles. I'm guessing you are not in the USA (due to your use of the word "whilst" - we use "while" here) but in my experience ffmpeg does not usually properly pass ATSC subtitles, even if you tell it to do a straight copy of all streams using the -c copy option. Whether it works any better in non-ATSC countries I have no idea.