News:

--

Main Menu

adding jpg or jpeg file

Started by randykshlerin, January 29, 2021, 06:10:00 AM

Previous topic - Next topic

randykshlerin

Can please someboy help a dummy like me? I want to paste a jpg image at the beginning and at the end of a mp4 file. They should stay 2 sec. max before the beginning and after the end. Thanks for help and suggestions, I could not find info on that subject

eumagga0x2a

First of all, ensure the size of the image exactly matches the size (storage width and height, not display width and height) of the source video. You also need to know the properties (codec, number of channels, sampling rate) of all audio tracks in the video.

1. The first step is to prepare the source for audio tracks, e.g. generating a silent audio with the required number of channels and sampling rate with Audacity and exporting it as signed 16-bit low endian WAV file.

2. Load the image in Avidemux, choose a codec of your liking (I would recommend lossless Ut Video codec for intermediate steps, else disable B-frames in the configuration of encoders like x264 or x265) and add "Still Image" video filter configured to repeat the image for the duration of 2 seconds.

If the FPS of the source video is not equal 25, add "Resample FPS" filter and configure it to the necessary FPS value (avoid using a custom value, select the matching one from the menu if possible!).

Now add the silent WAV as external audio track ("Audio" --> "Select Track", then enable the first one, open the dropdown menu and point to the file), choose audio encoder, configure it to output approx. the same bitrate as in the source video.

The output format (no AVI, please!) doesn't need to be MP4, it must be compatible with the codec, obviously. If you have chosen Ut Video as intermediate codec, you must select MKV as container.

Save (export) the video which will become the pre- and post-roll later.

3. To avoid potential complications, disable hardware accelerated decoding in Avidemux, if enabled. Restart the application.

4. Load the source video, go to the end (with alternative keyboard shortcuts enabled, press "E"), append the file with the post-roll video, reset markers ("R") go to the next keyframe (this will be the first frame of the post-roll), press "I" to set the A marker, then Ctrl+C to copy to clipboard, "S" to go to the start of the entire video, Ctrl+V to paste the post-roll, now becoming pre-roll.

Seek to the start ("S"), play the video to verify that the audio is played fine after the switch to the main video. If it does, proceed. Else you have to replace the audio track in the post-roll with a better match first.

Alternatively, you might choose to re-encode the audio track(s) in the source video before repeating the step 4 to match the post-roll file.

5. Choose the video encoder and muxer of your liking and configure it to match your needs. Set audio encoder to "Copy".

6. Save the video.

dosdan

#2
Quote from: randykshlerin on January 29, 2021, 06:10:00 AMCan please someboy help a dummy like me? I want to paste a jpg image at the beginning and at the end of a mp4 file. They should stay 2 sec. max before the beginning and after the end.

eumagga0x2a has explained how you can add start & end JPEGs to a video clip. If you want to do this for many video clips, and the start & end JPEGS are always the same, an easier way would be to create 2 short clips, intro.mp4 & ending.mp4, and use FFMPEG to prepend and append ("top & tail") them to all the videos in a text list. All the videos should have the same height, width and fps. Since this process involves a format-change-without-recompression, there is no additional quality loss and it's fast.

1. To get to the command prompt in a specific directory in Windows, first navigate there in Windows File Manager and then, in the address bar at the top of this window, type in cmd and press Enter to open a command prompt window at this location.

2. Create a name-sorted list of the MP4s you want to process.  For example:

dir /b *.mp4 > main_videos.txt would create a list of all the MP4s in the current directory.  If you are operating on a NTFS-formatted drive, the default is name-order sorting.  Note: any time you rerun this line it overwrite any previous list.

dir /b testfile*.mp4 > main_videos.txt would create a list of all the MP4s in the current directory whose filenames begin with "testfile".

To see what's in the list: type main_videos.txt

C:\test directory\Appending_Test>type main_videos.txt
testfile 1.mp4
testfile 2.mp4
testfile 3.mp4
testfile 4.mp4

You can also edit this list in a text editor (e.g. Notepad) to remove some filenames, if you wish.

3. To process this list, you need the free program FFMPEG and a batchfile. Both need either to be in the current directory, or in a directory that is mentioned in your PATH environmental variable list. (Type path to see what directories are currently in your PATH list.)

The batchfile:
topandtail_MP4s.bat
@echo off
setlocal EnableDelayedExpansion

cls
ffmpeg -i intro.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -hide_banner -y intermediate1.ts
ffmpeg -i ending.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts -hide_banner -y intermediate3.ts

if not exist converted\nul md converted
for /F "delims=" %%G in (main_videos.txt) do (
ffmpeg -i "%%~G" -c copy -bsf:v h264_mp4toannexb -f mpegts -hide_banner -y intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts|intermediate3.ts" -c copy -bsf:a aac_adtstoasc -hide_banner -y "converted\%%~G"
)

4. The batchfile expects intro.mp4 & ending.mp4 to be in the current directory. You could however place them in a different location and easily modify the batchfile to include their full pathname. 

5.  You could also incorporate the dir /b *.mp4 > main_videos.txt line in line 3 of the batchfile, but it would be a good idea to locate the intro and ending mp4s elsewhere as you don't want these filenames included in main_videos.txt. 

6. The batchfile creates a "converted" subdirectory under the current directory. This is where the top&tailed files are placed.

In the link below you can download testfile 1.mp4, a 30s 1080p/59.94fps processed clip which include 4s intro & ending segments. I haven't bothered including sound in the intro & ending portions, but you could. There is sound in the main portion, but this background sound was recorded at a low level. (This is not a processing fault.)

testfile 1.mp4

Dan.