Trim mp4 files without encoding it again - matlab

I have a .mp4 video file, I need to trim it, however no matter how I do it, trimmed video is being encoded again which results in noisy video.
What I've tried:
Open video with Matlab, read frames and write only the frames that I want to have in trimmed video, I use 'MPEG-4' option.
Trim video using Windows Movie Maker.
Trim video using VirtualDub.
In first 2 scenarios original mp4 movie is encoded again after trimming it. I couldn't get mp4 files open in VirtualDub.
So what would be the easiest way to trim a video without re-encdong it?

You can do the split and re-encode in one command.
Create a text file, list.txt,
like this
file 'in.mp4'
inpoint 48.101
outpoint 67.459
file 'in.mp4'
inpoint 76.178
outpoint 86.399
file 'in.mp4'
inpoint 112.140
outpoint 125.031
then run,
ffmpeg -f concat -i list.txt -an -crf 18 out_merged.mp4

I've solved it with the following commands:
ffmpeg.exe -ss 48.101 -t 19.358 -i in.mp4 -an out_part1.mp4
ffmpeg.exe -ss 76.178 -t 10.221 -i in.mp4 -an out_part2.mp4
ffmpeg.exe -ss 112.140 -t 12.891 -i in.mp4 -an out_part3.mp4
ffmpeg -i out_part1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intrmdt1.ts
ffmpeg -i out_part2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intrmdt2.ts
ffmpeg -i out_part3.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intrmdt3.ts
ffmpeg -i "concat:intrmdt1.ts|intrmdt2.ts|intrmdt3.ts" -c copy out_merged.mp4
And some explanation:
Giving -ss (start time) and -t (duration) options before -i (input) option avoids unnecessary decoding.
Not using -c copy provides transcoding hence result more precise cut (got this from here).
I used -an because I didn't need the audio, if you need audio just omit this option.
Before concatenating the resulting trimmed videos I needed to transcode them to mpeg transport streams, to achieve lossless concatenation (for more details you can see this link).

Related

Flutter: How to use flutter_ffmpeg to add overlays like watermarks and texts to a video?

I try to implement the video_editing feature to my app, and I'd tried the Tapioca Package and the Video_Manipulation package but found that they both do not meet my criteria, so I put my last hope on the flutter_ffmpeg package.
But as I read through its official doc on pub.dev, not a thing on my mind but "WHAT THE HECK", I can't understand what those commands are used for, and though I can't find anything related to add widget overlays to a video. And almost no tutorial on the web that explains how to use it.
So if you successfully implemented adding watermarks/texts to a video with the ffmpeg package, please show me how. Thanks!~
ffmpeg -i video.mp4 -i logo.png -filter_complex "[0:v][1:v]overlay=5:5,drawtext=text=:x=(w-0)/8:y=(h-4)/10:fontsize=64:fontcolor=white:ryanwangTV" -c:a copy -movflags +faststart output.mp4
ffmpeg -i video.mp4 -i logo.png
there are the video in cuestion to work and the png image that we want to apply how watermark
the video.mp4 has two "parts" a video and a audio file, remember it
the logo.png is a one image, but it consederer a "video" the duration is miliseconds.
how you call parts of video.mp4 and logo.png?
using mapping, for file 1) you will called [0] and for file 2 (logo.png) you will used [1]
if you want to use the video of video.mp4 you will call [0:v] and the video of png is [1:v]
for watermark use filter complex, to "mix" the image on the video
"[0:v][1:v]overlay=5:5,drawtext=text=:x=(w-0)/8:y=(h-4)/10:fontsize=64:fontcolor=white:ryanwangTV
[0:v][1:v] is the video of video.mp4 and image of logo.png
overlay=5:5 the first 5 is the main video, and the second 5 is the image to put on of the video.
x=(w-0)/8 : is the coordenada x y=(h-4)/10 : the coordenada y
fontsize=64 fontcolor=white and the ultimate word is text that you
want to draw in video
-c:a copy its mean: copy the audio of file 1
-movflags +faststart : is to fast start for users of internet on browsers
output.mp4 is the name final
//audio replace on video
- String commandToExecute ='-r 15 -f mp4 -i ${AllUrl.VIDEO_PATH} -f mp3 -i ${AllUrl.AUDIO_PATH} -c:v copy -c:a aac -map 0:v:0 -map 1:a:0 -t $timeLimit -y ${AllUrl.OUTPUT_PATH}';
//To combine audio with image
String commandToExecute = '-r 15 -f mp3 -i ${AllUrl.AUDIO_PATH} -f image2 -i ${AllUrl.IMAGE_PATH} -pix_fmt yuv420p -t $timeLimit -y ${AllUrl.OUTPUT_PATH}';
//overlay Image on video
String commandToExecute = "-i ${AllUrl.VIDEO_PATH} -i ${AllUrl.IMAGE_PATH} -filter_complex overlay=10:10 -codec:a copy ${AllUrl.OUTPUT_PATH}";
/// To combine audio with gif
String commandToExecute = '-r 15 -f mp3 -i ${AllUrl.AUDIO_PATH} -f gif -re -stream_loop 5 -i ${AllUrl.GIF_PATH} -y ${AllUrl.OUTPUT_PATH}';
/// To combine audio with sequence of images
String commandToExecute = '-r 30 -pattern_type sequence -start_number 01 -f image2 -i ${AllUrl.IMAGES_PATH} -f mp3 -i ${AllUrl.AUDIO_PATH} -y ${AllUrl.OUTPUT_PATH}';

How to http stream FFMPEG encoded frames with VLC

I have a python script that write images (numpy arrays) on the standard output.
I want to keep this frames and encode them h264 with FFMPEG, using GPU, then give it to vlc to expose a stream over http.
Here there's a working example of my apporach, without the part of h264 encoding:
python3 script.py | ffmpeg -r 24 -s 1920x1080 -f rawvideo -i - -vcodec copy -f avi - | cvlc --demux=rawvideo --rawvid-fps=25
--rawvid-width=1920 --rawvid-height=1080 --rawvid-chroma=RV24 - --no-audio --sout '#transcode{vcodec=MJPG,venc=ffmpeg{strict=1}}:standard{access=http{user=pippo,pwd=pluto,mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a},mux=mpjpeg,dst=:10001/}'
Now, I'm having troubles in writing working pipes to do what I need.
Here the pipe I'm actually working on, FFMPEG process is managed by GPU, but VLC cannot correctly manage the flow, I suppose, in fact I can connect to VLC from another VLC instance used as client, but then I got an error in which VLC client cannot open MRL.
Here the pipe:
python3 script.py | ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -f rawvideo -s 1920x1080 -i - -c:a copy -c:v h264_nvenc -f h264 - | cvlc --demux=rawvideo --rawvid-fps=25 --rawvid-width=1920 --rawvid-height=1080 --rawvid-chroma=RV24 - --no-audio --sout '#transcode{vcodec=MJPG,venc=ffmpeg{strict=1}}:standard{access=http{user=pippo,pwd=pluto,mime=multipart/x-mixed-replace;boundary=--7b3cc56e5f51db803f790dad720ed50a},mux=mpjpeg,dst=:10001/}'
I don't understand how to set vlc parameters in order to manage the incoming stream. I also could have made errors in ffmpeg pipe, any suggestion is welcome.

Text streaming with RTMP?

I'm trying to get the output of a bash file to an RTMP stream.
I've successfully done it with FFMPEG using a filter, but the stream stops at Random intervals.
I assume that it's FFMPEG reading NULL data from the file.
I already write another file "output.txt", delete " input.txt" (which FFMPEG is reading) and rename "output.txt" to "input.txt".
Is there any way to do it more atomic in bash so it will work? Or is there a more elegant way to turn a changing text (max one time per second) to an FFMPEG stream?
Here is my current script:
ffmpeg -s 1920x1080 -f rawvideo -pix_fmt rgb24 -r 10 -i /dev/zero -f lavfi -i anullsrc -vcodec h264 -pix_fmt yuv420p -r 10 -b:v 2500k -qscale:v 3 -b:a 712000 -bufsize 512k -vf "drawtext=fontcolor=0xFFFFFF:fontsize=15:fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf:textfile=input.txt:x=0:y=0:reload=1" -f flv "rtmp://example.com/key"

How to extract the bitstream from H.264 video?

I have a H.264 video and I want to extract the bitstream from it. In other words, I need to know the stream after encoding a video via H.264 standard. I am going to use the extracted stream in Matlab. How can I do this (extract the bitstream)? Is it possible to use ffmpeg?if so what is the command.
You can do this using
ffmpeg -i in.mp4 -c copy -f h264 stream.264
Depending on what MATLAB expects, you may need to add a bitstream filter
ffmpeg -i in.mp4 -c copy -f h264 -vbsf h264_mp4toannexb stream.264

Cut parts of subtitle file using FFMPEG

I want to edit an input mp4 file with audio video subtitle streams, i can successfully cut video and audio streams by:
1- Convert each part to a new mp4 file
2- Convert mp4 files to mpg files
ffmpeg -i out00.mp4 -qscale:v 0 intermediate1.mpg
ffmpeg -i out01.mp4 -qscale:v 0 intermediate2.mpg
3- Merge mpg files
ffmpeg -i concat:"intermediate1.mpg|intermediate2.mpg" -c copy intermediate_all.mpg
4- Convert the mpg file back to mp4
ffmpeg -i intermediate_all.mpg -f mp4 output.mp4
and i can extract the subtitle stream to srt file, but i can't cut parts of the subtitle file, do you have an idea how to do this?
Ok it works after long searching trip, here what i did:
1- Download latest FFMPEG from here (this was causing alot of problems for me).
2- Add the subtitle to the input files:
ffmpeg -i "input.mkv" -i subtitles.srt -c copy -c:s srt -metadata:s:s:1 language=eng -map 0 -map 1 -map_metadata 0 output.mkv
3- Cut the parts you need as you did normally ffmpeg will cut subtitle stream also
ffmpeg -i input.mkv -ss 00:00:00 -t 00:01:35 -c copy -map 0:0 -map 0:1 -map 0:2 -qscale:0:V 0 -y out0.mkv
4- Join the cut files using concat demux:
ffmpeg -f concat -i mylist.txt -c copy output.mkv
where mylist.txt contains:
file out0.mkv
file out1.mkv
And enjoy :).
I think you have to edit the srt manually (text and timecode the text needs to appear at) and remux it to your final video with a command like:
ffmpeg -i EditedSource.mp4 -i EditedSub.srt -c copy -map 0:a -map 0:v -map 1:s output.mp4
And instead of creating these intermediate files, have you tried:
ffmpeg -i out00.mp4 -i out01.mp4 -filter_complex '[0][1]concat=n=2:v=1:a=1 [v][a]' -map '[v]' -map '[a]' output.mp4